Increase PHPStan analysis to level 2

Created on 28 May 2025, 9 days ago

Problem/Motivation

Currently we run PHPStan analysis at the default level 1. Let's do better!

Steps to reproduce

Increase _PHPSTAN_LEVEL in .gitlab-ci.yml and observe the new errors.

Proposed resolution

I propose we bump it to level 2 and see what that looks like. If it makes sense to go higher, let's do that too!

Remaining tasks

  1. Open MR that bumps the level.
  2. Fix errors.
  3. Repeat until satisfied.
  4. Merge.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ“Œ Task
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States m.stenta

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @m.stenta
  • Merge request !22Resolve #3527089 "Increase phpstan level" β†’ (Merged) created by m.stenta
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta

    Level 2 errors:

     ------ --------------------------------------------------------------- 
      Line   src/Controller/LogAutocompleteController.php                   
     ------ --------------------------------------------------------------- 
      62     Call to an undefined method                                    
             Drupal\Core\Entity\EntityStorageInterface::getTableMapping().  
     ------ --------------------------------------------------------------- 
     ------ ---------------------------------------------------------- 
      Line   src/Entity/LogType.php                                    
     ------ ---------------------------------------------------------- 
      131    Call to an undefined method                               
             Drupal\Core\Entity\EntityStorageInterface::updateType().  
     ------ ---------------------------------------------------------- 
     ------ ------------------------------------------------------- 
      Line   src/Form/LogCloneActionForm.php                        
     ------ ------------------------------------------------------- 
      100    Variable $new_date in PHPDoc tag @var does not exist.  
     ------ ------------------------------------------------------- 
     ------ --------------------------------------------------- 
      Line   src/Form/LogForm.php                               
     ------ --------------------------------------------------- 
      111    Call to an undefined method                        
             Drupal\Core\Field\FieldItemInterface::getLabel().  
     ------ --------------------------------------------------- 
     ------ -------------------------------------------------------------- 
      Line   src/Form/LogRescheduleActionForm.php                          
     ------ -------------------------------------------------------------- 
      148    Call to an undefined method                                   
             Drupal\Core\Field\FieldItemInterface::isTransitionAllowed().  
      149    Call to an undefined method                                   
             Drupal\Core\Field\FieldItemInterface::applyTransitionById().  
      161    Call to an undefined method                                   
             Drupal\Core\Field\FieldItemInterface::isTransitionAllowed().  
      162    Call to an undefined method                                   
             Drupal\Core\Field\FieldItemInterface::applyTransitionById().  
     ------ -------------------------------------------------------------- 
     ------ ---------------------------------------------------------------- 
      Line   src/Form/LogTypeForm.php                                        
     ------ ---------------------------------------------------------------- 
      83     Call to an undefined method                                     
             Drupal\Core\Entity\EntityInterface::getDescription().           
      90     Call to an undefined method                                     
             Drupal\Core\Entity\EntityInterface::getNamePattern().           
      103    Call to an undefined method                                     
             Drupal\Core\Entity\EntityInterface::getWorkflowId().            
      110    Call to an undefined method                                     
             Drupal\Core\Entity\EntityInterface::shouldCreateNewRevision().  
     ------ ---------------------------------------------------------------- 
     ------ ----------------------------------------------------------- 
      Line   src/LogStorage.php                                         
     ------ ----------------------------------------------------------- 
      143    Call to an undefined method                                
             Drupal\Core\Entity\EntityInterface::getTypeNamePattern().  
     ------ ----------------------------------------------------------- 
     ------ ---------------------------------------------------------------- 
      Line   src/Plugin/views/field/LogField.php                             
     ------ ---------------------------------------------------------------- 
      30     Call to an undefined method                                     
             Drupal\views\Plugin\views\query\QueryPluginBase::addOrderBy().  
      31     Call to an undefined method                                     
             Drupal\views\Plugin\views\query\QueryPluginBase::addOrderBy().  
     ------ ---------------------------------------------------------------- 
     ------ ---------------------------------------------------------------- 
      Line   src/Plugin/views/sort/LogStandardSort.php                       
     ------ ---------------------------------------------------------------- 
      23     Call to an undefined method                                     
             Drupal\views\Plugin\views\query\QueryPluginBase::addOrderBy().  
      24     Call to an undefined method                                     
             Drupal\views\Plugin\views\query\QueryPluginBase::addOrderBy().  
      48     Call to an undefined method                                     
             Drupal\views\Plugin\views\query\QueryPluginBase::addOrderBy().  
      49     Call to an undefined method                                     
             Drupal\views\Plugin\views\query\QueryPluginBase::addOrderBy().  
     ------ ---------------------------------------------------------------- 
     ------ --------------------------------------------------------------------- 
      Line   tests/src/Functional/LogActionsTest.php                              
     ------ --------------------------------------------------------------------- 
      34     Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      55     Call to method loadMultiple() on an unknown class                    
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      83     Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      106    Call to method loadMultiple() on an unknown class                    
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      128    Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      149    Call to method loadMultiple() on an unknown class                    
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      172    Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      195    Call to method loadMultiple() on an unknown class                    
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      217    Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      247    Call to method loadMultiple() on an unknown class                    
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      275    Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      298    Call to method loadMultiple() on an unknown class                    
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
     ------ --------------------------------------------------------------------- 
     ------ --------------------------------------------------------------------- 
      Line   tests/src/Functional/LogCRUDTest.php                                 
     ------ --------------------------------------------------------------------- 
      46     Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      52     Call to method load() on an unknown class                            
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      110    Call to method load() on an unknown class                            
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
     ------ --------------------------------------------------------------------- 
     ------ --------------------------------------------------------------------- 
      Line   tests/src/Functional/LogNamePatternTest.php                          
     ------ --------------------------------------------------------------------- 
      26     Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      32     Call to method load() on an unknown class                            
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      52     Call to method getQuery() on an unknown class                        
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      58     Call to method load() on an unknown class                            
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
     ------ --------------------------------------------------------------------- 
     ------ --------------------------------------------------------------------- 
      Line   tests/src/Functional/LogTestBase.php                                 
     ------ --------------------------------------------------------------------- 
      25     Property Drupal\Tests\log\Functional\LogTestBase::$storage has       
             unknown class Drupal\group\Entity\Storage\GroupRoleStorageInterface  
             as its type.                                                         
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
      91     Call to method create() on an unknown class                          
             Drupal\group\Entity\Storage\GroupRoleStorageInterface.               
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols  
     ------ --------------------------------------------------------------------- 
     ------ ---------------------------------------------------------------------- 
      Line   tests/src/Kernel/LogActionsTest.php                                   
     ------ ---------------------------------------------------------------------- 
      77     Method Drupal\Core\Executable\ExecutableInterface::execute() invoked  
             with 1 parameter, 0 required.                                         
      80     Call to an undefined method                                           
             Drupal\Core\Entity\EntityInterface::get().                            
      94     Method Drupal\Core\Executable\ExecutableInterface::execute() invoked  
             with 1 parameter, 0 required.                                         
      97     Call to an undefined method                                           
             Drupal\Core\Entity\EntityInterface::get().                            
     ------ ---------------------------------------------------------------------- 
     [ERROR] Found 44 errors
    
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta

    I fixed all the level 2 errors.

    Level 3 only adds 3 more errors, so let's fix those as well:

     ------ --------------------------------------------------------------------------------------------------------------------------------------- 
      Line   web/modules/log/src/Form/LogActionFormBase.php                                                                                         
     ------ --------------------------------------------------------------------------------------------------------------------------------------- 
      94     Method Drupal\log\Form\LogActionFormBase::getDescription() should return Drupal\Core\StringTranslation\TranslatableMarkup but returns  
             string.                                                                                                                                
     ------ --------------------------------------------------------------------------------------------------------------------------------------- 
    
     ------ ------------------------------------------------------------------------------------------------------------------------------------ 
      Line   web/modules/log/tests/src/Functional/LogTestBase.php                                                                                
     ------ ------------------------------------------------------------------------------------------------------------------------------------ 
      32     PHPDoc type array of property Drupal\Tests\log\Functional\LogTestBase::$modules is not covariant with PHPDoc type array<string> of  
             overridden property Drupal\Tests\BrowserTestBase::$modules.                                                                         
             πŸ’‘ You can fix 3rd party PHPDoc types with stub files:                                                                              
             πŸ’‘ https://phpstan.org/user-guide/stub-files                                                                                        
                                                                                                                                                 
      96     Method Drupal\Tests\log\Functional\LogTestBase::createLogEntity() should return Drupal\log\Entity\LogInterface but returns          
             Drupal\Core\Entity\EntityInterface.                                                                                                 
     ------ ------------------------------------------------------------------------------------------------------------------------------------
    
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta

    I fixed all the level 2 errors.

    Level 4 doesn't cause any errors, and level 5 only adds 5 more, so I'll try to fix those as well.

     ------ --------------------------------------------------------------------------------------------------------- 
      Line   web/modules/log/src/Form/LogActionFormBase.php                                                           
     ------ --------------------------------------------------------------------------------------------------------- 
      69     Parameter #1 $key of method Drupal\Core\TempStore\PrivateTempStore::get() expects string, int given.     
      124    Parameter #1 $key of method Drupal\Core\TempStore\PrivateTempStore::delete() expects string, int given.  
     ------ --------------------------------------------------------------------------------------------------------- 
    
     ------ ------------------------------------------------------------------------------------------------------ 
      Line   web/modules/log/src/Plugin/Action/LogActionBase.php                                                   
     ------ ------------------------------------------------------------------------------------------------------ 
      70     Parameter #1 $key of method Drupal\Core\TempStore\PrivateTempStore::set() expects string, int given.  
     ------ ------------------------------------------------------------------------------------------------------ 
    
     ------ ------------------------------------------------------------------------------------------------------------------ 
      Line   web/modules/log/tests/src/Functional/LogActionsTest.php                                                           
     ------ ------------------------------------------------------------------------------------------------------------------ 
      304    Parameter #1 $callback of function array_map expects (callable(Drupal\Core\Entity\EntityInterface): mixed)|null,  
             Closure(Drupal\log\Entity\LogInterface): mixed given.                                                             
     ------ ------------------------------------------------------------------------------------------------------------------ 
    
     ------ -------------------------------------------------------------------------------------------------- 
      Line   web/modules/log/tests/src/Functional/LogCRUDTest.php                                              
     ------ -------------------------------------------------------------------------------------------------- 
      21     Parameter #1 $code of method Behat\Mink\WebAssert::statusCodeEquals() expects int, string given.  
     ------ -------------------------------------------------------------------------------------------------- 
    
                                                                                                                            
     [ERROR] Found 5 errors
    
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta
Production build 0.71.5 2024