Skip to content

Commit ed3a92f

Browse files
westonruterarshidkv12
authored andcommitted
Build/Test Tools: Add a script to fix stale PHPStan baselines.
Since PHPStan 2.3.5, its `--fix` option removes stale ignores in place: it lowers the count of a baseline entry whose error now occurs fewer times, removes an entry that matches nothing, and removes an `@phpstan-ignore` comment that no longer ignores anything. That is quicker than regenerating the baselines, since it reuses the result cache, and it also clears the stale inline ignores, which regenerating leaves to be removed by hand. The new `composer phpstan:fix` script (and `npm run typecheck:php:fix`) runs PHPStan with `--fix`, then the baseline generator with its new `--prune` option, which deletes any baseline left with no entries along with its `includes` entry, and then the analysis once more, since `--fix` reports only what it fixed and succeeds even when other errors remain. Both analyses use `phpstan.neon.dist`, because a local configuration at another rule level can report a baselined error with a different message, and `--fix` would then remove entries that the default configuration still needs. The CI annotations, step summary, and pull request comment now recommend the new script, and the annotation of an error that PHPStan can fix itself says so. Developed in WordPress#15479. Follow-up to r63019, r64539, r65310. Props westonruter, apermo. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@65372 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9bce2f4 commit ed3a92f

6 files changed

Lines changed: 183 additions & 21 deletions

File tree

‎.github/workflows/phpstan-pull-request-comments.yml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,21 +261,21 @@ jobs:
261261
'> [!IMPORTANT]',
262262
'> **The PHPStan baselines are out of date, so the PHPStan check fails.**',
263263
'>',
264-
'> An error recorded in a baseline no longer occurs, or occurs a different number of times. If this pull request fixed it, that is expected: nothing needs to change in the code, but each baseline entry records an exact count for a specific file, and the baselines need to catch up. Regenerate them and commit the result:',
264+
'> An error recorded in a baseline no longer occurs, or occurs a different number of times. If this pull request fixed it, that is expected: nothing needs to change in the code, but each baseline entry records an exact count for a specific file, and the baselines need to catch up. Remove the stale entries, and commit the result:',
265265
'>',
266266
'> ```bash',
267-
'> npm run typecheck:php:baselines',
267+
'> npm run typecheck:php:fix',
268268
'> ```',
269269
'>',
270270
'> or, outside the Docker environment:',
271271
'>',
272272
'> ```bash',
273-
'> composer phpstan:baselines',
273+
'> composer phpstan:fix',
274274
'> ```',
275275
'>',
276-
'> Where an error below names an `@phpstan-ignore` comment in the code rather than a baseline entry, remove that comment instead; regenerating will not clear it.',
276+
'> This also deletes any baseline left with no entries, and removes any `@phpstan-ignore` comment in the code that no longer ignores anything.',
277277
'>',
278-
`> See [Regenerating the baselines](${ readmeUrl }#regenerating-the-baselines) for details.`,
278+
`> See [Removing stale entries with \`--fix\`](${ readmeUrl }#removing-stale-entries-with---fix) for details, and for regenerating the baselines instead.`,
279279
].join( '\n' ) );
280280
} else {
281281
sections.push( [

‎.github/workflows/reusable-phpstan-static-analysis-v1.yml‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,20 @@ jobs:
150150
identifier: message.identifier ?? null,
151151
tip: message.tip ?? null,
152152
ignorable: message.ignorable,
153+
fixable: message.fixable ?? false,
153154
} );
154155
}
155156
}
156157
157-
// Errors that are not specific to a file, such as an `ignoreErrors` entry without a
158-
// path that no longer matches. These cannot be ignored either.
158+
// Errors that are not specific to a file, such as an `ignoreErrors` entry from a config
159+
// file PHPStan cannot locate it in. These cannot be ignored either.
159160
for ( const message of report.errors ) {
160-
errors.push( { file: null, line: null, message, identifier: null, tip: null, ignorable: false } );
161+
errors.push( { file: null, line: null, message, identifier: null, tip: null, ignorable: false, fixable: false } );
161162
}
162163
164+
// PHPStan marks an error it can fix itself, such as a stale baseline entry, as fixable.
165+
const fixHint = 'This can be fixed automatically by running `composer phpstan:fix` (or `npm run typecheck:php:fix`).';
166+
163167
for ( const error of errors ) {
164168
const properties = { title: error.identifier ? `PHPStan: ${ error.identifier }` : 'PHPStan' };
165169
if ( error.file ) {
@@ -168,15 +172,16 @@ jobs:
168172
if ( error.line ) {
169173
properties.startLine = error.line;
170174
}
171-
const message = error.tip ? `${ error.message }\n${ error.tip }` : error.message;
175+
const message = [ error.message, error.tip, error.fixable ? fixHint : null ].filter( Boolean ).join( '\n' );
172176
if ( error.ignorable ) {
173177
core.warning( message, properties );
174178
} else {
175179
core.error( message, properties );
176180
}
177181
}
178182
179-
// Tips are left out of the results, since they are only shown in the annotations, next to the code.
183+
// Tips and whether an error is fixable are left out of the results, since they are only shown in the
184+
// annotations, next to the code.
180185
//
181186
// The commit analyzed is the pull request merged into its base branch, whose second parent is the head
182187
// commit. The files with errors that differ between the two are those the base branch has changed, in
@@ -196,7 +201,7 @@ jobs:
196201
fs.writeFileSync( `${ process.env.RUNNER_TEMP }/phpstan-results/results.json`, JSON.stringify( {
197202
pull_request: context.payload.pull_request.number,
198203
changed_in_base: changedInBase,
199-
errors: errors.map( ( { tip, ...error } ) => error ),
204+
errors: errors.map( ( { tip, fixable, ...error } ) => error ),
200205
} ) );
201206
}
202207
@@ -266,21 +271,21 @@ jobs:
266271
267272
**If you fixed the error, this is expected.** Each baseline entry records an exact count for a specific file, so that a new occurrence of an already baselined error is reported rather than absorbed. That same exactness means fixing one leaves the baseline describing a state that no longer exists. There is nothing to fix in the code; the baselines just need to catch up.
268273
269-
Regenerate them and commit the result:
274+
Remove the stale entries, and commit the result:
270275
271276
```bash
272-
npm run typecheck:php:baselines
277+
npm run typecheck:php:fix
273278
```
274279
275280
or, outside the Docker environment:
276281
277282
```bash
278-
composer phpstan:baselines
283+
composer phpstan:fix
279284
```
280285
281-
That rewrites the files under [`tests/phpstan/baselines`][baselines], deletes any whose errors are now all fixed, and updates the list of them in `phpstan.neon.dist`.
286+
That runs PHPStan with `--fix`, which lowers the counts in the files under [`tests/phpstan/baselines`][baselines], removes the entries that no longer match, and removes any `@phpstan-ignore` annotation in the code that no longer ignores anything. It then deletes any baseline left with no entries, and updates the list of them in `phpstan.neon.dist`.
282287
283-
Where the report names an `@phpstan-ignore` annotation in the code rather than a baseline entry, remove that annotation instead; regenerating will not clear it.
288+
Alternatively, regenerate the baselines with `npm run typecheck:php:baselines` or `composer phpstan:baselines`. That also deletes any baseline whose errors are now all fixed, and updates the list of them in `phpstan.neon.dist`, but it does not clear an `@phpstan-ignore` annotation.
284289
285290
See [`tests/phpstan/README.md`][readme] for details.
286291
@@ -294,7 +299,7 @@ jobs:
294299
echo "[readme]: ${README_URL}"
295300
} >> "${GITHUB_STEP_SUMMARY}"
296301
297-
echo "::error title=PHPStan baselines are out of date::An ignored error no longer occurs, or occurs a different number of times. If you fixed it, that is expected: run \`npm run typecheck:php:baselines\` or \`composer phpstan:baselines\` and commit the updated baselines. See ${README_URL}"
302+
echo "::error title=PHPStan baselines are out of date::An ignored error no longer occurs, or occurs a different number of times. If you fixed it, that is expected: run \`npm run typecheck:php:fix\` or \`composer phpstan:fix\` and commit the updated baselines. See ${README_URL}"
298303
else
299304
echo "::error title=PHPStan reported errors that cannot be ignored::PHPStan reported ${unignorable} error(s) that cannot be ignored or reported as warnings. See the error annotations for details, and ${README_URL} for how to run the analysis locally."
300305
fi

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"scripts": {
7878
"phpstan": "@php ./vendor/bin/phpstan analyse --memory-limit=2G",
7979
"phpstan:baselines": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/generate-baselines.php" ],
80+
"phpstan:fix": [ "Composer\\Config::disableProcessTimeout", "@phpstan --configuration=phpstan.neon.dist --fix", "@phpstan:baselines --prune", "@phpstan --configuration=phpstan.neon.dist" ],
8081
"compat": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source",
8182
"format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source",
8283
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source",

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
"test:visual": "wp-scripts test-playwright --config tests/visual-regression/playwright.config.js",
155155
"typecheck:php": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan --",
156156
"typecheck:php:baselines": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:baselines --",
157+
"typecheck:php:fix": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:fix --",
157158
"gutenberg:copy": "node tools/gutenberg/copy.js",
158159
"gutenberg:verify": "node tools/gutenberg/utils.js",
159160
"gutenberg:download": "node tools/gutenberg/download.js && npm run build:gutenberg"

‎tests/phpstan/README.md‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The analysis also runs on every pull request that changes PHP files or the PHPSt
5050
- As annotations on the changed files. Most errors are annotated as warnings, which do not fail the check.
5151
- In a comment on the pull request, which lists every error in a table along with the commit it was first reported for. The comment is updated as new commits are pushed, moving each error that a commit resolves into a collapsed list of resolved errors, which also names that commit. Once all of the errors are resolved, the comment is marked as resolved and collapsed.
5252

53-
Some errors cannot be ignored, and so cannot be reported as warnings either. These fail the check. The most common is an [out-of-date baseline](#regenerating-the-baselines), which is reported when an error recorded in a baseline was fixed without the baseline being regenerated. Such an error is reported against a file rather than a line in it, so its annotation does not appear alongside the changes, and it would easily go unnoticed if the check passed.
53+
Some errors cannot be ignored, and so cannot be reported as warnings either. These fail the check. The most common is an [out-of-date baseline](#removing-stale-entries-with---fix), which is reported when an error recorded in a baseline was fixed without the baseline being updated. Such an error is reported against a file rather than a line in it, so its annotation does not appear alongside the changes, and it would easily go unnoticed if the check passed.
5454

5555
## The PHPStan configuration
5656

@@ -216,11 +216,40 @@ Every entry is scoped to the file the error occurs in and carries an exact occur
216216

217217
Both the path and the count matter. A new occurrence of an already baselined error does not match the entry, even in a file that is already listed, and is reported as a new error. That is the point of recording them this way: the baselines describe exactly what exists today, so nothing new slips in behind them.
218218

219-
The consequence is that **fixing a baselined error means regenerating its baseline as part of the same change**, because the count no longer matches. A count that no longer matches is reported as an `ignore.count` error, which PHPStan does not allow to be ignored or baselined.
219+
The consequence is that **fixing a baselined error means updating its baseline as part of the same change**, because the count no longer matches. A count that no longer matches is reported as an `ignore.count` error, and an entry that matches nothing at all as an `ignore.unmatched` error, neither of which PHPStan allows to be ignored or baselined. Either [remove the stale entries with `--fix`](#removing-stale-entries-with---fix), or [regenerate the baselines](#regenerating-the-baselines).
220+
221+
### Removing stale entries with `--fix`
222+
223+
PHPStan marks an error it can fix automatically with 🔧, and ends its report by saying how many there are. Stale ignores are among them: a baseline entry whose count is now too high, or that matches nothing at all, and an `@phpstan-ignore` comment that no longer ignores anything. Fix them in place with:
224+
225+
```bash
226+
npm run typecheck:php:fix
227+
```
228+
229+
or, outside the Docker environment:
230+
231+
```bash
232+
composer phpstan:fix
233+
```
234+
235+
This runs three steps, each only when the one before it succeeds:
236+
237+
1. PHPStan with its `--fix` option, which lowers the count of the entry, or removes the entry, leaving the rest of the baseline as it was. It is usually much faster than regenerating, since it reuses the result cache of the run that reported the errors. Unlike regenerating, it also removes an `@phpstan-ignore` comment that no longer matches.
238+
2. `composer phpstan:baselines -- --prune`, which deletes any baseline left with no entries, along with its `includes` entry in `phpstan.neon.dist`. Removing the last entry of a baseline leaves the file behind, as `ignoreErrors: []`. Pruning analyzes nothing, so it can also be run on its own.
239+
3. The analysis once more, which reuses the result cache and so is quick. PHPStan's `--fix` reports only what it fixed, and succeeds even when other errors remain, so this is what shows the errors that are left.
240+
241+
Keep in mind:
242+
243+
- **It fails when nothing is fixable.** PHPStan then says `No fixable errors found` and stops the script there, so run `composer phpstan` to see whether any errors remain.
244+
- **It always analyzes the whole tree with `phpstan.neon.dist`**, whatever a local `phpstan.neon` says. PHPStan only reports stale ignores when it analyzes every path, and a configuration at another rule level can report a baselined error with a different message, in which case `--fix` would remove an entry that the default configuration still needs.
245+
- **It fixes every fixable error the run reports**, not only stale ignores. A few other errors can also be fixed automatically, such as a [useless cast](https://phpstan.org/error-identifiers/cast.useless), so check the diff before committing it.
246+
- PHPStan's `--fix-ignored`, run as `composer phpstan -- --fix-ignored`, goes further and also fixes the fixable errors which a baseline currently ignores. That is a way to work through a baseline, but expect larger diffs, and follow it with `composer phpstan:baselines -- --prune`.
247+
248+
The feature is marked experimental in PHPStan. See [the PHPStan documentation](https://phpstan.org/user-guide/command-line-usage) for details.
220249

221250
### Regenerating the baselines
222251

223-
The baselines are generated, and should not be edited by hand. Regenerate them with:
252+
The baselines are generated, and should not be edited by hand, other than by `--fix` as described [above](#removing-stale-entries-with---fix). Regenerate them with:
224253

225254
```bash
226255
npm run typecheck:php:baselines
@@ -241,6 +270,9 @@ npm run typecheck:php:baselines -- --identifier=isset.variable --identifier=empt
241270
# print every error as one baseline, writing nothing:
242271
npm run typecheck:php:baselines -- --combined
243272

273+
# without analyzing anything, delete the baselines that list no entries:
274+
npm run typecheck:php:baselines -- --prune
275+
244276
# the remaining options:
245277
npm run typecheck:php:baselines -- --help
246278
```
@@ -252,6 +284,7 @@ composer phpstan:baselines
252284

253285
composer phpstan:baselines -- --identifier=variable.undefined
254286
composer phpstan:baselines -- --combined
287+
composer phpstan:baselines -- --prune
255288
composer phpstan:baselines -- --help
256289
```
257290

0 commit comments

Comments
 (0)