Fix PHPStan detected errors

Created on 27 January 2024, about 1 year ago
Updated 19 July 2024, 7 months ago

Problem/Motivation

At 📌 Set up to date the gitlab-ci.yml configuration RTBC We found we are not passing the validation checks for PHPStan.

An initial list of errors can be found at : https://git.drupalcode.org/issue/examples-3417410/-/jobs/699833

We would like to have the module Examples passing these checks with the final goal of having a clean pipeline to be used as base for the incoming examples or maintenance tasks of this module.

Proposed resolution

  1. Follow the instructions from https://www.drupal.org/docs/develop/development-tools/phpstan/getting-st... to have running PHPStan locally.
  2. Run the PHPStan against the Examples module.
  3. Fix all findings and make a MR with the fixes.

Notes: We don't have any phpstan.neon defined for this project, we might need to have one to adjust / ignore certain well known issues like Drupal Core does.
In addtion, ideally we are aiming for Level 8, but decide if it is really doable.

📌 Task
Status

Fixed

Version

4.0

Component

Code

Created by

🇪🇸Spain jlbellido

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

Merge Requests

Comments & Activities

  • Issue created by @jlbellido
  • Pipeline finished with Failed
    about 1 year ago
    Total: 31s
    #83484
  • 🇪🇸Spain jlbellido

    I'm attaching the initial report with all the issues detected before fixing anything:

  • Pipeline finished with Failed
    about 1 year ago
    Total: 92s
    #83506
  • Pipeline finished with Failed
    about 1 year ago
    Total: 122s
    #83505
  • 🇪🇸Spain jlbellido

    Hello,

    I've created an initial MR fixing most of the errors reported. It still pending the following one:

     ------ --------------------------------------------------------------------- 
      Line   modules/file_example/src/FileExampleSubmitHandlerHelper.php          
     ------ --------------------------------------------------------------------- 
      460    Class Drupal\devel\DevelDumperInterface not found.                   
             💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
    

    I don't really know if by using Stub files we can fix it since it is a conditional class not always present (depending is Devel is required or not).

  • Pipeline finished with Failed
    about 1 year ago
    Total: 121s
    #83673
  • Pipeline finished with Failed
    about 1 year ago
    Total: 121s
    #83674
  • Status changed to Needs review about 1 year ago
  • 🇪🇸Spain jlbellido

    I've checked in deep the error from #5 but I think Stub files is not the right way because they are meant for other kind of scenarios according to (https://phpstan.org/user-guide/stub-files). Therefore I don't see other way to get rid of it than ignoring it via php-baseline.neon

    I've added a new commit with this approach.

    Now we are passing the PHPStan checks for Level 1:

    $ php ../vendor/bin/phpstan analyze --configuration modules/contrib/examples/phpstan.neon modules/contrib/examples
     242/242 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
                                                                                                                            
     [OK] No errors                                                                                                      
    

    I think this is now ready to be reviewed.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    186 pass, 13 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    186 pass, 13 fail
  • Pipeline finished with Failed
    about 1 year ago
    Total: 30s
    #83677
  • Pipeline finished with Failed
    about 1 year ago
    Total: 31s
    #83678
  • Pipeline finished with Failed
    about 1 year ago
    Total: 31s
    #85387
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    186 pass, 13 fail
  • Pipeline finished with Success
    about 1 year ago
    Total: 255s
    #85399
  • 🇪🇸Spain jlbellido

    After [#] has been merged, now we can see we are passing the PHPStan check within !32 if we compare it with the last run on the main project (https://git.drupalcode.org/project/examples/-/pipelines/85372)

    Therefore I'd say this is fully ready to be reviewed by someone else.

    Thanks!

  • Status changed to Needs work 8 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    The errors reported by PHPStan are the following ones.

     ------ ----------------------------------------------------------------------- 
      Line   modules/config_entity_example/src/Form/RobotFormBase.php               
     ------ ----------------------------------------------------------------------- 
      218    Method Drupal\config_entity_example\Form\RobotFormBase::save() should  
             return int but return statement is missing.                            
     ------ ----------------------------------------------------------------------- 
     ------ ---------------------------------------------------------------------- 
      Line   modules/content_entity_example/src/Form/ContactForm.php               
     ------ ---------------------------------------------------------------------- 
      39     Method Drupal\content_entity_example\Form\ContactForm::save() should  
             return int but return statement is missing.                           
     ------ ---------------------------------------------------------------------- 
     ------ ---------------------------------------------------------------- 
      Line   modules/file_example/tests/src/Functional/FileExampleTest.php   
     ------ ---------------------------------------------------------------- 
      28     Property                                                        
             Drupal\Tests\file_example\Functional\FileExampleTest::$modules  
             property must be protected.                                     
             💡 Change record: https://www.drupal.org/node/2909426           
     ------ ---------------------------------------------------------------- 
     ------ ---------------------------------------------------------------------- 
      Line   modules/form_api_example/src/Form/InputDemo.php                       
     ------ ---------------------------------------------------------------------- 
      263    \Drupal calls should be avoided in classes, use dependency injection  
             instead                                                               
     ------ ---------------------------------------------------------------------- 
     ------ ----------------------------------------------------------------------- 
      Line   modules/rest_example/src/RestExampleClientCalls.php                    
     ------ ----------------------------------------------------------------------- 
      155    Method Drupal\rest_example\RestExampleClientCalls::create() should     
             return Symfony\Component\HttpFoundation\Response but return statement  
             is missing.                                                            
      197    Method Drupal\rest_example\RestExampleClientCalls::update() should     
             return Symfony\Component\HttpFoundation\Response but return statement  
             is missing.                                                            
      231    Method Drupal\rest_example\RestExampleClientCalls::delete() should     
             return Symfony\Component\HttpFoundation\Response but return statement  
             is missing.                                                            
     ------ ----------------------------------------------------------------------- 
     ------ ---------------------------------------------------------------------- 
      Line   modules/testing_example/src/Controller/TestingExampleController.php   
     ------ ---------------------------------------------------------------------- 
      33     \Drupal calls should be avoided in classes, use dependency injection  
             instead                                                               
     ------ ---------------------------------------------------------------------- 
    

     
    Part of them has been fixed in 📌 Require at least Drupal 10.3 and remove any usage of deprecated classes, methods, constants, or functions Fixed .

  • Pipeline finished with Failed
    8 months ago
    Total: 31s
    #216963
  • Merge request !66Issue #3417527: Fix PHPStan detected errors → (Merged) created by apaderno
  • Status changed to Needs review 8 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    apaderno changed the visibility of the branch 3417527-fix-phpstan-detected to hidden.

  • Status changed to Fixed 8 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024