You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ed3a92f
Browse filesBrowse the repository at this point in the historyBrowse files
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
Copy file name to clipboardExpand all lines: .github/workflows/phpstan-pull-request-comments.yml
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -261,21 +261,21 @@ jobs:
261
261
'> [!IMPORTANT]',
262
262
'> **The PHPStan baselines are out of date, so the PHPStan check fails.**',
263
263
'>',
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:',
265
265
'>',
266
266
'> ```bash',
267
-
'> npm run typecheck:php:baselines',
267
+
'> npm run typecheck:php:fix',
268
268
'> ```',
269
269
'>',
270
270
'> or, outside the Docker environment:',
271
271
'>',
272
272
'> ```bash',
273
-
'> composer phpstan:baselines',
273
+
'> composer phpstan:fix',
274
274
'> ```',
275
275
'>',
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.',
277
277
'>',
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.`,
**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.
268
273
269
-
Regenerate them and commit the result:
274
+
Remove the stale entries, and commit the result:
270
275
271
276
```bash
272
-
npm run typecheck:php:baselines
277
+
npm run typecheck:php:fix
273
278
```
274
279
275
280
or, outside the Docker environment:
276
281
277
282
```bash
278
-
composer phpstan:baselines
283
+
composer phpstan:fix
279
284
```
280
285
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`.
282
287
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.
284
289
285
290
See [`tests/phpstan/README.md`][readme] for details.
286
291
@@ -294,7 +299,7 @@ jobs:
294
299
echo "[readme]: ${README_URL}"
295
300
} >> "${GITHUB_STEP_SUMMARY}"
296
301
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}"
298
303
else
299
304
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."
Copy file name to clipboardExpand all lines: tests/phpstan/README.md
+36-3Lines changed: 36 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,7 @@ The analysis also runs on every pull request that changes PHP files or the PHPSt
50
50
- As annotations on the changed files. Most errors are annotated as warnings, which do not fail the check.
51
51
- 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.
52
52
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.
54
54
55
55
## The PHPStan configuration
56
56
@@ -216,11 +216,40 @@ Every entry is scoped to the file the error occurs in and carries an exact occur
216
216
217
217
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.
218
218
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.
220
249
221
250
### Regenerating the baselines
222
251
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:
224
253
225
254
```bash
226
255
npm run typecheck:php:baselines
@@ -241,6 +270,9 @@ npm run typecheck:php:baselines -- --identifier=isset.variable --identifier=empt
241
270
# print every error as one baseline, writing nothing:
242
271
npm run typecheck:php:baselines -- --combined
243
272
273
+
# without analyzing anything, delete the baselines that list no entries:
0 commit comments