Node Authlink: Fix errors reported by phpstan

Created on 11 August 2023, over 1 year ago
Updated 14 August 2023, over 1 year ago

Problem/Motivation

phpstan detects many inconsistencies in the code. Would be great to have all reported errors by phpstan fixed.

Current problems (run phpstan because this would probably quickly outdated)

------ ------------------------------------------------------------------- 
  Line   node_authlink.install                                              
 ------ ------------------------------------------------------------------- 
  41     Variable $messenger in isset() always exists and is not nullable.  
 ------ ------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   node_authlink.module                                                                                                                                                                   
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  31     Call to an undefined method Drupal\node\Entity\NodeType::getEntity().                                                                                                                  
  152    Cannot call method fetchCol() on Drupal\Core\Database\StatementInterface|null.                                                                                                         
  177    Cannot call method fetchCol() on Drupal\Core\Database\StatementInterface|null.                                                                                                         
  205    Cannot call method fetchField() on Drupal\Core\Database\StatementInterface|int|string|null.                                                                                            
  249    Function node_authlink_get_url() should return Drupal\Core\Url|null but returns false.                                                                                                 
  252    Call to an undefined method object::id().                                                                                                                                              
  422    Parameter #2 $value of method Drupal\Core\Database\Query\ConditionInterface::condition() expects array|Drupal\Core\Database\Query\SelectInterface|string|null, int|string|null given.  
  423    Parameter #2 $value of method Drupal\Core\Database\Query\ConditionInterface::condition() expects array|Drupal\Core\Database\Query\SelectInterface|string|null, (float|int) given.      
  424    Cannot call method fetchCol() on Drupal\Core\Database\StatementInterface|null.                                                                                                         
  482    Cannot call method toString() on Drupal\Core\Url|null.                                                                                                                                 
  486    Cannot call method toString() on Drupal\Core\Url|null.                                                                                                                                 
  490    Cannot call method toString() on Drupal\Core\Url|null.                                                                                                                                 
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/Access/NodeAuthlinkGroupContentAccessControlHandler.php                                                                                                                                                                    
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  16     Class Drupal\node_authlink\Plugin\NodeAuthlinkGroupContentAccessControlHandler extends unknown class Drupal\group\Plugin\GroupContentAccessControlHandler.                                                                     
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols                                                                                                                                                            
  22     \Drupal calls should be avoided in classes, use dependency injection instead                                                                                                                                                   
  30     Drupal\node_authlink\Plugin\NodeAuthlinkGroupContentAccessControlHandler::entityAccess() calls parent::entityAccess() but Drupal\node_authlink\Plugin\NodeAuthlinkGroupContentAccessControlHandler does not extend any class.  
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------ 
  Line   src/Access/NodeAuthlinkNodeAccessControlHandler.php                           
 ------ ------------------------------------------------------------------------------ 
  22     \Drupal calls should be avoided in classes, use dependency injection instead  
  26     \Drupal calls should be avoided in classes, use dependency injection instead  
 ------ ------------------------------------------------------------------------------ 

 ------ ------------------------------------------------------------------------------------------ 
  Line   src/Access/NodeRevisionAccessCheck.php                                                    
 ------ ------------------------------------------------------------------------------------------ 
  42     Variable $requirements in PHPDoc tag @var does not match assigned variable $requirement.  
  43     Parameter #2 $string of function explode expects string, string|null given.               
  57     Call to an undefined method Drupal\Core\Access\AccessResultInterface::cachePerUser().     
 ------ ------------------------------------------------------------------------------------------ 

 ------ ----------------------------------------------------------------------------------------------------------------------- 
  Line   src/Form/NodeAuthlinkNodeForm.php                                                                                      
 ------ ----------------------------------------------------------------------------------------------------------------------- 
  35     Access to an undefined property Drupal\node_authlink\Form\NodeAuthlinkNodeForm::$entityTypeManager.                    
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                                   
  36     Access to an undefined property Drupal\node_authlink\Form\NodeAuthlinkNodeForm::$linkGenerator.                        
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                                   
  37     Access to an undefined property Drupal\node_authlink\Form\NodeAuthlinkNodeForm::$dateFormatter.                        
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                                   
  44     Unsafe usage of new static().                                                                                          
         💡 See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static                                      
  77     Access to an undefined property Drupal\node_authlink\Form\NodeAuthlinkNodeForm::$entityTypeManager.                    
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                                   
  78     Access to an undefined property Drupal\node_authlink\Form\NodeAuthlinkNodeForm::$linkGenerator.                        
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                                   
  79     Access to an undefined property Drupal\node_authlink\Form\NodeAuthlinkNodeForm::$dateFormatter.                        
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                                   
  81     Parameter #6 $bundle_grants of class Drupal\node_authlink\Form\NodeFormManager constructor expects array, true given.  
  136    Cannot call method bundle() on Drupal\node\Entity\Node|null.                                                           
  136    Cannot call method bundle() on Drupal\node\Entity\Node|null.                                                           
  136    Cannot call method bundle() on Drupal\node\Entity\Node|null.                                                           
 ------ ----------------------------------------------------------------------------------------------------------------------- 

                                                                                                                        
 [ERROR] Found 32 errors                                                                                                
                                                                                                                        

Steps to reproduce

Run phpstan. I'm using Metadrop Boilerplate because it includes a docker env with phpstan preconfigured for Drupal. So, create a new project using composer, require node authlink, and inside the php container (make up, make shell) run:

phpstan analyze web/modules/development/node_authlink/

Proposed resolution

Fix errors.

Remaining tasks

Do it.

User interface changes

TBD.

API changes

TBD.

Data model changes

TBD.

📌 Task
Status

Fixed

Version

2.0

Component

Code

Created by

🇪🇸Spain tunic Madrid

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024