Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False violation on paragraph entity #276

Closed
agentrickard opened this issue May 26, 2022 · 2 comments
Closed

False violation on paragraph entity #276

agentrickard opened this issue May 26, 2022 · 2 comments

Comments

@agentrickard
Copy link

How is drupal-check installed?

drupal-check is installed as a dependency to my project

Environment:

  • OS: MacOS
  • PHP Version: 7.3
  • Drupal core: 9.3.12

Describe the bug

We have the following custom code (ignoring the dependency injection need and where data comes from:

    $paragraph_storage = $this->entityTypeManager->getStorage('paragraph');
    if ($paragraph = $paragraph_storage->load($data['target_id'])) {
      // If there is no role access field, grant access.
      $is_granted = TRUE;
      // Support paragraphs that don't have role access control fields.
      // @todo PHPSTAN cannot find this method, which exists.
      if ($paragraph->hasField('field_role_access')) {
        // Only grant if the role that should have access based on field value.
        $is_granted = FALSE;
        // @todo PHPSTAN cannot find this method, which exists.
        $paragraph_role_access = $paragraph->get('field_role_access')->value;

Tests for this code fails because it checks against EntityInterface and not the Entity class.

Console output

php vendor/bin/drupal-check -vvv web/modules/custom/tc_utility/src/Services/JsonUtilityService.php
Performing deprecation checks
Analyzing path: /Users/rickard/Sites/tc22/web/modules/custom/tc_utility/src/Services/JsonUtilityService.php
Current working directory: /Users/rickard/Sites/tc22
Using Drupal root: /Users/rickard/Sites/tc22/web
Using vendor root: /Users/rickard/Sites/tc22/vendor
Assumed running as global dependency
PHPStan path: /Users/rickard/Sites/tc22/vendor/phpstan/phpstan/phpstan.phar
PHPStan configuration path: /private/var/folders/kp/5qn2wy3163n8bqhx9cv1h25w0000gn/T/drupal_check_phpstan_1653576914.neon
PHPStan configuration:
parameters:
	tipsOfTheDay: false
	reportUnmatchedIgnoredErrors: false
	excludePaths:
		- */tests/Drupal/Tests/Listeners/Legacy/*
		- */tests/fixtures/*.php
		- */settings*.php
		- */node_modules/*

	ignoreErrors:
		- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
		- '#Plugin definitions cannot be altered.#'
		- '#Missing cache backend declaration for performance.#'
		- '#Plugin manager has cache backend specified but does not declare cache tags.#'
		- '#Unsafe usage of new static\(\)#'

	drupal:
		drupal_root: /Users/rickard/Sites/tc22/web

	level: 2
	bootstrapFiles:
		- /Users/rickard/Sites/tc22/vendor/mglaman/drupal-check/error-bootstrap.php

includes:
	- /Users/rickard/Sites/tc22/vendor/phpstan/phpstan-deprecation-rules/rules.neon
	- /Users/rickard/Sites/tc22/vendor/mglaman/phpstan-drupal/extension.neon


Executing PHPStan
Result cache not used because only files were passed as analysed paths.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% < 1 sec/< 1 sec 72.5 MiB

Result cache was not saved because only files were passed as analysed paths.
 ------ ------------------------------------------------- 
  Line   JsonUtilityService.php                           
 ------ ------------------------------------------------- 
  293    Call to an undefined method                      
         Drupal\Core\Entity\EntityInterface::hasField().  
  297    Call to an undefined method                      
         Drupal\Core\Entity\EntityInterface::get().       
 ------ ------------------------------------------------- 


 [ERROR] Found 2 errors                                                         

Used memory: 72.5 MB
Finished executing PHPStan
Unlinking PHPStan configuration
Return PHPStan exit code
Thanks for using drupal-check!
@mglaman
Copy link
Owner

mglaman commented May 27, 2022

Your code should make sure $paragraph is not null and is an instance of ParagraphInterface. Then the method will be understood.

The get and hasFIeld methods exist on FieldableEntityInterface. This is why PHPStan was bumped to level 2. With these unknown methods, you wouldn't know if one was deprecated.

@agentrickard
Copy link
Author

Thanks. This works:

      // Support paragraphs that don't have role access control fields.
      if ($paragraph instanceof ParagraphInterface && $paragraph->hasField('field_role_access')) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants