Fix PHPStan detected errors

Created on 27 January 2024, 5 months ago
Updated 31 January 2024, 5 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

Needs review

Version

4.0

Component

Other

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
    5 months ago
    Total: 31s
    #83484
  • 🇪🇸Spain jlbellido

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

  • Pipeline finished with Failed
    5 months ago
    Total: 92s
    #83506
  • Pipeline finished with Failed
    5 months 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
    5 months ago
    Total: 121s
    #83673
  • Pipeline finished with Failed
    5 months ago
    Total: 121s
    #83674
  • Status changed to Needs review 5 months 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 5 months 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 5 months ago
    186 pass, 13 fail
  • Pipeline finished with Failed
    5 months ago
    Total: 30s
    #83677
  • Pipeline finished with Failed
    5 months ago
    Total: 31s
    #83678
  • Pipeline finished with Failed
    5 months ago
    Total: 31s
    #85387
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 5 months ago
    186 pass, 13 fail
  • Pipeline finished with Success
    5 months 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!

Production build 0.69.0 2024