Tools: Add and configure PHPStan static analysis #66693
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
This PR adds a PHPStan configuration along with error baselines though Level 7.
PHPStan errors are not remediated in this PR.
Why?
Mirrors efforts in https://core.trac.wordpress.org/ticket/61175
Error baselines can be used as a list of preexisting tech debt that can be remediated independently of this PR. Baselines go up to PHPStan Level 7, as Level 8 baselines currently conflict with previous levels.
Once a decision is made regarding the PHPStan Level to target, the baselines can be merged/adjusted.
Part of #66598
How?
Run PHPStan
Create a local
phpstan.neon
for remediating errorsphpstan.neon.dist
tophpstan.neon
.parameters.level
to the desired level, and filter out the errors by adding them to theparameters.ignoreErrors
list.composer run analyse
.Remediating errors using the error baselines
While
parameters.ignoreErrors
is used to filter out "unsupported" errors, error baselines are used to suppress preexisting tech debt.This allows for the PR to be merged as is to enforce the rules on new code, while allowing contributors to remediate the existing errors at their own pace. This is in the spirit of 'Avoid unnecessary refactoring'.
To remediate a baselined error, remove the error from the
tests/phpstan/baseline/level-{%N}
file and run PHPStan again.Triaging errors and regenerating baselines
If an error is found to be a false positive (or otherwise not worth fixing), it should be added to the
parameters.ignoreErrors
list in thephpstan.neon.dist
file. When this happens, the baseline file suppressing the error will cause PHPStan to fail.To avoid manual remediation, the baseline files can be regenerated by following the following steps:
Identify the baseline level that contains the error. (Search for the error in
tools/phpstan/baseline
).Change the
parameters.level
inphpstan.neon
to the level identified in step 1.Comment out the
includes
for that level and all levels above it.Run the following command:
# Replace {%N} with the level identified in step 1 composer analyse --generate-baseline tools/phpstan/baseline/level-{%N}.php
Testing Instructions
composer analyse
and confirm PHPStan passes.phpstan.neon.dist
tophpstan.neon
, comment out one of the baselineincludes:
, reruncomposer analyse
and confirm PHPStan fails.Todo
( PRs open. )
gutenberg_*()
functions and*_Gutenberg
classesignoreErrors
candidates.