PHPSTAN: Raise validation level from 0 (min) to 6

Created on 5 April 2025, 4 months ago

Problem/Motivation

Currently, the module uses the default Gitlab CI templates for the PHPSTAN job:
https://git.drupalcode.org/project/gitlab_templates/-/raw/default-ref/as...

With the lowest rule level: 0, which only does very basic PHP code validation.

We would like to enforce stricter a validation of the module's code base, see the PHPSTAN rules levels:
https://phpstan.org/user-guide/rule-levels

Steps to reproduce

Run locally:

./vendor/bin/phpstan analyze web/modules/contrib/admin_toolbar/ -l 6

Many errors will be displayed.

Proposed resolution

Fix all validation errors and add a phpstan.neon configuration file to set the rule level to 6 for the job on Gitlab CI.

📌 Task
Status

Active

Version

3.0

Component

Code

Created by

🇫🇷France dydave

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

Merge Requests

Comments & Activities

  • Issue created by @dydave
  • Pipeline finished with Success
    4 months ago
    Total: 303s
    #466037
  • Merge request !133Resolve #3517506 "Phpstan validate level6" → (Merged) created by dydave
    • dydave committed c4ac4d9a on 3.x
      Issue #3517506 by dydave: PHPSTAN: Raised validation level from 0 (min)...
  • 🇫🇷France dydave

    Quick follow-up on this issue:

    Starting from level 3, I gradually raised the level and each time fixed all the errors prompted by PHPSTAN, until reaching level 6.

    Fixed all validation errors, mostly:

    • Updated Doc comment blocks return types.
    • Updated Doc comment blocks param types.
    • Added inline @var types definitions.
    • Changed 4 lines of code, to add elvis operators and reorder chained calls.
    • Caught a wrong variable passed in assert function in a Test class.

     
    Added project's phpstan.neon file to raise validation level to 6 and ignore certain error messages.
     

    Very few lines of code with very minor changes were impacted in the merge request.

    Since the Tests of the MR all passed 🟢, I went ahead and merged the changes above at #3.

    The configuration change was made on Gitlab CI by adding the file: phpstan.neon, forcing rule level to 6.

    This should be helpful keeping module's code properly maintained with the evolution of the different Core versions, APIs and PHP versions.

    PHPSTAN validation is currently not required to merge.

    Marking issue as Fixed for now.

    Thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024