From f17cf7f9b524296ef8fa52d3dfbc79aa0b1fe379 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 18:09:51 -0800 Subject: [PATCH 01/38] Build/Test Tools: Include the PHPStan baselines through a PHP loader. `phpstan.neon.dist` listed every per-identifier baseline in its `includes`, between `# phpstan:baselines` markers, and `generate-baselines.php` rewrote that list whenever a baseline was added or deleted. Every PR that retired a baseline therefore edited the same list, so two such PRs conflicted with each other. The config now includes `tests/phpstan/baselines/loader.php`, a PHP config file that returns every `.neon` file in its directory as its `includes`. NEON can only include files by name, but a PHP config can find them itself. A baseline takes effect when it is added and stops when it is deleted, with no edit of the config. PHPStan keys its config cache on the included files, so the next run sees the change. The resolved `ignoreErrors` are identical to those from the explicit list. The generator no longer maintains the list: the markers, `update_config_includes()` and `find_includes_end()` are removed, and so is the line in each baseline's header about deleting its `includes` entry. While regenerating, it still suppresses the baselines by stripping includes from a temporary copy of the config: the loader itself, and anything else inside the output directory, so `--output-dir` still yields a complete baseline rather than a differential one. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- phpstan.neon.dist | 40 +--- tests/phpstan/README.md | 6 +- tests/phpstan/baselines/argument.type.neon | 3 +- .../baselines/argument.unresolvableType.neon | 3 +- .../baselines/assign.propertyType.neon | 3 +- tests/phpstan/baselines/binaryOp.invalid.neon | 3 +- .../baselines/booleanAnd.leftAlwaysTrue.neon | 3 +- .../baselines/booleanNot.alwaysTrue.neon | 3 +- tests/phpstan/baselines/class.notFound.neon | 3 +- .../baselines/deadCode.unreachable.neon | 3 +- tests/phpstan/baselines/empty.property.neon | 3 +- tests/phpstan/baselines/empty.variable.neon | 3 +- .../function.alreadyNarrowedType.neon | 3 +- .../baselines/function.resultUnused.neon | 3 +- .../baselines/identical.alwaysFalse.neon | 3 +- tests/phpstan/baselines/if.alwaysFalse.neon | 3 +- tests/phpstan/baselines/if.alwaysTrue.neon | 3 +- tests/phpstan/baselines/isset.property.neon | 3 +- tests/phpstan/baselines/loader.php | 20 ++ .../baselines/method.childParameterType.neon | 3 +- tests/phpstan/baselines/method.unused.neon | 3 +- .../baselines/notIdentical.alwaysTrue.neon | 3 +- .../offsetAccess.nonOffsetAccessible.neon | 3 +- .../baselines/offsetAccess.notFound.neon | 3 +- .../baselines/offsetAssign.valueType.neon | 3 +- .../baselines/parameterByRef.type.neon | 3 +- .../baselines/parameterByRef.unusedType.neon | 3 +- .../baselines/property.defaultValue.neon | 3 +- .../phpstan/baselines/property.nonObject.neon | 3 +- .../phpstan/baselines/property.notFound.neon | 3 +- tests/phpstan/baselines/property.private.neon | 3 +- tests/phpstan/baselines/return.type.neon | 3 +- .../phpstan/baselines/varTag.noVariable.neon | 3 +- .../phpstan/baselines/variable.undefined.neon | 3 +- tests/phpstan/generate-baselines.php | 191 ++++-------------- 35 files changed, 93 insertions(+), 257 deletions(-) create mode 100644 tests/phpstan/baselines/loader.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index cbaa19f3e8db1..260afa934953f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -18,44 +18,12 @@ includes: # Preexisting errors that should be ignored, one baseline per error identifier # so that the remaining work on each is visible as a single shrinking file. - # Each is meant to reach zero and be deleted, taking its line below with it. + # Each is meant to reach zero and be deleted. # https://phpstan.org/user-guide/baseline # - # Regenerate with `composer phpstan:baselines`, which rewrites both the files - # and the list between the markers. Do not edit that list by hand. - # phpstan:baselines start - - tests/phpstan/baselines/argument.type.neon - - tests/phpstan/baselines/argument.unresolvableType.neon - - tests/phpstan/baselines/assign.propertyType.neon - - tests/phpstan/baselines/binaryOp.invalid.neon - - tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon - - tests/phpstan/baselines/booleanNot.alwaysTrue.neon - - tests/phpstan/baselines/class.notFound.neon - - tests/phpstan/baselines/deadCode.unreachable.neon - - tests/phpstan/baselines/empty.property.neon - - tests/phpstan/baselines/empty.variable.neon - - tests/phpstan/baselines/function.alreadyNarrowedType.neon - - tests/phpstan/baselines/function.resultUnused.neon - - tests/phpstan/baselines/identical.alwaysFalse.neon - - tests/phpstan/baselines/if.alwaysFalse.neon - - tests/phpstan/baselines/if.alwaysTrue.neon - - tests/phpstan/baselines/isset.property.neon - - tests/phpstan/baselines/method.childParameterType.neon - - tests/phpstan/baselines/method.unused.neon - - tests/phpstan/baselines/notIdentical.alwaysTrue.neon - - tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon - - tests/phpstan/baselines/offsetAccess.notFound.neon - - tests/phpstan/baselines/offsetAssign.valueType.neon - - tests/phpstan/baselines/parameterByRef.type.neon - - tests/phpstan/baselines/parameterByRef.unusedType.neon - - tests/phpstan/baselines/property.defaultValue.neon - - tests/phpstan/baselines/property.nonObject.neon - - tests/phpstan/baselines/property.notFound.neon - - tests/phpstan/baselines/property.private.neon - - tests/phpstan/baselines/return.type.neon - - tests/phpstan/baselines/varTag.noVariable.neon - - tests/phpstan/baselines/variable.undefined.neon - # phpstan:baselines end + # The loader includes every baseline in its directory, so adding or deleting one + # needs no edit here. Regenerate them with `composer phpstan:baselines`. + - tests/phpstan/baselines/loader.php # Rules from phpstan-strict-rules which core already complies with, or which enforce the # WordPress Coding Standards. The others are not enabled until the code complies with them, diff --git a/tests/phpstan/README.md b/tests/phpstan/README.md index 62003ca812d6a..32a06c079460c 100644 --- a/tests/phpstan/README.md +++ b/tests/phpstan/README.md @@ -204,6 +204,8 @@ PHPStan errors can be ignored in the following ways: The baselines live in [`baselines/`](baselines), one file per error identifier, such as `variable.undefined.neon`. Splitting them this way keeps each kind of error visible as a single file that should shrink to nothing and then be deleted, rather than as part of one large file in which every kind is mixed together. +[`phpstan.neon.dist`](../../phpstan.neon.dist) does not list the baselines one by one. It includes [`baselines/loader.php`](baselines/loader.php), which includes every `.neon` file in that directory, so a baseline takes effect as soon as it is added, and stops as soon as it is deleted, without an edit of the configuration. + Every entry is scoped to the file the error occurs in and carries an exact occurrence count: ```neon @@ -235,7 +237,7 @@ composer phpstan:fix This runs three steps, each only when the one before it succeeds: 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. -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. +2. `composer phpstan:baselines -- --prune`, which deletes any baseline left with no entries. 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. 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. Keep in mind: @@ -290,7 +292,7 @@ composer phpstan:baselines -- --help Note the `--` in each of those. Composer needs it in order to pass the flags on to the script rather than reading them as its own, and without it they are discarded silently, so `composer phpstan:baselines --identifier=variable.undefined` regenerates every baseline rather than that one. The npm script supplies it, which is why only one is needed there. -A run also deletes any baseline whose identifier no longer reports anything, and rewrites the list of them between the `# phpstan:baselines` markers in the `includes` of [`phpstan.neon.dist`](../../phpstan.neon.dist) to match. Adding a newly split out baseline, and retiring one that has reached zero, therefore need no edit of the configuration. +A run also deletes any baseline whose identifier no longer reports anything. Since the baselines are included through [`baselines/loader.php`](baselines/loader.php), adding a newly split out baseline, and retiring one that has reached zero, need no edit of the configuration. PHPStan's own `--generate-baseline` is deliberately not used directly. It captures every error a run reports, with no way to restrict it to one identifier, so it cannot refresh a single baseline without sweeping every other kind of error into it. diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index aece929f18751..44c86e3f775f0 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/argument.unresolvableType.neon b/tests/phpstan/baselines/argument.unresolvableType.neon index 3e8cdcb76c85d..d2dbdbf78b558 100644 --- a/tests/phpstan/baselines/argument.unresolvableType.neon +++ b/tests/phpstan/baselines/argument.unresolvableType.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/assign.propertyType.neon b/tests/phpstan/baselines/assign.propertyType.neon index 80b4b8b9b43c1..821971cf7ff0f 100644 --- a/tests/phpstan/baselines/assign.propertyType.neon +++ b/tests/phpstan/baselines/assign.propertyType.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/binaryOp.invalid.neon b/tests/phpstan/baselines/binaryOp.invalid.neon index 167f159cffb34..5361d1c1373d4 100644 --- a/tests/phpstan/baselines/binaryOp.invalid.neon +++ b/tests/phpstan/baselines/binaryOp.invalid.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon b/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon index 10ed28f6b433d..6f08065e4598e 100644 --- a/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon +++ b/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/booleanNot.alwaysTrue.neon b/tests/phpstan/baselines/booleanNot.alwaysTrue.neon index b61a98cef358e..f0596cafb4de5 100644 --- a/tests/phpstan/baselines/booleanNot.alwaysTrue.neon +++ b/tests/phpstan/baselines/booleanNot.alwaysTrue.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/class.notFound.neon b/tests/phpstan/baselines/class.notFound.neon index 560ceafb56573..308b6a3db00bb 100644 --- a/tests/phpstan/baselines/class.notFound.neon +++ b/tests/phpstan/baselines/class.notFound.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/deadCode.unreachable.neon b/tests/phpstan/baselines/deadCode.unreachable.neon index ef8ca56577602..9c8d9780eeddf 100644 --- a/tests/phpstan/baselines/deadCode.unreachable.neon +++ b/tests/phpstan/baselines/deadCode.unreachable.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/empty.property.neon b/tests/phpstan/baselines/empty.property.neon index 5bc6d4e3cbdd3..56c8df0a0f625 100644 --- a/tests/phpstan/baselines/empty.property.neon +++ b/tests/phpstan/baselines/empty.property.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/empty.variable.neon b/tests/phpstan/baselines/empty.variable.neon index c9a94ecd02d9f..37a3c9fda14fd 100644 --- a/tests/phpstan/baselines/empty.variable.neon +++ b/tests/phpstan/baselines/empty.variable.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/function.alreadyNarrowedType.neon b/tests/phpstan/baselines/function.alreadyNarrowedType.neon index a7c36b7d7d0b8..ed05ca3edde93 100644 --- a/tests/phpstan/baselines/function.alreadyNarrowedType.neon +++ b/tests/phpstan/baselines/function.alreadyNarrowedType.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/function.resultUnused.neon b/tests/phpstan/baselines/function.resultUnused.neon index 1ad6a84dbfa4c..8084a056a9529 100644 --- a/tests/phpstan/baselines/function.resultUnused.neon +++ b/tests/phpstan/baselines/function.resultUnused.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/identical.alwaysFalse.neon b/tests/phpstan/baselines/identical.alwaysFalse.neon index d8d766d45827f..c52fec18de570 100644 --- a/tests/phpstan/baselines/identical.alwaysFalse.neon +++ b/tests/phpstan/baselines/identical.alwaysFalse.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/if.alwaysFalse.neon b/tests/phpstan/baselines/if.alwaysFalse.neon index 15f01b77b4cf4..489ab252ce8e9 100644 --- a/tests/phpstan/baselines/if.alwaysFalse.neon +++ b/tests/phpstan/baselines/if.alwaysFalse.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/if.alwaysTrue.neon b/tests/phpstan/baselines/if.alwaysTrue.neon index 6f5169ee0b1e6..457b233a9bbd0 100644 --- a/tests/phpstan/baselines/if.alwaysTrue.neon +++ b/tests/phpstan/baselines/if.alwaysTrue.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/isset.property.neon b/tests/phpstan/baselines/isset.property.neon index 5278005df2869..aed467fb1e958 100644 --- a/tests/phpstan/baselines/isset.property.neon +++ b/tests/phpstan/baselines/isset.property.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php new file mode 100644 index 0000000000000..a91e334ed0e5f --- /dev/null +++ b/tests/phpstan/baselines/loader.php @@ -0,0 +1,20 @@ + false === $baselines ? array() : $baselines, +); diff --git a/tests/phpstan/baselines/method.childParameterType.neon b/tests/phpstan/baselines/method.childParameterType.neon index 3d6cfe40fdb08..67b5e9342f9c6 100644 --- a/tests/phpstan/baselines/method.childParameterType.neon +++ b/tests/phpstan/baselines/method.childParameterType.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/method.unused.neon b/tests/phpstan/baselines/method.unused.neon index bf95fbb71715c..220d3bdd22908 100644 --- a/tests/phpstan/baselines/method.unused.neon +++ b/tests/phpstan/baselines/method.unused.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/notIdentical.alwaysTrue.neon b/tests/phpstan/baselines/notIdentical.alwaysTrue.neon index f4daf4d4ebc01..a8c4874b90177 100644 --- a/tests/phpstan/baselines/notIdentical.alwaysTrue.neon +++ b/tests/phpstan/baselines/notIdentical.alwaysTrue.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon b/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon index 9179b5365005f..8a50c32209afb 100644 --- a/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon +++ b/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/offsetAccess.notFound.neon b/tests/phpstan/baselines/offsetAccess.notFound.neon index a27d5cc14f437..61ed8d3d01353 100644 --- a/tests/phpstan/baselines/offsetAccess.notFound.neon +++ b/tests/phpstan/baselines/offsetAccess.notFound.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/offsetAssign.valueType.neon b/tests/phpstan/baselines/offsetAssign.valueType.neon index fba6c3ffcd663..82a2c14537d91 100644 --- a/tests/phpstan/baselines/offsetAssign.valueType.neon +++ b/tests/phpstan/baselines/offsetAssign.valueType.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/parameterByRef.type.neon b/tests/phpstan/baselines/parameterByRef.type.neon index 8b7394add3c7e..c14b642e8556c 100644 --- a/tests/phpstan/baselines/parameterByRef.type.neon +++ b/tests/phpstan/baselines/parameterByRef.type.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/parameterByRef.unusedType.neon b/tests/phpstan/baselines/parameterByRef.unusedType.neon index 73746ef20c078..8a77b0a01e582 100644 --- a/tests/phpstan/baselines/parameterByRef.unusedType.neon +++ b/tests/phpstan/baselines/parameterByRef.unusedType.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/property.defaultValue.neon b/tests/phpstan/baselines/property.defaultValue.neon index e604833eade20..79dcee9573015 100644 --- a/tests/phpstan/baselines/property.defaultValue.neon +++ b/tests/phpstan/baselines/property.defaultValue.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/property.nonObject.neon b/tests/phpstan/baselines/property.nonObject.neon index e17cdfa2c2458..90148e9eaa731 100644 --- a/tests/phpstan/baselines/property.nonObject.neon +++ b/tests/phpstan/baselines/property.nonObject.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/property.notFound.neon b/tests/phpstan/baselines/property.notFound.neon index 425d00444ca8e..f8e4ef760b6c7 100644 --- a/tests/phpstan/baselines/property.notFound.neon +++ b/tests/phpstan/baselines/property.notFound.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/property.private.neon b/tests/phpstan/baselines/property.private.neon index 7549ab9c5ddd5..e4aa11d30d16c 100644 --- a/tests/phpstan/baselines/property.private.neon +++ b/tests/phpstan/baselines/property.private.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/return.type.neon b/tests/phpstan/baselines/return.type.neon index a395fbd5ca63a..9e8bd928c43ff 100644 --- a/tests/phpstan/baselines/return.type.neon +++ b/tests/phpstan/baselines/return.type.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/varTag.noVariable.neon b/tests/phpstan/baselines/varTag.noVariable.neon index 36e0f9fed1382..30e819cb96cd8 100644 --- a/tests/phpstan/baselines/varTag.noVariable.neon +++ b/tests/phpstan/baselines/varTag.noVariable.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/baselines/variable.undefined.neon b/tests/phpstan/baselines/variable.undefined.neon index 94ac3791e4bed..e0b9f272fb643 100644 --- a/tests/phpstan/baselines/variable.undefined.neon +++ b/tests/phpstan/baselines/variable.undefined.neon @@ -7,8 +7,7 @@ # silently. Fixing an occurrence therefore means decrementing or removing its # entry here as part of the same change. # -# The goal is to empty this file and delete it, along with the `includes` entry -# for it in phpstan.neon.dist. +# The goal is to empty this file and delete it. # # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with # diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 7f28af6bcdc47..59823484db43f 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -12,17 +12,15 @@ * identifier and writes one file per kind. Each file is self-describing and is * meant to shrink to nothing and then be deleted. * - * Baselines whose identifier no longer reports anything are removed, and the - * list of them between the `# phpstan:baselines` markers in the configuration's - * `includes` is rewritten to match what is on disk. Adding a newly split out - * baseline, and retiring one that has reached zero, therefore need no manual - * edit of the configuration. + * Baselines whose identifier no longer reports anything are removed. The + * configuration includes them through `tests/phpstan/baselines/loader.php`, which + * includes every baseline in that directory, so adding a newly split out baseline, + * and retiring one that has reached zero, need no edit of the configuration. * * With `--prune`, nothing is analyzed: the baselines that no longer list any - * entries are deleted, and the list in the configuration is updated to match. That - * completes running PHPStan with `--fix`, which removes the entries of fixed - * errors in place but leaves a baseline it has emptied behind, and so - * `composer phpstan:fix` runs it with `--prune` after `--fix`. + * entries are deleted. That completes running PHPStan with `--fix`, which removes + * the entries of fixed errors in place but leaves a baseline it has emptied + * behind, and so `composer phpstan:fix` runs it with `--prune` after `--fix`. * * The intermediate baseline is generated in PHPStan's PHP format and read back * with `require`, so the entries arrive as an array. Nothing has to parse, or @@ -43,17 +41,12 @@ namespace WordPress\PHPStan; /** - * Opening marker of the region of the configuration's `includes` this script owns. + * The file that includes every baseline in its directory. * - * Everything between this and the closing marker is generated: it is rewritten to - * match the baselines on disk, and suppressed while the analysis runs. + * Relative to the repository root. The configuration includes the baselines + * through it, and it is suppressed while the analysis runs. */ -const BASELINES_START_MARKER = '# phpstan:baselines start'; - -/** - * Closing marker of the region of the configuration's `includes` this script owns. - */ -const BASELINES_END_MARKER = '# phpstan:baselines end'; +const BASELINES_LOADER = 'tests/phpstan/baselines/loader.php'; /** * The configuration analyzed when `--config` does not name one. @@ -143,8 +136,8 @@ * entries whose errors were fixed and removes those that no longer match, but when * it removes the last entry of a baseline it leaves the file behind, as * `ignoreErrors: []`. That file suppresses nothing, so all that remains is to - * delete it and drop it from the configuration's `includes`, which is known from - * the file alone. + * delete it, which is known from the file alone. The loader stops including it + * once it is gone. */ if ( $prune ) { if ( $combined ) { @@ -185,7 +178,6 @@ echo "No empty baselines found.\n"; } - update_config_includes( $config_path, $config_option, $output_dir ); exit( 0 ); } @@ -218,7 +210,7 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { } ); -file_put_contents( $temp_stripped, strip_baseline_includes( $config_path, $output_dir ) ); +file_put_contents( $temp_stripped, strip_baseline_includes( $config_path, $output_dir, $repo_root . '/' . BASELINES_LOADER ) ); /* * The analysis gets a scratch directory of its own, rather than the `tmpDir` that @@ -336,7 +328,7 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { foreach ( $grouped as $identifier => $entries ) { file_put_contents( $output_dir . '/' . $identifier . '.neon', - build_baseline( $entries, $output_dir, build_baseline_header( $identifier, $config_option ) ) + build_baseline( $entries, $output_dir, build_baseline_header( $identifier ) ) ); printf( @@ -377,8 +369,6 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { } } -update_config_includes( $config_path, $config_option, $output_dir ); - /** * Returns the usage message. * @@ -388,10 +378,10 @@ function get_usage(): string { return <<<'TEXT' Generates PHPStan baselines split by error identifier. - Writes one baseline per identifier, retires any whose identifier no longer - reports anything, and rewrites the list of them between the - `# phpstan:baselines` markers in the configuration's `includes`, so that - neither addition nor removal has to be done by hand. + Writes one baseline per identifier, and retires any whose identifier no + longer reports anything. The configuration includes every baseline through + tests/phpstan/baselines/loader.php, so neither addition nor removal needs an + edit of it. Usage: composer phpstan:baselines [-- ] @@ -412,8 +402,7 @@ function get_usage(): string { per-identifier files. Nothing is written to disk. --prune Without analyzing anything, delete the baselines that no longer list any entries, such as one emptied by running - PHPStan with --fix, and update the configuration's - `includes` to match. With --identifier, only those + PHPStan with --fix. With --identifier, only those baselines are considered. --memory-limit= Passed through to PHPStan. Default: 2G -h, --help Show this message. @@ -521,11 +510,10 @@ function read_baseline( string $path ): array { * Those files suppress the very errors being regenerated, so they have to be out * of the way for the analysis to report anything. * - * Two kinds of include are dropped. Everything between the `# phpstan:baselines` - * markers goes, whatever it points at, because that region is generated and every - * baseline in force is listed there. Any other include resolving inside the output - * directory goes as well, so that a baseline listed by hand outside the markers is - * still suppressed when it is about to be rewritten. + * Two kinds of include are dropped. The loader goes, because every baseline in + * force is included through it. Any other include resolving inside the output + * directory goes as well, so that a baseline included by hand rather than through + * the loader is still suppressed when it is about to be rewritten. * * The distinction matters to `--output-dir`. Were only the output directory * considered, writing somewhere other than the default would leave the baselines @@ -534,12 +522,12 @@ function read_baseline( string $path ): array { * * @param non-falsy-string $config_path Absolute path to the configuration file. * @param non-falsy-string $output_dir Absolute path to the baseline directory. + * @param non-falsy-string $loader_path Absolute path to the loader of the baselines. * @return string Configuration contents. */ -function strip_baseline_includes( string $config_path, string $output_dir ): string { +function strip_baseline_includes( string $config_path, string $output_dir, string $loader_path ): string { $config_dir = dirname( $config_path ); $in_block = false; - $in_managed = false; $kept = array(); foreach ( explode( "\n", read_file( $config_path ) ) as $line ) { @@ -551,27 +539,18 @@ function strip_baseline_includes( string $config_path, string $output_dir ): str // A non-indented, non-blank line ends the block. if ( $in_block && '' !== trim( $line ) && 1 !== preg_match( '/^\s/', $line ) ) { - $in_block = false; - $in_managed = false; - } - - if ( $in_block ) { - if ( BASELINES_START_MARKER === trim( $line ) ) { - $in_managed = true; - } elseif ( BASELINES_END_MARKER === trim( $line ) ) { - $in_managed = false; - } + $in_block = false; } if ( $in_block && 1 === preg_match( '/^\s*-\s*(\S+)\s*$/', $line, $matches ) ) { - if ( $in_managed ) { + $included = $matches[1]; + $absolute = normalize_path( ( '/' === $included[0] ) ? $included : $config_dir . '/' . $included ); + + if ( normalize_path( $loader_path ) === $absolute ) { continue; } - $included = $matches[1]; - $absolute = ( '/' === $included[0] ) ? $included : $config_dir . '/' . $included; - - if ( 0 === strpos( normalize_path( $absolute ), normalize_path( $output_dir ) . '/' ) ) { + if ( 0 === strpos( $absolute, normalize_path( $output_dir ) . '/' ) ) { continue; } } @@ -654,106 +633,6 @@ function is_empty_baseline( string $contents ): ?bool { return null; } -/** - * Rewrites the managed region of the configuration's `includes` list. - * - * The region is delimited by marker comments, so the hand written entries around - * it are never touched. Where the markers are absent they are appended to the end - * of the `includes` block, which is what happens the first time this is run - * against a configuration. - * - * @param non-falsy-string $config_path Absolute path to the configuration file. - * @param non-empty-string $config_option Configuration path, as passed on the command line. - * @param non-empty-string $output_dir Absolute path to the baseline directory. - */ -function update_config_includes( string $config_path, string $config_option, string $output_dir ): void { - $start_marker = BASELINES_START_MARKER; - $end_marker = BASELINES_END_MARKER; - - $before = read_file( $config_path ); - $lines = explode( "\n", $before ); - - $start = null; - $end = null; - foreach ( $lines as $i => $line ) { - if ( $start_marker === trim( $line ) ) { - $start = $i; - } - if ( $end_marker === trim( $line ) ) { - $end = $i; - } - } - - $region = array( "\t" . $start_marker ); - foreach ( find_baselines( $output_dir ) as $file ) { - $region[] = "\t- " . get_relative_path( dirname( $config_path ), $file ); - } - $region[] = "\t" . $end_marker; - - if ( null !== $start && null !== $end && $start < $end ) { - $updated = array_merge( - array_slice( $lines, 0, $start ), - $region, - array_slice( $lines, $end + 1 ) - ); - } else { - $insert = find_includes_end( $lines ); - - if ( null === $insert ) { - fwrite( STDERR, "No `includes` block found in $config_option, left untouched.\n" ); - return; - } - - $updated = array_merge( - array_slice( $lines, 0, $insert ), - array( '' ), - $region, - array_slice( $lines, $insert ) - ); - } - - $after = implode( "\n", $updated ); - - if ( $before === $after ) { - return; - } - - file_put_contents( $config_path, $after ); - printf( "%s: `includes` updated.\n", $config_option ); -} - -/** - * Finds where the `includes` block ends. - * - * @param list $lines Configuration lines. - * @return int|null Index of the first line after the block, or null when there is none. - */ -function find_includes_end( array $lines ): ?int { - $in_block = false; - $last = null; - - foreach ( $lines as $i => $line ) { - if ( 1 === preg_match( '/^includes:/', $line ) ) { - $in_block = true; - $last = $i; - continue; - } - - if ( ! $in_block || '' === trim( $line ) ) { - continue; - } - - // A non-indented line ends the block. - if ( 1 !== preg_match( '/^\s/', $line ) ) { - break; - } - - $last = $i; - } - - return null === $last ? null : $last + 1; -} - /** * Resolves ".." segments in a path without requiring it to exist. * @@ -861,10 +740,9 @@ function quote_neon_value( string $value ): string { * Builds the header comment for a per-identifier baseline. * * @param non-falsy-string $identifier Error identifier, a group followed by a code. - * @param non-empty-string $config Configuration path, as passed on the command line. * @return non-falsy-string Comment block. */ -function build_baseline_header( string $identifier, string $config ): string { +function build_baseline_header( string $identifier ): string { return << Date: Mon, 16 Nov 2026 18:36:39 -0800 Subject: [PATCH 02/38] Build/Test Tools: Suppress the PHPStan baselines through the loader. The generator suppressed the baselines while regenerating them by dropping the loader's line from a copy of the config. That only worked for one spelling of one line in the top-level config: * A quoted path, a trailing comment, or a `%rootDir%` path left the loader in place. * `--config=phpstan.neon`, with that file including `phpstan.neon.dist` as the README suggests, left it in place too, since only the top-level config is rewritten. Either way every baseline stayed active, so every identifier fully covered by one reported nothing, and the run deleted those baselines. The loader now includes nothing when WP_PHPSTAN_GENERATING_BASELINES is set, and the generator sets it for the PHPStan process. This works however and wherever the loader is included, and the separate `BASELINES_LOADER` path, which repeated the default output directory, is no longer needed. Includes inside the output directory are still dropped from the copy, for a loader written to a directory other than the default. The docblock of `strip_baseline_includes()` no longer describes a baseline included by hand alongside the loader. With the default output directory that includes the file twice, which PHPStan rejects. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- tests/phpstan/baselines/loader.php | 10 ++++++++ tests/phpstan/generate-baselines.php | 38 ++++++++++++++-------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php index a91e334ed0e5f..453ded1612974 100644 --- a/tests/phpstan/baselines/loader.php +++ b/tests/phpstan/baselines/loader.php @@ -10,9 +10,19 @@ * PHPStan keys its cache of the configuration on the files it includes, so a * baseline added or deleted here takes effect on the next run. * + * `composer phpstan:baselines` sets WP_PHPSTAN_GENERATING_BASELINES while it + * analyzes, so that the errors the baselines suppress are reported again for it + * to write back out. Checking it here suppresses them however the configuration + * includes this file, even through another configuration that includes + * `phpstan.neon.dist`. + * * @package WordPress */ +if ( false !== getenv( 'WP_PHPSTAN_GENERATING_BASELINES' ) ) { + return array( 'includes' => array() ); +} + $baselines = glob( __DIR__ . '/*.neon' ); return array( diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 59823484db43f..d1d64645071d2 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -41,12 +41,13 @@ namespace WordPress\PHPStan; /** - * The file that includes every baseline in its directory. + * Environment variable that tells `baselines/loader.php` to include nothing. * - * Relative to the repository root. The configuration includes the baselines - * through it, and it is suppressed while the analysis runs. + * It is set while the analysis runs, so that the baselines in force do not + * suppress the errors being regenerated. The loader checks it itself, so this + * works however the configuration includes the loader. */ -const BASELINES_LOADER = 'tests/phpstan/baselines/loader.php'; +const GENERATING_BASELINES_ENV = 'WP_PHPSTAN_GENERATING_BASELINES'; /** * The configuration analyzed when `--config` does not name one. @@ -210,7 +211,7 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { } ); -file_put_contents( $temp_stripped, strip_baseline_includes( $config_path, $output_dir, $repo_root . '/' . BASELINES_LOADER ) ); +file_put_contents( $temp_stripped, strip_baseline_includes( $config_path, $output_dir ) ); /* * The analysis gets a scratch directory of its own, rather than the `tmpDir` that @@ -287,6 +288,9 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { fwrite( STDERR, "Analyzing with $config_option, existing baselines suppressed...\n" ); +// PHPStan inherits it, as do the worker processes it starts. +putenv( GENERATING_BASELINES_ENV . '=1' ); + $exit_code = 0; passthru( $command, $exit_code ); @@ -510,22 +514,22 @@ function read_baseline( string $path ): array { * Those files suppress the very errors being regenerated, so they have to be out * of the way for the analysis to report anything. * - * Two kinds of include are dropped. The loader goes, because every baseline in - * force is included through it. Any other include resolving inside the output - * directory goes as well, so that a baseline included by hand rather than through - * the loader is still suppressed when it is about to be rewritten. + * The baselines included through a loader are already out of the way, since the + * loader includes nothing while GENERATING_BASELINES_ENV is set. What is dropped + * here is any include in the configuration that resolves inside the output + * directory, such as a loader written there for a directory other than the + * default, so that the baselines about to be rewritten are suppressed too. * - * The distinction matters to `--output-dir`. Were only the output directory - * considered, writing somewhere other than the default would leave the baselines - * already in force active, and the analysis would report just the errors they do - * not cover: a differential baseline rather than a complete one. + * Both are needed for `--output-dir`. Were only the output directory considered, + * writing somewhere other than the default would leave the baselines already in + * force active, and the analysis would report just the errors they do not cover: + * a differential baseline rather than a complete one. * * @param non-falsy-string $config_path Absolute path to the configuration file. * @param non-falsy-string $output_dir Absolute path to the baseline directory. - * @param non-falsy-string $loader_path Absolute path to the loader of the baselines. * @return string Configuration contents. */ -function strip_baseline_includes( string $config_path, string $output_dir, string $loader_path ): string { +function strip_baseline_includes( string $config_path, string $output_dir ): string { $config_dir = dirname( $config_path ); $in_block = false; $kept = array(); @@ -546,10 +550,6 @@ function strip_baseline_includes( string $config_path, string $output_dir, strin $included = $matches[1]; $absolute = normalize_path( ( '/' === $included[0] ) ? $included : $config_dir . '/' . $included ); - if ( normalize_path( $loader_path ) === $absolute ) { - continue; - } - if ( 0 === strpos( $absolute, normalize_path( $output_dir ) . '/' ) ) { continue; } From 5dcf22c6f72147ee6b027b0f7d25470eeb10d35c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 18:36:53 -0800 Subject: [PATCH 03/38] Build/Test Tools: Normalize the output directory once when stripping includes. `strip_baseline_includes()` normalized the output directory again for every line of the config's `includes`, though it never changes. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- tests/phpstan/generate-baselines.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index d1d64645071d2..1e5fbd2b5c90a 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -530,9 +530,10 @@ function read_baseline( string $path ): array { * @return string Configuration contents. */ function strip_baseline_includes( string $config_path, string $output_dir ): string { - $config_dir = dirname( $config_path ); - $in_block = false; - $kept = array(); + $config_dir = dirname( $config_path ); + $output_prefix = normalize_path( $output_dir ) . '/'; + $in_block = false; + $kept = array(); foreach ( explode( "\n", read_file( $config_path ) ) as $line ) { if ( 1 === preg_match( '/^includes:/', $line ) ) { @@ -550,7 +551,7 @@ function strip_baseline_includes( string $config_path, string $output_dir ): str $included = $matches[1]; $absolute = normalize_path( ( '/' === $included[0] ) ? $included : $config_dir . '/' . $included ); - if ( 0 === strpos( $absolute, normalize_path( $output_dir ) . '/' ) ) { + if ( 0 === strpos( $absolute, $output_prefix ) ) { continue; } } From b2e1a4efa2c6436c5a0040213c1aa56ea5fbcd65 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 18:37:15 -0800 Subject: [PATCH 04/38] Build/Test Tools: Give a PHPStan baselines directory its own loader. With the marker list gone, nothing included the baselines written by `composer phpstan:baselines -- --output-dir=`. The default loader only includes its own directory, so they were written and then ignored, with no hint why. When the output directory has no `loader.php`, the generator now copies the default one there and says that the config has to include it. A later run against that directory finds its loader inside the output directory, which is dropped from the config while it regenerates. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- tests/phpstan/generate-baselines.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 1e5fbd2b5c90a..43e4084b6dc6f 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -329,6 +329,23 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { exit( 1 ); } +/* + * The baselines only take effect through a loader in their own directory, which + * the default one already has. A directory given with `--output-dir` gets a copy, + * but nothing includes it yet, so say so. + */ +if ( ! is_file( $output_dir . '/loader.php' ) ) { + if ( ! copy( __DIR__ . '/baselines/loader.php', $output_dir . '/loader.php' ) ) { + fwrite( STDERR, "Could not write $output_option/loader.php\n" ); + exit( 1 ); + } + + printf( + "%s: written. Include it in the configuration for these baselines to take effect.\n", + $output_option . '/loader.php' + ); +} + foreach ( $grouped as $identifier => $entries ) { file_put_contents( $output_dir . '/' . $identifier . '.neon', @@ -400,7 +417,9 @@ function get_usage(): string { root. Default: phpstan.neon.dist --output-dir= Where the per-identifier baselines are written, relative to the repository root. Paths inside them are written - relative to this directory. + relative to this directory. A copy of the loader is + written there if it has none, which the configuration + then has to include. Default: tests/phpstan/baselines --combined Print one combined baseline to stdout instead of writing per-identifier files. Nothing is written to disk. From 77856d1fe50374260d6075b210e707dd88cb3a93 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 18:37:45 -0800 Subject: [PATCH 05/38] Build/Test Tools: Only load PHPStan baselines named for an identifier. The loader included every `.neon` file in its directory, so a stray one, such as `argument.type_BACKUP_1234.neon` left by a merge tool, was loaded along with the real baseline. Its entries then matched nothing, failing the analysis with ignore.unmatched errors that cannot be ignored. Before the loader, only the files listed by name were loaded. The loader now only includes files whose name is an error identifier, matched with the pattern PHPStan validates identifiers with, followed by `.neon`. Every baseline the generator writes is named that way. `find_baselines()` matches the same names, so a stray file is not retired or pruned either. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- tests/phpstan/baselines/loader.php | 20 ++++++++++++++++++-- tests/phpstan/generate-baselines.php | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php index 453ded1612974..02873d61c6800 100644 --- a/tests/phpstan/baselines/loader.php +++ b/tests/phpstan/baselines/loader.php @@ -23,8 +23,24 @@ return array( 'includes' => array() ); } -$baselines = glob( __DIR__ . '/*.neon' ); +$found = glob( __DIR__ . '/*.neon' ); +if ( false === $found ) { + $found = array(); +} + +/* + * Only a file named for an error identifier is a baseline, matched with the + * pattern PHPStan validates identifiers with, so that a stray file such as a + * merge tool's `argument.type_BACKUP_1234.neon` is not loaded along with the + * real one. find_baselines() in `generate-baselines.php` matches the same names. + */ +$baselines = array(); +foreach ( $found as $file ) { + if ( 1 === preg_match( '/^[a-zA-Z0-9](?:[a-zA-Z0-9.]*[a-zA-Z0-9])?\.neon$/', basename( $file ) ) ) { + $baselines[] = $file; + } +} return array( - 'includes' => false === $baselines ? array() : $baselines, + 'includes' => $baselines, ); diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 43e4084b6dc6f..e7c2d4bd09082 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -49,6 +49,15 @@ */ const GENERATING_BASELINES_ENV = 'WP_PHPSTAN_GENERATING_BASELINES'; +/** + * Pattern matching the file name of a baseline. + * + * The name is the error identifier, matched as PHPStan validates identifiers, + * followed by `.neon`. `baselines/loader.php` matches the same names, and the two + * have to stay in sync. + */ +const BASELINE_FILE_PATTERN = '/^[a-zA-Z0-9](?:[a-zA-Z0-9.]*[a-zA-Z0-9])?\.neon$/'; + /** * The configuration analyzed when `--config` does not name one. * @@ -596,9 +605,14 @@ function find_baselines( string $output_dir ): array { sort( $found ); + /* + * Only a file named for an error identifier is a baseline. The loader matches + * the same names, so a stray file such as a merge tool's backup is neither + * loaded nor retired here. + */ $files = array(); foreach ( $found as $file ) { - if ( '' !== $file ) { + if ( '' !== $file && 1 === preg_match( BASELINE_FILE_PATTERN, basename( $file ) ) ) { $files[] = $file; } } From 4d365cac7134b92e8b675aff6c6799647c3c5efb Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 20:05:47 -0800 Subject: [PATCH 06/38] Build/Test Tools: Stop stripping baseline includes from the PHPStan configuration. Every loader includes nothing while WP_PHPSTAN_GENERATING_BASELINES is set, including the copy written to a directory given with `--output-dir`, so the baselines are already out of the way during the analysis. What `strip_baseline_includes()` still removed was a baseline listed by name inside the output directory, which in the default directory PHPStan rejects as a duplicate of the loader's include, and elsewhere is no longer a supported setup. The wrapper setting `tmpDir` now includes the configuration directly, so the stripped copy of it, and the function producing it, are removed. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- tests/phpstan/generate-baselines.php | 80 +++++----------------------- 1 file changed, 12 insertions(+), 68 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index e7c2d4bd09082..9527fecfb022e 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -192,12 +192,11 @@ } /* - * All three temporary files sit beside the configuration, and so inside the + * Both temporary files sit beside the configuration, and so inside the * repository, for two separate reasons. * - * A neon file's `includes` resolve relative to its own directory, so the copy of - * the configuration, and the wrapper including it, have to live where the - * original did. + * A neon file's `includes` resolve relative to its own directory, so the wrapper + * including the configuration has to live where the configuration does. * * PHPStan writes a PHP baseline's paths as __DIR__ followed by a relative chain, * which it can only produce when the baseline shares an ancestry with the files @@ -207,12 +206,11 @@ */ $temp_prefix = dirname( $config_path ) . '/.phpstan-baselines-' . getmypid(); $temp_config = $temp_prefix . '.neon'; -$temp_stripped = $temp_prefix . '-stripped.neon'; $temp_baseline = $temp_prefix . '.php'; register_shutdown_function( - static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { - foreach ( array( $temp_config, $temp_stripped, $temp_baseline ) as $file ) { + static function () use ( $temp_config, $temp_baseline ): void { + foreach ( array( $temp_config, $temp_baseline ) as $file ) { if ( is_file( $file ) ) { unlink( $file ); } @@ -220,8 +218,6 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { } ); -file_put_contents( $temp_stripped, strip_baseline_includes( $config_path, $output_dir ) ); - /* * The analysis gets a scratch directory of its own, rather than the `tmpDir` that * `tests/phpstan/base.neon` configures for everything else. @@ -262,11 +258,13 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { } /* - * `tmpDir` is set in a wrapper including the stripped copy, rather than in the - * copy itself, because neon rejects a duplicated key and the copy already carries - * the `parameters` section it was made from. A file's own parameters win over - * those of the files it includes, so the wrapper's `tmpDir` is the one that takes + * `tmpDir` is set in a wrapper including the configuration, so that the + * configuration itself is left untouched. A file's own parameters win over those + * of the files it includes, so the wrapper's `tmpDir` is the one that takes * effect. + * + * The baselines need no removing from the configuration: GENERATING_BASELINES_ENV + * is set for the analysis, and every loader includes nothing while it is. */ file_put_contents( $temp_config, @@ -277,7 +275,7 @@ static function () use ( $temp_config, $temp_stripped, $temp_baseline ): void { parameters: tmpDir: %s NEON, - basename( $temp_stripped ), + basename( $config_path ), quote_neon_value( $analysis_tmp_dir ) ) ); @@ -536,60 +534,6 @@ function read_baseline( string $path ): array { return $entries; } -/** - * Returns the configuration with the baseline `includes` removed. - * - * Those files suppress the very errors being regenerated, so they have to be out - * of the way for the analysis to report anything. - * - * The baselines included through a loader are already out of the way, since the - * loader includes nothing while GENERATING_BASELINES_ENV is set. What is dropped - * here is any include in the configuration that resolves inside the output - * directory, such as a loader written there for a directory other than the - * default, so that the baselines about to be rewritten are suppressed too. - * - * Both are needed for `--output-dir`. Were only the output directory considered, - * writing somewhere other than the default would leave the baselines already in - * force active, and the analysis would report just the errors they do not cover: - * a differential baseline rather than a complete one. - * - * @param non-falsy-string $config_path Absolute path to the configuration file. - * @param non-falsy-string $output_dir Absolute path to the baseline directory. - * @return string Configuration contents. - */ -function strip_baseline_includes( string $config_path, string $output_dir ): string { - $config_dir = dirname( $config_path ); - $output_prefix = normalize_path( $output_dir ) . '/'; - $in_block = false; - $kept = array(); - - foreach ( explode( "\n", read_file( $config_path ) ) as $line ) { - if ( 1 === preg_match( '/^includes:/', $line ) ) { - $in_block = true; - $kept[] = $line; - continue; - } - - // A non-indented, non-blank line ends the block. - if ( $in_block && '' !== trim( $line ) && 1 !== preg_match( '/^\s/', $line ) ) { - $in_block = false; - } - - if ( $in_block && 1 === preg_match( '/^\s*-\s*(\S+)\s*$/', $line, $matches ) ) { - $included = $matches[1]; - $absolute = normalize_path( ( '/' === $included[0] ) ? $included : $config_dir . '/' . $included ); - - if ( 0 === strpos( $absolute, $output_prefix ) ) { - continue; - } - } - - $kept[] = $line; - } - - return implode( "\n", $kept ); -} - /** * Lists the per-identifier baselines present on disk. * From 6d34b30a48a82287a6f36512e7d3bebc97f709ee Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 23:27:17 -0800 Subject: [PATCH 07/38] Build/Test Tools: Update docs for the PHPStan baselines loader. The message shown when the PHPStan workflow fails still said that `composer phpstan:fix` and `composer phpstan:baselines` update the list of baselines in `phpstan.neon.dist`. There is no such list any more, since the loader finds the baselines itself. The README said the loader includes every `.neon` file in its directory, but it only includes those named for an error identifier. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- .github/workflows/reusable-phpstan-static-analysis-v1.yml | 4 ++-- tests/phpstan/README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/reusable-phpstan-static-analysis-v1.yml b/.github/workflows/reusable-phpstan-static-analysis-v1.yml index 9a80af539ae13..5474a810164cd 100644 --- a/.github/workflows/reusable-phpstan-static-analysis-v1.yml +++ b/.github/workflows/reusable-phpstan-static-analysis-v1.yml @@ -283,9 +283,9 @@ jobs: composer phpstan:fix ``` - 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`. + 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. - 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. + 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, but it does not clear an `@phpstan-ignore` annotation. See [`tests/phpstan/README.md`][readme] for details. diff --git a/tests/phpstan/README.md b/tests/phpstan/README.md index 32a06c079460c..7dd1df431d4a9 100644 --- a/tests/phpstan/README.md +++ b/tests/phpstan/README.md @@ -204,7 +204,7 @@ PHPStan errors can be ignored in the following ways: The baselines live in [`baselines/`](baselines), one file per error identifier, such as `variable.undefined.neon`. Splitting them this way keeps each kind of error visible as a single file that should shrink to nothing and then be deleted, rather than as part of one large file in which every kind is mixed together. -[`phpstan.neon.dist`](../../phpstan.neon.dist) does not list the baselines one by one. It includes [`baselines/loader.php`](baselines/loader.php), which includes every `.neon` file in that directory, so a baseline takes effect as soon as it is added, and stops as soon as it is deleted, without an edit of the configuration. +[`phpstan.neon.dist`](../../phpstan.neon.dist) does not list the baselines one by one. It includes [`baselines/loader.php`](baselines/loader.php), which includes every `.neon` file in that directory named for an error identifier, so a baseline takes effect as soon as it is added, and stops as soon as it is deleted, without an edit of the configuration. Every entry is scoped to the file the error occurs in and carries an exact occurrence count: From e46fbbb044f208bae081341cdbd38612b96c4dc4 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 23:27:41 -0800 Subject: [PATCH 08/38] Build/Test Tools: Only suppress PHPStan baselines when generating them. The loader included no baselines whenever WP_PHPSTAN_GENERATING_BASELINES was set, to any value. Exporting it as `0` or as an empty string to turn it off, or leaving it set in a shell, then silently dropped every baseline from an ordinary run, which reported thousands of errors with nothing pointing at the cause. The loader now only does so when the variable is `1`, the value the generator sets. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baselines/loader.php | 14 ++++++++------ tests/phpstan/generate-baselines.php | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php index 02873d61c6800..4a39ee317ef56 100644 --- a/tests/phpstan/baselines/loader.php +++ b/tests/phpstan/baselines/loader.php @@ -10,16 +10,18 @@ * PHPStan keys its cache of the configuration on the files it includes, so a * baseline added or deleted here takes effect on the next run. * - * `composer phpstan:baselines` sets WP_PHPSTAN_GENERATING_BASELINES while it - * analyzes, so that the errors the baselines suppress are reported again for it - * to write back out. Checking it here suppresses them however the configuration - * includes this file, even through another configuration that includes - * `phpstan.neon.dist`. + * `composer phpstan:baselines` sets WP_PHPSTAN_GENERATING_BASELINES to 1 while + * it analyzes, so that the errors the baselines suppress are reported again for + * it to write back out. Checking it here suppresses them however the + * configuration includes this file, even through another configuration that + * includes `phpstan.neon.dist`. Any other value, such as 0 or an empty string, + * leaves the baselines in force, so that a variable left set by mistake cannot + * silently drop them all. * * @package WordPress */ -if ( false !== getenv( 'WP_PHPSTAN_GENERATING_BASELINES' ) ) { +if ( '1' === getenv( 'WP_PHPSTAN_GENERATING_BASELINES' ) ) { return array( 'includes' => array() ); } diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 9527fecfb022e..a9bf38b103711 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -43,8 +43,8 @@ /** * Environment variable that tells `baselines/loader.php` to include nothing. * - * It is set while the analysis runs, so that the baselines in force do not - * suppress the errors being regenerated. The loader checks it itself, so this + * It is set to 1 while the analysis runs, so that the baselines in force do not + * suppress the errors being regenerated. The loader ignores any other value. The loader checks it itself, so this * works however the configuration includes the loader. */ const GENERATING_BASELINES_ENV = 'WP_PHPSTAN_GENERATING_BASELINES'; @@ -264,7 +264,7 @@ static function () use ( $temp_config, $temp_baseline ): void { * effect. * * The baselines need no removing from the configuration: GENERATING_BASELINES_ENV - * is set for the analysis, and every loader includes nothing while it is. + * is set to 1 for the analysis, and every loader includes nothing while it is. */ file_put_contents( $temp_config, From 8e5f885e7a6c708b85deb5ef85b6700f3380f336 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 23:43:14 -0800 Subject: [PATCH 09/38] Build/Test Tools: Keep the PHPStan baselines loader docblock true when copied. `composer phpstan:baselines --output-dir` copies the loader into the directory it writes to, docblock included. The docblock said that `phpstan.neon.dist` includes the file, which only holds for the default directory, and a reader of a copy could take the baselines there to be in force already. It now refers to whichever configuration includes the file, and notes that the file is copied. The docblock of GENERATING_BASELINES_ENV in the generator is also rewrapped, after the previous commit added a sentence to it. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baselines/loader.php | 7 +++++-- tests/phpstan/generate-baselines.php | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php index 4a39ee317ef56..9653590452d8c 100644 --- a/tests/phpstan/baselines/loader.php +++ b/tests/phpstan/baselines/loader.php @@ -2,11 +2,14 @@ /** * Includes every PHPStan baseline in this directory. * - * `phpstan.neon.dist` includes this file rather than listing the baselines one by + * A configuration includes this file rather than listing the baselines one by * one, so that adding a baseline, or deleting one that has reached zero, needs no * edit of the configuration. A NEON configuration can only include files by name, * but a PHP one returns the same structure and can find them itself. * + * `composer phpstan:baselines` copies this file into any other directory it is + * told to write baselines to, so nothing here is specific to one directory. + * * PHPStan keys its cache of the configuration on the files it includes, so a * baseline added or deleted here takes effect on the next run. * @@ -14,7 +17,7 @@ * it analyzes, so that the errors the baselines suppress are reported again for * it to write back out. Checking it here suppresses them however the * configuration includes this file, even through another configuration that - * includes `phpstan.neon.dist`. Any other value, such as 0 or an empty string, + * includes the one that includes it. Any other value, such as 0 or an empty string, * leaves the baselines in force, so that a variable left set by mistake cannot * silently drop them all. * diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index a9bf38b103711..30208f5b025dd 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -44,8 +44,8 @@ * Environment variable that tells `baselines/loader.php` to include nothing. * * It is set to 1 while the analysis runs, so that the baselines in force do not - * suppress the errors being regenerated. The loader ignores any other value. The loader checks it itself, so this - * works however the configuration includes the loader. + * suppress the errors being regenerated. The loader checks it itself, ignoring + * any other value, so this works however the configuration includes the loader. */ const GENERATING_BASELINES_ENV = 'WP_PHPSTAN_GENERATING_BASELINES'; From 9e9c3ef26fa261aa33758bcb2ffbe01dd3c245cf Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 11:12:40 -0800 Subject: [PATCH 10/38] Build/Test Tools: Only suppress the PHPStan baselines being regenerated. The generator set WP_PHPSTAN_GENERATING_BASELINES to 1, and every loader then included nothing. A stricter configuration that includes `phpstan.neon.dist` and keeps its own baselines in another directory, such as one holding selected paths to level 10, means for those to hold only the errors the default baselines leave, so that each error is baselined once. Regenerating them with every loader off wrote a duplicate of each default entry for those paths. The analysis then matched every error with the first of the two, and reported the other as unmatched, which cannot be ignored. The generator now sets the variable to the resolved output directory, and a loader only includes nothing when it names its own directory. The baselines elsewhere stay in force, so that only the errors they leave are written. Regenerating the default directory is unchanged, including through a local `phpstan.neon` that includes `phpstan.neon.dist`, and a value left set by mistake still matches no directory. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baselines/loader.php | 18 +++++++------- tests/phpstan/generate-baselines.php | 36 +++++++++++++++++++--------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php index 9653590452d8c..6c0cf433cf28d 100644 --- a/tests/phpstan/baselines/loader.php +++ b/tests/phpstan/baselines/loader.php @@ -13,18 +13,20 @@ * PHPStan keys its cache of the configuration on the files it includes, so a * baseline added or deleted here takes effect on the next run. * - * `composer phpstan:baselines` sets WP_PHPSTAN_GENERATING_BASELINES to 1 while - * it analyzes, so that the errors the baselines suppress are reported again for - * it to write back out. Checking it here suppresses them however the - * configuration includes this file, even through another configuration that - * includes the one that includes it. Any other value, such as 0 or an empty string, - * leaves the baselines in force, so that a variable left set by mistake cannot - * silently drop them all. + * `composer phpstan:baselines` sets WP_PHPSTAN_GENERATING_BASELINES to the + * directory it is writing baselines to while it analyzes, so that the errors + * those baselines suppress are reported again for it to write back out. + * Checking it here suppresses them however the configuration includes this file, + * even through another configuration that includes the one that includes it. + * + * Only the loader in that directory includes nothing. Baselines elsewhere stay + * in force, so that the regenerated ones hold only the errors those leave, and + * any other value, such as a variable left set by mistake, matches no directory. * * @package WordPress */ -if ( '1' === getenv( 'WP_PHPSTAN_GENERATING_BASELINES' ) ) { +if ( getenv( 'WP_PHPSTAN_GENERATING_BASELINES' ) === __DIR__ ) { return array( 'includes' => array() ); } diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 30208f5b025dd..3e838dfa9c53e 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -41,11 +41,17 @@ namespace WordPress\PHPStan; /** - * Environment variable that tells `baselines/loader.php` to include nothing. + * Environment variable that tells a baselines loader to include nothing. * - * It is set to 1 while the analysis runs, so that the baselines in force do not - * suppress the errors being regenerated. The loader checks it itself, ignoring - * any other value, so this works however the configuration includes the loader. + * It is set to the output directory while the analysis runs, so that the + * baselines being regenerated do not suppress their own errors. Each loader + * compares it with its own directory, so this works however the configuration + * includes the loader, and only the loader in the output directory is affected. + * + * The baselines in any other directory stay in force. Those regenerated are + * then only the errors the others leave, so that an error is baselined once: + * the baselines of a stricter configuration that includes `phpstan.neon.dist` + * hold what its baselines do not. */ const GENERATING_BASELINES_ENV = 'WP_PHPSTAN_GENERATING_BASELINES'; @@ -263,8 +269,9 @@ static function () use ( $temp_config, $temp_baseline ): void { * of the files it includes, so the wrapper's `tmpDir` is the one that takes * effect. * - * The baselines need no removing from the configuration: GENERATING_BASELINES_ENV - * is set to 1 for the analysis, and every loader includes nothing while it is. + * The baselines being regenerated need no removing from the configuration: + * GENERATING_BASELINES_ENV names their directory for the analysis, and the loader + * there includes nothing while it does. */ file_put_contents( $temp_config, @@ -293,10 +300,15 @@ static function () use ( $temp_config, $temp_baseline ): void { escapeshellarg( $memory_limit ) ); -fwrite( STDERR, "Analyzing with $config_option, existing baselines suppressed...\n" ); +fwrite( STDERR, "Analyzing with $config_option, baselines in $output_option suppressed...\n" ); -// PHPStan inherits it, as do the worker processes it starts. -putenv( GENERATING_BASELINES_ENV . '=1' ); +/* + * PHPStan inherits it, as do the worker processes it starts. The loader compares + * it with its __DIR__, which PHP resolves to a real path, so it is resolved the + * same way. An output directory not created yet holds no loader to match. + */ +$resolved_output_dir = realpath( $output_dir ); +putenv( GENERATING_BASELINES_ENV . '=' . ( false !== $resolved_output_dir ? $resolved_output_dir : $output_dir ) ); $exit_code = 0; passthru( $command, $exit_code ); @@ -426,7 +438,9 @@ function get_usage(): string { to the repository root. Paths inside them are written relative to this directory. A copy of the loader is written there if it has none, which the configuration - then has to include. + then has to include. Baselines in other directories + stay in force, so only the errors they leave are + written. Default: tests/phpstan/baselines --combined Print one combined baseline to stdout instead of writing per-identifier files. Nothing is written to disk. @@ -489,7 +503,7 @@ function read_file( string $path ): string { function read_baseline( string $path ): array { $data = require $path; - $parameters = is_array( $data ) ? ( $data['parameters'] ?? null ) : null; + $parameters = is_array( $data ) ? ( $data['parameters'] ?? null ) : null; $ignore_errors = is_array( $parameters ) ? ( $parameters['ignoreErrors'] ?? null ) : null; if ( ! is_array( $ignore_errors ) ) { From 61ccde15ea57b1e576a7f7155659858615966a18 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 11:12:54 -0800 Subject: [PATCH 11/38] Build/Test Tools: Accept an empty PHPStan baseline when regenerating. When the analysis reports no errors, PHPStan writes the baseline as `return [];`, without the `parameters` key it otherwise has. The generator rejected that as an unexpected structure and stopped before writing anything. That used to need a codebase free of errors, but it is now the usual outcome for a directory whose errors the baselines elsewhere already cover, such as with `--output-dir` and the default configuration. It is now read as no entries. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 3e838dfa9c53e..d574079a6834a 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -503,7 +503,16 @@ function read_file( string $path ): string { function read_baseline( string $path ): array { $data = require $path; - $parameters = is_array( $data ) ? ( $data['parameters'] ?? null ) : null; + /* + * PHPStan writes a baseline of no errors as an empty array, without the + * `parameters` it otherwise has. That is the usual outcome for a directory + * whose errors the baselines elsewhere already cover. + */ + if ( array() === $data ) { + return array(); + } + + $parameters = is_array( $data ) ? ( $data['parameters'] ?? null ) : null; $ignore_errors = is_array( $parameters ) ? ( $parameters['ignoreErrors'] ?? null ) : null; if ( ! is_array( $ignore_errors ) ) { From c8c39a481987d60f6b407ff36a1c44d3e6261d74 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 11:25:39 -0800 Subject: [PATCH 12/38] Build/Test Tools: Restore the alignment of an assignment in the baseline generator. The two previous commits each removed a space before the `=` of the `$parameters` assignment in `read_baseline()` by accident, leaving it out of line with the `$ignore_errors` assignment below it. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index d574079a6834a..2a85863c621c6 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -512,7 +512,7 @@ function read_baseline( string $path ): array { return array(); } - $parameters = is_array( $data ) ? ( $data['parameters'] ?? null ) : null; + $parameters = is_array( $data ) ? ( $data['parameters'] ?? null ) : null; $ignore_errors = is_array( $parameters ) ? ( $parameters['ignoreErrors'] ?? null ) : null; if ( ! is_array( $ignore_errors ) ) { From 04c4cc02059b74b192d2f3338b3ccf21e9ca0e90 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 11:38:13 -0800 Subject: [PATCH 13/38] Build/Test Tools: Include the PHPStan baselines by bare file name. The loader built each baseline's path from __DIR__, which PHP resolves to the real path, following any symbolic link. When the checkout was reached through a link, the baselines were then included from the real path, and the paths their entries name relative to themselves resolved there too, while the analyzed files were under the linked path. No entry matched, so every baselined error was reported, along with every entry as unmatched. Listing the baselines one by one, as before the loader, did not have this problem, since those paths were relative to the configuration. The loader now returns bare file names, which PHPStan resolves against the loader's path as the configuration reached it. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baselines/loader.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php index 6c0cf433cf28d..f302e8299c591 100644 --- a/tests/phpstan/baselines/loader.php +++ b/tests/phpstan/baselines/loader.php @@ -40,11 +40,18 @@ * pattern PHPStan validates identifiers with, so that a stray file such as a * merge tool's `argument.type_BACKUP_1234.neon` is not loaded along with the * real one. find_baselines() in `generate-baselines.php` matches the same names. + * + * Each is included by its bare file name, which PHPStan resolves against this + * file's path as the configuration reached it. __DIR__ is the real path, with any + * symbolic link resolved, so an absolute path built from it would place the + * baselines, and the paths their entries name relative to themselves, outside a + * checkout reached through a link, where they match none of the analyzed files. */ $baselines = array(); foreach ( $found as $file ) { - if ( 1 === preg_match( '/^[a-zA-Z0-9](?:[a-zA-Z0-9.]*[a-zA-Z0-9])?\.neon$/', basename( $file ) ) ) { - $baselines[] = $file; + $name = basename( $file ); + if ( 1 === preg_match( '/^[a-zA-Z0-9](?:[a-zA-Z0-9.]*[a-zA-Z0-9])?\.neon$/', $name ) ) { + $baselines[] = $name; } } From 2ce88ce6c25a1c8185501d744563585b6c58895b Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 11:53:31 -0800 Subject: [PATCH 14/38] Build/Test Tools: Share the PHPStan baselines loader logic in one place. The loader held the logic for finding the baselines, and the generator copied it word for word into any other directory given with `--output-dir`. A later fix to the loader never reached a copy, since one is only written where there is no loader yet. The pattern for a baseline's file name was also written twice, in the loader and in the generator, with a comment asking that the two be kept in sync. That logic now lives in `tests/phpstan/baseline-includes.php`, together with the pattern, the environment variable's name, and find_baselines(), which the generator requires. A loader is a stub that requires it and returns get_baseline_includes() for its own directory. The generator writes such a stub for a directory given with `--output-dir`, with the path to the shared file relative to that directory, and the default directory's loader is the stub it builds for that directory. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baseline-includes.php | 96 ++++++++++++++++++++++++++++ tests/phpstan/baselines/loader.php | 53 ++------------- tests/phpstan/generate-baselines.php | 81 +++++++++++------------ 3 files changed, 136 insertions(+), 94 deletions(-) create mode 100644 tests/phpstan/baseline-includes.php diff --git a/tests/phpstan/baseline-includes.php b/tests/phpstan/baseline-includes.php new file mode 100644 index 0000000000000..acd22ddbc524e --- /dev/null +++ b/tests/phpstan/baseline-includes.php @@ -0,0 +1,96 @@ + Absolute paths, sorted by name. + */ +function find_baselines( string $directory ): array { + $found = glob( $directory . '/*.neon' ); + + if ( false === $found ) { + return array(); + } + + sort( $found ); + + $files = array(); + foreach ( $found as $file ) { + if ( '' !== $file && 1 === preg_match( BASELINE_FILE_PATTERN, basename( $file ) ) ) { + $files[] = $file; + } + } + + return $files; +} + +/** + * Returns the configuration a baselines loader hands to PHPStan. + * + * Each baseline is included by its bare file name, which PHPStan resolves against + * the loader's path as the configuration reached it. A loader's __DIR__ is the + * real path, with any symbolic link resolved, so an absolute path built from it + * would place the baselines, and the paths their entries name relative to + * themselves, outside a checkout reached through a link, where they match none + * of the analyzed files. + * + * @param non-empty-string $directory The loader's directory, as its __DIR__. + * @return array{includes: list} Configuration. + */ +function get_baseline_includes( string $directory ): array { + if ( getenv( GENERATING_BASELINES_ENV ) === $directory ) { + return array( 'includes' => array() ); + } + + $names = array(); + foreach ( find_baselines( $directory ) as $file ) { + $names[] = basename( $file ); + } + + return array( 'includes' => $names ); +} diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php index f302e8299c591..75a23ab50eb8e 100644 --- a/tests/phpstan/baselines/loader.php +++ b/tests/phpstan/baselines/loader.php @@ -3,58 +3,13 @@ * Includes every PHPStan baseline in this directory. * * A configuration includes this file rather than listing the baselines one by - * one, so that adding a baseline, or deleting one that has reached zero, needs no - * edit of the configuration. A NEON configuration can only include files by name, - * but a PHP one returns the same structure and can find them itself. + * one. * - * `composer phpstan:baselines` copies this file into any other directory it is - * told to write baselines to, so nothing here is specific to one directory. - * - * PHPStan keys its cache of the configuration on the files it includes, so a - * baseline added or deleted here takes effect on the next run. - * - * `composer phpstan:baselines` sets WP_PHPSTAN_GENERATING_BASELINES to the - * directory it is writing baselines to while it analyzes, so that the errors - * those baselines suppress are reported again for it to write back out. - * Checking it here suppresses them however the configuration includes this file, - * even through another configuration that includes the one that includes it. - * - * Only the loader in that directory includes nothing. Baselines elsewhere stay - * in force, so that the regenerated ones hold only the errors those leave, and - * any other value, such as a variable left set by mistake, matches no directory. + * @see \WordPress\PHPStan\get_baseline_includes() * * @package WordPress */ -if ( getenv( 'WP_PHPSTAN_GENERATING_BASELINES' ) === __DIR__ ) { - return array( 'includes' => array() ); -} - -$found = glob( __DIR__ . '/*.neon' ); -if ( false === $found ) { - $found = array(); -} - -/* - * Only a file named for an error identifier is a baseline, matched with the - * pattern PHPStan validates identifiers with, so that a stray file such as a - * merge tool's `argument.type_BACKUP_1234.neon` is not loaded along with the - * real one. find_baselines() in `generate-baselines.php` matches the same names. - * - * Each is included by its bare file name, which PHPStan resolves against this - * file's path as the configuration reached it. __DIR__ is the real path, with any - * symbolic link resolved, so an absolute path built from it would place the - * baselines, and the paths their entries name relative to themselves, outside a - * checkout reached through a link, where they match none of the analyzed files. - */ -$baselines = array(); -foreach ( $found as $file ) { - $name = basename( $file ); - if ( 1 === preg_match( '/^[a-zA-Z0-9](?:[a-zA-Z0-9.]*[a-zA-Z0-9])?\.neon$/', $name ) ) { - $baselines[] = $name; - } -} +require_once __DIR__ . '/../baseline-includes.php'; -return array( - 'includes' => $baselines, -); +return \WordPress\PHPStan\get_baseline_includes( __DIR__ ); diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 2a85863c621c6..291e2a703a5fa 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -40,29 +40,15 @@ namespace WordPress\PHPStan; -/** - * Environment variable that tells a baselines loader to include nothing. - * - * It is set to the output directory while the analysis runs, so that the - * baselines being regenerated do not suppress their own errors. Each loader - * compares it with its own directory, so this works however the configuration - * includes the loader, and only the loader in the output directory is affected. - * - * The baselines in any other directory stay in force. Those regenerated are - * then only the errors the others leave, so that an error is baselined once: - * the baselines of a stricter configuration that includes `phpstan.neon.dist` - * hold what its baselines do not. - */ -const GENERATING_BASELINES_ENV = 'WP_PHPSTAN_GENERATING_BASELINES'; +// GENERATING_BASELINES_ENV and find_baselines(), shared with the loaders. +require_once __DIR__ . '/baseline-includes.php'; /** - * Pattern matching the file name of a baseline. + * The directory baselines are written to when `--output-dir` does not name one. * - * The name is the error identifier, matched as PHPStan validates identifiers, - * followed by `.neon`. `baselines/loader.php` matches the same names, and the two - * have to stay in sync. + * Relative to the repository root. */ -const BASELINE_FILE_PATTERN = '/^[a-zA-Z0-9](?:[a-zA-Z0-9.]*[a-zA-Z0-9])?\.neon$/'; +const DEFAULT_OUTPUT_DIR = 'tests/phpstan/baselines'; /** * The configuration analyzed when `--config` does not name one. @@ -88,7 +74,7 @@ array_shift( $args ); $config_option = DEFAULT_CONFIG; -$output_option = 'tests/phpstan/baselines'; +$output_option = DEFAULT_OUTPUT_DIR; $memory_limit = '2G'; $only_identifiers = array(); $combined = false; @@ -350,11 +336,11 @@ static function () use ( $temp_config, $temp_baseline ): void { /* * The baselines only take effect through a loader in their own directory, which - * the default one already has. A directory given with `--output-dir` gets a copy, + * the default one already has. A directory given with `--output-dir` gets one, * but nothing includes it yet, so say so. */ if ( ! is_file( $output_dir . '/loader.php' ) ) { - if ( ! copy( __DIR__ . '/baselines/loader.php', $output_dir . '/loader.php' ) ) { + if ( false === file_put_contents( $output_dir . '/loader.php', build_loader( $output_dir ) ) ) { fwrite( STDERR, "Could not write $output_option/loader.php\n" ); exit( 1 ); } @@ -436,8 +422,8 @@ function get_usage(): string { root. Default: phpstan.neon.dist --output-dir= Where the per-identifier baselines are written, relative to the repository root. Paths inside them are written - relative to this directory. A copy of the loader is - written there if it has none, which the configuration + relative to this directory. A loader is written + there if it has none, which the configuration then has to include. Baselines in other directories stay in force, so only the errors they leave are written. @@ -558,33 +544,38 @@ function read_baseline( string $path ): array { } /** - * Lists the per-identifier baselines present on disk. + * Builds the loader for a baselines directory. + * + * The loader holds no logic of its own. It requires `baseline-includes.php`, by a + * path relative to its directory, and returns what get_baseline_includes() finds + * there, so a fix to that reaches every directory's loader alike. + * `baselines/loader.php` is the one this builds for the default directory. * * @param non-empty-string $output_dir Absolute path to the baseline directory. - * @return list Absolute paths, sorted by name. + * @return non-falsy-string Loader contents. */ -function find_baselines( string $output_dir ): array { - $found = glob( $output_dir . '/*.neon' ); - - if ( false === $found ) { - return array(); - } +function build_loader( string $output_dir ): string { + return sprintf( + <<<'PHP' + Date: Tue, 17 Nov 2026 11:53:44 -0800 Subject: [PATCH 15/38] Build/Test Tools: Say when other PHPStan baselines stay in force while regenerating. Writing baselines anywhere but the default directory leaves the default baselines in force, so only the errors they leave are written. A run expecting every error came up short with nothing to say why. The generator now says so when the output directory is not the default. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 291e2a703a5fa..d67c2239e43df 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -288,6 +288,15 @@ static function () use ( $temp_config, $temp_baseline ): void { fwrite( STDERR, "Analyzing with $config_option, baselines in $output_option suppressed...\n" ); +/* + * Writing anywhere but the default leaves the default baselines in force, so a + * run expecting every error, rather than those the others leave, would come up + * short with nothing to say why. + */ +if ( DEFAULT_OUTPUT_DIR !== trim( normalize_path( $output_option ), '/' ) ) { + fwrite( STDERR, "Baselines in other directories stay in force, so only the errors they leave are written.\n" ); +} + /* * PHPStan inherits it, as do the worker processes it starts. The loader compares * it with its __DIR__, which PHP resolves to a real path, so it is resolved the From 280eb7db6ecfad8cb082039d0ae2eb33e38644ef Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 12:11:40 -0800 Subject: [PATCH 16/38] Build/Test Tools: Resolve both ends of a PHPStan baselines loader's require path. build_loader() worked out the path from a loader to the shared `baseline-includes.php` starting at the output directory as given. The loader follows it from its __DIR__, which PHP resolves to the real path. When the output directory was reached through a symbolic link, the two disagreed, and the loader required a file that does not exist, so PHPStan failed as soon as the configuration included it. The path is now worked out between the resolved directories. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index d67c2239e43df..71370a1341c2d 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -560,10 +560,23 @@ function read_baseline( string $path ): array { * there, so a fix to that reaches every directory's loader alike. * `baselines/loader.php` is the one this builds for the default directory. * - * @param non-empty-string $output_dir Absolute path to the baseline directory. + * The relative path is worked out between the resolved directories, since that is + * how the loader's __DIR__ is resolved when the path is followed. Worked out from + * the directory as given, a symbolic link in it would send the loader looking + * somewhere else. + * + * @param non-empty-string $output_dir Absolute path to the baseline directory, which exists. * @return non-falsy-string Loader contents. */ function build_loader( string $output_dir ): string { + $from = realpath( $output_dir ); + $to = realpath( __DIR__ . '/baseline-includes.php' ); + + if ( false === $from || false === $to ) { + fwrite( STDERR, "Could not resolve $output_dir or the shared baseline includes.\n" ); + exit( 1 ); + } + return sprintf( <<<'PHP' Date: Tue, 17 Nov 2026 12:11:58 -0800 Subject: [PATCH 17/38] Build/Test Tools: Say other PHPStan baselines stay in force for any non-default run. The note that baselines in other directories stay in force while regenerating was only printed for an output directory other than the default. They also stay in force when the default directory is regenerated with a configuration that includes another directory's loader, such as a local `phpstan.neon` adding stricter baselines of its own. The default baselines were then written without the errors those cover, with nothing said, and CI, which does not include that loader, reported them. The note is now printed unless both the configuration and the output directory are the defaults, with the directories compared as resolved, as the loader compares them. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 71370a1341c2d..dfed172098039 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -288,15 +288,6 @@ static function () use ( $temp_config, $temp_baseline ): void { fwrite( STDERR, "Analyzing with $config_option, baselines in $output_option suppressed...\n" ); -/* - * Writing anywhere but the default leaves the default baselines in force, so a - * run expecting every error, rather than those the others leave, would come up - * short with nothing to say why. - */ -if ( DEFAULT_OUTPUT_DIR !== trim( normalize_path( $output_option ), '/' ) ) { - fwrite( STDERR, "Baselines in other directories stay in force, so only the errors they leave are written.\n" ); -} - /* * PHPStan inherits it, as do the worker processes it starts. The loader compares * it with its __DIR__, which PHP resolves to a real path, so it is resolved the @@ -305,6 +296,19 @@ static function () use ( $temp_config, $temp_baseline ): void { $resolved_output_dir = realpath( $output_dir ); putenv( GENERATING_BASELINES_ENV . '=' . ( false !== $resolved_output_dir ? $resolved_output_dir : $output_dir ) ); +/* + * The baselines in any other directory the configuration includes stay in force, + * so a run expecting every error, rather than those the others leave, would come + * up short with nothing to say why. The default configuration includes only the + * default directory's, so that is the one combination with nothing to say. Any + * other, whether a configuration including the default one alongside a loader of + * its own, or a directory other than the default, may well leave some in force. + * The directories are compared as resolved, as the loader does. + */ +if ( DEFAULT_CONFIG !== $config_relative || realpath( $repo_root . '/' . DEFAULT_OUTPUT_DIR ) !== $resolved_output_dir ) { + fwrite( STDERR, "Baselines in any other directory the configuration includes stay in force, so only the errors they leave are written.\n" ); +} + $exit_code = 0; passthru( $command, $exit_code ); From c972e54482b856443b298da334782bf27a60c396 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 12:12:15 -0800 Subject: [PATCH 18/38] Build/Test Tools: Rewrite the PHPStan baselines loader on every regeneration. A loader was only written to a directory that had none, so the committed `tests/phpstan/baselines/loader.php`, and a loader written to any other directory, were copies of what build_loader() makes that could drift from it unnoticed: a change to one and not the other kept both working, but differently. The generator now writes the loader on every run where it differs, so any drift shows up as a change to the loader. It only asks for the loader to be included when the directory had none before. The loader says it is written by the generator and not to be edited by hand. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baselines/loader.php | 3 ++- tests/phpstan/generate-baselines.php | 26 +++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/tests/phpstan/baselines/loader.php b/tests/phpstan/baselines/loader.php index 75a23ab50eb8e..829e4ac4880d4 100644 --- a/tests/phpstan/baselines/loader.php +++ b/tests/phpstan/baselines/loader.php @@ -3,7 +3,8 @@ * Includes every PHPStan baseline in this directory. * * A configuration includes this file rather than listing the baselines one by - * one. + * one. `composer phpstan:baselines` writes it on every run, so it is not to be + * edited by hand. * * @see \WordPress\PHPStan\get_baseline_includes() * diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index dfed172098039..a62767cf19199 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -348,18 +348,25 @@ static function () use ( $temp_config, $temp_baseline ): void { } /* - * The baselines only take effect through a loader in their own directory, which - * the default one already has. A directory given with `--output-dir` gets one, - * but nothing includes it yet, so say so. + * The baselines only take effect through a loader in their own directory. It is + * written on every run, so that the committed loader of the default directory, and + * that of any other, stay what build_loader() makes rather than drifting from it + * unnoticed. A directory given with `--output-dir` that had none is not included + * by anything yet, so say so. */ -if ( ! is_file( $output_dir . '/loader.php' ) ) { - if ( false === file_put_contents( $output_dir . '/loader.php', build_loader( $output_dir ) ) ) { +$loader_path = $output_dir . '/loader.php'; +$loader = build_loader( $output_dir ); +$loader_existed = is_file( $loader_path ); +$loader_contents = $loader_existed ? file_get_contents( $loader_path ) : false; + +if ( $loader !== $loader_contents ) { + if ( false === file_put_contents( $loader_path, $loader ) ) { fwrite( STDERR, "Could not write $output_option/loader.php\n" ); exit( 1 ); } printf( - "%s: written. Include it in the configuration for these baselines to take effect.\n", + $loader_existed ? "%s: rewritten.\n" : "%s: written. Include it in the configuration for these baselines to take effect.\n", $output_option . '/loader.php' ); } @@ -436,8 +443,8 @@ function get_usage(): string { --output-dir= Where the per-identifier baselines are written, relative to the repository root. Paths inside them are written relative to this directory. A loader is written - there if it has none, which the configuration - then has to include. Baselines in other directories + there too, which the configuration has to + include. Baselines in other directories stay in force, so only the errors they leave are written. Default: tests/phpstan/baselines @@ -588,7 +595,8 @@ function build_loader( string $output_dir ): string { * Includes every PHPStan baseline in this directory. * * A configuration includes this file rather than listing the baselines one by - * one. + * one. `composer phpstan:baselines` writes it on every run, so it is not to be + * edited by hand. * * @see \WordPress\PHPStan\get_baseline_includes() * From 11dd05aa1c7ab1512ab37b631115d5e9076a1ff1 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 12:35:01 -0800 Subject: [PATCH 19/38] Build/Test Tools: List the PHPStan baselines directory rather than globbing it. find_baselines() passed the directory to glob() unescaped, so a checkout whose path holds a glob metacharacter, such as the brackets of `wp [old]/`, matched nothing. The loader then included no baselines, and every baselined error was reported, with nothing pointing at why. A glob() failure was likewise read as no baselines. The directory is now listed with scandir(), and one that cannot be listed throws, which stops PHPStan while it loads the configuration rather than dropping every suppression. find_baselines() returns the file names it finds, which is what the loader includes, rather than absolute paths it turned back into names. The generator adds the directory where it needs a path, and `--prune` of a directory not created yet finds nothing to prune, as before. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baseline-includes.php | 41 ++++++++++++++++------------ tests/phpstan/generate-baselines.php | 16 ++++++----- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/tests/phpstan/baseline-includes.php b/tests/phpstan/baseline-includes.php index acd22ddbc524e..ebb5f0364587f 100644 --- a/tests/phpstan/baseline-includes.php +++ b/tests/phpstan/baseline-includes.php @@ -47,26 +47,34 @@ /** * Lists the per-identifier baselines present on disk. * + * The directory is listed rather than globbed, since glob() would read any of its + * metacharacters in the directory's own path, such as the brackets of a checkout + * in `wp [old]/`, as a pattern, and match nothing. + * + * A directory that cannot be listed is an error rather than one with no + * baselines, since treating it as empty would drop every suppression at once, + * with nothing to say why. + * * @param non-empty-string $directory Absolute path to the baseline directory. - * @return list Absolute paths, sorted by name. + * @return list File names, sorted. + * + * @throws \RuntimeException When the directory cannot be listed. */ function find_baselines( string $directory ): array { - $found = glob( $directory . '/*.neon' ); + $entries = is_dir( $directory ) ? scandir( $directory ) : false; - if ( false === $found ) { - return array(); + if ( false === $entries ) { + throw new \RuntimeException( "Could not list the PHPStan baselines in $directory." ); } - sort( $found ); - - $files = array(); - foreach ( $found as $file ) { - if ( '' !== $file && 1 === preg_match( BASELINE_FILE_PATTERN, basename( $file ) ) ) { - $files[] = $file; + $names = array(); + foreach ( $entries as $name ) { + if ( '' !== $name && 1 === preg_match( BASELINE_FILE_PATTERN, $name ) ) { + $names[] = $name; } } - return $files; + return $names; } /** @@ -80,17 +88,14 @@ function find_baselines( string $directory ): array { * of the analyzed files. * * @param non-empty-string $directory The loader's directory, as its __DIR__. - * @return array{includes: list} Configuration. + * @return array{includes: list} Configuration. + * + * @throws \RuntimeException When the directory cannot be listed. */ function get_baseline_includes( string $directory ): array { if ( getenv( GENERATING_BASELINES_ENV ) === $directory ) { return array( 'includes' => array() ); } - $names = array(); - foreach ( find_baselines( $directory ) as $file ) { - $names[] = basename( $file ); - } - - return array( 'includes' => $names ); + return array( 'includes' => find_baselines( $directory ) ); } diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index a62767cf19199..c21d35bbe9b7b 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -149,17 +149,19 @@ $pruned = 0; - foreach ( find_baselines( $output_dir ) as $file ) { - $identifier = basename( $file, '.neon' ); + // A directory not written to yet has no baselines to prune. + foreach ( is_dir( $output_dir ) ? find_baselines( $output_dir ) : array() as $name ) { + $identifier = basename( $name, '.neon' ); if ( $only_identifiers && ! in_array( $identifier, $only_identifiers, true ) ) { continue; } + $file = $output_dir . '/' . $name; $is_empty = is_empty_baseline( read_file( $file ) ); if ( null === $is_empty ) { - fwrite( STDERR, sprintf( "%s: no `ignoreErrors` list found, left untouched.\n", $output_option . '/' . basename( $file ) ) ); + fwrite( STDERR, sprintf( "%s: no `ignoreErrors` list found, left untouched.\n", $output_option . '/' . $name ) ); continue; } @@ -168,11 +170,11 @@ } if ( ! unlink( $file ) ) { - fwrite( STDERR, sprintf( "%s: could not be deleted.\n", $output_option . '/' . basename( $file ) ) ); + fwrite( STDERR, sprintf( "%s: could not be deleted.\n", $output_option . '/' . $name ) ); exit( 1 ); } - printf( "%s: no entries remain, file deleted.\n", $output_option . '/' . basename( $file ) ); + printf( "%s: no entries remain, file deleted.\n", $output_option . '/' . $name ); ++$pruned; } @@ -397,8 +399,8 @@ static function () use ( $temp_config, $temp_baseline ): void { $retired = $only_identifiers; if ( ! $only_identifiers ) { - foreach ( find_baselines( $output_dir ) as $file ) { - $retired[] = basename( $file, '.neon' ); + foreach ( find_baselines( $output_dir ) as $name ) { + $retired[] = basename( $name, '.neon' ); } } From 953f66c14f9259aec313c45d623a382aa245eb38 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 12:35:21 -0800 Subject: [PATCH 20/38] Build/Test Tools: Reject a PHPStan baselines `--identifier` that is not an identifier. The value of `--identifier` names the file a baseline is written to and retired from, and it was not checked. One such as `../../../phpstan` named a file outside the baselines directory, which a run deleted when no error with that identifier was reported, taking a local `phpstan.neon` with it. An identifier is now checked against the same pattern a baseline's file name is, and the run stops on one that does not match. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index c21d35bbe9b7b..1aa9a5784b991 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -99,9 +99,22 @@ if ( 1 === preg_match( '/^--identifier=(.+)$/', $arg, $matches ) ) { foreach ( explode( ',', $matches[1] ) as $identifier ) { $identifier = trim( $identifier ); - if ( '' !== $identifier ) { - $only_identifiers[] = $identifier; + if ( '' === $identifier ) { + continue; + } + + /* + * The identifier names the file a baseline is written to and retired + * from, so one that is not an identifier, such as `../../../phpstan`, + * could name a file outside the baselines, which would be deleted when + * no such identifier is reported. + */ + if ( 1 !== preg_match( BASELINE_FILE_PATTERN, $identifier . '.neon' ) ) { + fwrite( STDERR, "Not an error identifier: $identifier\n" ); + exit( 1 ); } + + $only_identifiers[] = $identifier; } continue; } From 3563d3bc3699b64a53977e44a6d46aad6dde652f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 12:35:40 -0800 Subject: [PATCH 21/38] Build/Test Tools: Rewrap the `--output-dir` help of the PHPStan baselines generator. An earlier edit left its lines at about 45 columns, where the rest of the usage message wraps at about 80. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 1aa9a5784b991..5ec16c145942f 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -457,11 +457,10 @@ function get_usage(): string { root. Default: phpstan.neon.dist --output-dir= Where the per-identifier baselines are written, relative to the repository root. Paths inside them are written - relative to this directory. A loader is written - there too, which the configuration has to - include. Baselines in other directories - stay in force, so only the errors they leave are - written. + relative to this directory. A loader is written there + too, which the configuration has to include. Baselines + in other directories stay in force, so only the errors + they leave are written. Default: tests/phpstan/baselines --combined Print one combined baseline to stdout instead of writing per-identifier files. Nothing is written to disk. From 25ab64b91bae75fcda1ff3417cebfa32e7f6f5c1 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 12:36:01 -0800 Subject: [PATCH 22/38] Build/Test Tools: Only name an existing output directory to the PHPStan baselines loaders. When the output directory did not exist yet, WP_PHPSTAN_GENERATING_BASELINES was set to its unresolved path instead. No loader can be there to match it, so it did nothing. It is now only set when the directory resolves. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 5ec16c145942f..aaf19f31a44cf 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -306,10 +306,13 @@ static function () use ( $temp_config, $temp_baseline ): void { /* * PHPStan inherits it, as do the worker processes it starts. The loader compares * it with its __DIR__, which PHP resolves to a real path, so it is resolved the - * same way. An output directory not created yet holds no loader to match. + * same way. An output directory not created yet holds no loader to match, so it + * is not set at all then. */ $resolved_output_dir = realpath( $output_dir ); -putenv( GENERATING_BASELINES_ENV . '=' . ( false !== $resolved_output_dir ? $resolved_output_dir : $output_dir ) ); +if ( false !== $resolved_output_dir ) { + putenv( GENERATING_BASELINES_ENV . '=' . $resolved_output_dir ); +} /* * The baselines in any other directory the configuration includes stay in force, From 30d2ef7ce1821739d30c5ed77389cb0a3d555574 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:09:02 -0800 Subject: [PATCH 23/38] Build/Test Tools: Compare PHPStan baselines directories by identity, not spelling. A loader included nothing when WP_PHPSTAN_GENERATING_BASELINES was the same string as its __DIR__. The generator set it to the realpath() of the output directory, which resolves symbolic links but keeps the case it was given. On a case-insensitive file system, `--output-dir` spelled as `Tests/PHPStan/Baselines` therefore missed the loader of `tests/phpstan/baselines`: the baselines stayed in force, their errors were not reported, and the run retired every one of them. Directories are now compared with is_same_directory(), by the device and inode the file system reports, falling back to the resolved paths where it reports no inode. The loader, the note about other baselines staying in force, and build_loader() all use it. build_loader() finds the common ancestor of the output directory and the shared file that way, so the loader it writes no longer changes with the spelling of the option. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baseline-includes.php | 44 ++++++++++++++++++++++++++-- tests/phpstan/generate-baselines.php | 42 +++++++++++++++++++++----- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/tests/phpstan/baseline-includes.php b/tests/phpstan/baseline-includes.php index ebb5f0364587f..4a7b2d249f456 100644 --- a/tests/phpstan/baseline-includes.php +++ b/tests/phpstan/baseline-includes.php @@ -24,8 +24,9 @@ * `composer phpstan:baselines` sets it to the directory it is writing baselines * to while it analyzes, so that the errors those baselines suppress are reported * again for it to write back out. Each loader compares it with its own directory, - * so this works however the configuration includes the loader, even through - * another configuration that includes the one that includes it. + * with is_same_directory(), so this works however the directory is spelled, and + * however the configuration includes the loader, even through another + * configuration that includes the one that includes it. * * The baselines in any other directory stay in force. Those regenerated are then * only the errors the others leave, so that an error is baselined once: the @@ -77,6 +78,42 @@ function find_baselines( string $directory ): array { return $names; } +/** + * Determines whether two paths name the same directory. + * + * They are compared by the device and inode the file system reports, not by how + * they are spelled. realpath() resolves symbolic links but keeps the case it was + * given, so on a case-insensitive file system `Tests/PHPStan/Baselines` and + * `tests/phpstan/baselines` resolve to two strings naming one directory. Were + * the loader's check to miss that, the baselines being regenerated would stay in + * force, report nothing, and all be retired. + * + * A file system that reports no inode, as some on Windows do, falls back to + * comparing the resolved paths. + * + * @param string $a A path. + * @param string $b Another path. + * @return bool Whether both are directories, and the same one. + */ +function is_same_directory( string $a, string $b ): bool { + if ( '' === $a || '' === $b || ! is_dir( $a ) || ! is_dir( $b ) ) { + return false; + } + + $stat_a = stat( $a ); + $stat_b = stat( $b ); + + if ( false === $stat_a || false === $stat_b ) { + return false; + } + + if ( 0 === $stat_a['ino'] || 0 === $stat_b['ino'] ) { + return realpath( $a ) === realpath( $b ); + } + + return $stat_a['dev'] === $stat_b['dev'] && $stat_a['ino'] === $stat_b['ino']; +} + /** * Returns the configuration a baselines loader hands to PHPStan. * @@ -93,7 +130,8 @@ function find_baselines( string $directory ): array { * @throws \RuntimeException When the directory cannot be listed. */ function get_baseline_includes( string $directory ): array { - if ( getenv( GENERATING_BASELINES_ENV ) === $directory ) { + $generating = getenv( GENERATING_BASELINES_ENV ); + if ( false !== $generating && is_same_directory( $generating, $directory ) ) { return array( 'includes' => array() ); } diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index aaf19f31a44cf..595f1847d81d1 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -304,10 +304,8 @@ static function () use ( $temp_config, $temp_baseline ): void { fwrite( STDERR, "Analyzing with $config_option, baselines in $output_option suppressed...\n" ); /* - * PHPStan inherits it, as do the worker processes it starts. The loader compares - * it with its __DIR__, which PHP resolves to a real path, so it is resolved the - * same way. An output directory not created yet holds no loader to match, so it - * is not set at all then. + * PHPStan inherits it, as do the worker processes it starts. An output directory + * not created yet holds no loader to match, so it is not set at all then. */ $resolved_output_dir = realpath( $output_dir ); if ( false !== $resolved_output_dir ) { @@ -321,9 +319,9 @@ static function () use ( $temp_config, $temp_baseline ): void { * default directory's, so that is the one combination with nothing to say. Any * other, whether a configuration including the default one alongside a loader of * its own, or a directory other than the default, may well leave some in force. - * The directories are compared as resolved, as the loader does. + * The directories are compared as the loader compares them. */ -if ( DEFAULT_CONFIG !== $config_relative || realpath( $repo_root . '/' . DEFAULT_OUTPUT_DIR ) !== $resolved_output_dir ) { +if ( DEFAULT_CONFIG !== $config_relative || ! is_same_directory( $repo_root . '/' . DEFAULT_OUTPUT_DIR, $output_dir ) ) { fwrite( STDERR, "Baselines in any other directory the configuration includes stay in force, so only the errors they leave are written.\n" ); } @@ -593,6 +591,13 @@ function read_baseline( string $path ): array { * the directory as given, a symbolic link in it would send the loader looking * somewhere else. * + * Their common ancestor is found with is_same_directory() rather than by + * comparing the paths as strings, since realpath() keeps the case it was given. + * On a case-insensitive file system, `--output-dir=Tests/PHPStan/Baselines` + * would otherwise share no ancestor with `tests/phpstan` short of the repository + * root, and the loader written for the default directory would change with the + * spelling of the option. + * * @param non-empty-string $output_dir Absolute path to the baseline directory, which exists. * @return non-falsy-string Loader contents. */ @@ -605,6 +610,29 @@ function build_loader( string $output_dir ): string { exit( 1 ); } + $relative = null; + $ups = 0; + for ( $dir = $from; null === $relative; $dir = dirname( $dir ), $ups++ ) { + for ( $base = dirname( $to ); null === $relative; $base = dirname( $base ) ) { + if ( is_same_directory( $dir, $base ) ) { + $relative = str_repeat( '../', $ups ) . ltrim( substr( $to, strlen( $base ) ), '/' ); + } + + if ( dirname( $base ) === $base ) { + break; + } + } + + if ( dirname( $dir ) === $dir ) { + break; + } + } + + if ( null === $relative ) { + fwrite( STDERR, "Could not relate $output_dir to the shared baseline includes.\n" ); + exit( 1 ); + } + return sprintf( <<<'PHP' Date: Tue, 17 Nov 2026 13:09:24 -0800 Subject: [PATCH 24/38] Build/Test Tools: Clear an inherited WP_PHPSTAN_GENERATING_BASELINES before regenerating. The generator only set the variable when the output directory existed, and otherwise left alone any value inherited from the shell. One exported to name the default directory, while writing to a new one, then switched off the default baselines during the analysis, and the new directory got a duplicate of them rather than only the errors they leave. The variable is now cleared first, and set only when the output directory exists. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 595f1847d81d1..58307940f238c 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -305,8 +305,11 @@ static function () use ( $temp_config, $temp_baseline ): void { /* * PHPStan inherits it, as do the worker processes it starts. An output directory - * not created yet holds no loader to match, so it is not set at all then. + * not created yet holds no loader to match, so it is not set at all then. It is + * cleared first either way, so that a value exported in the shell, perhaps naming + * another directory, cannot switch off baselines this run means to keep. */ +putenv( GENERATING_BASELINES_ENV ); $resolved_output_dir = realpath( $output_dir ); if ( false !== $resolved_output_dir ) { putenv( GENERATING_BASELINES_ENV . '=' . $resolved_output_dir ); From 0d03b7303deaf4c35eace3f5006906027ba2ff3b Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:09:52 -0800 Subject: [PATCH 25/38] Build/Test Tools: Report an unlistable PHPStan baselines directory without a stack trace. find_baselines() throws when the directory cannot be listed, which is what stops PHPStan when a loader calls it. In the generator the exception went uncaught, so the run ended in a fatal error and a stack trace, where every other failure prints a message and exits with 1. The generator now lists the baselines through list_baselines(), which reports the failure that way. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 58307940f238c..b0294c665d272 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -163,7 +163,7 @@ $pruned = 0; // A directory not written to yet has no baselines to prune. - foreach ( is_dir( $output_dir ) ? find_baselines( $output_dir ) : array() as $name ) { + foreach ( is_dir( $output_dir ) ? list_baselines( $output_dir ) : array() as $name ) { $identifier = basename( $name, '.neon' ); if ( $only_identifiers && ! in_array( $identifier, $only_identifiers, true ) ) { @@ -416,7 +416,7 @@ static function () use ( $temp_config, $temp_baseline ): void { $retired = $only_identifiers; if ( ! $only_identifiers ) { - foreach ( find_baselines( $output_dir ) as $name ) { + foreach ( list_baselines( $output_dir ) as $name ) { $retired[] = basename( $name, '.neon' ); } } @@ -498,6 +498,24 @@ function get_usage(): string { TEXT; } +/** + * Lists the baselines in a directory, stopping the run if it cannot be listed. + * + * find_baselines() throws, which stops PHPStan when a loader calls it. Here the + * same failure is reported the way the rest of this script reports one. + * + * @param non-empty-string $output_dir Absolute path to the baseline directory. + * @return list File names, sorted. + */ +function list_baselines( string $output_dir ): array { + try { + return find_baselines( $output_dir ); + } catch ( \RuntimeException $e ) { + fwrite( STDERR, $e->getMessage() . "\n" ); + exit( 1 ); + } +} + /** * Reads a file, failing loudly rather than continuing with false. * From 54af27bbcf3b4ae555ac18654ee956e624dee907 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:10:50 -0800 Subject: [PATCH 26/38] Build/Test Tools: Only delete PHPStan baselines the generator wrote. A full regeneration retired every `.neon` file in the output directory whose name passes for an identifier when that identifier reported nothing, and `--prune` deleted any such file with an empty `ignoreErrors`. Pointed at a directory holding other configuration, that meant `--output-dir=tests/phpstan` deleted `base.neon`, and `--output-dir=.` the root `phpstan.neon`. Both now only delete a file that starts with the header the generator writes for its identifier, and leave any other in place with a warning. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 37 ++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index b0294c665d272..a27ce46cedc79 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -170,7 +170,13 @@ continue; } - $file = $output_dir . '/' . $name; + $file = $output_dir . '/' . $name; + + if ( ! is_generated_baseline( $file, $identifier ) ) { + fwrite( STDERR, sprintf( "%s: not a baseline written by this script, left untouched.\n", $output_option . '/' . $name ) ); + continue; + } + $is_empty = is_empty_baseline( read_file( $file ) ); if ( null === $is_empty ) { @@ -427,7 +433,9 @@ static function () use ( $temp_config, $temp_baseline ): void { } $file = $output_dir . '/' . $identifier . '.neon'; - if ( is_file( $file ) && unlink( $file ) ) { + if ( is_file( $file ) && ! is_generated_baseline( $file, $identifier ) ) { + fwrite( STDERR, sprintf( "%s: not a baseline written by this script, left untouched.\n", $output_option . '/' . $identifier . '.neon' ) ); + } elseif ( is_file( $file ) && unlink( $file ) ) { printf( "%s: no errors remain, file deleted.\n", $output_option . '/' . $identifier . '.neon' ); } else { printf( "%s: no errors reported.\n", $identifier ); @@ -498,6 +506,31 @@ function get_usage(): string { TEXT; } +/** + * Determines whether a file is a baseline this script wrote. + * + * A file is only retired or pruned when it starts with the header this script + * writes for its identifier. Any `.neon` file whose name passes for an identifier + * would otherwise be deleted when that identifier reports nothing, such as + * `base.neon` with `--output-dir=tests/phpstan`, or the root `phpstan.neon` with + * `--output-dir=.`. + * + * @param non-empty-string $file Absolute path to the file. + * @param string $identifier Identifier the file is named for. + * @return bool Whether the file starts with the header for that identifier. + */ +function is_generated_baseline( string $file, string $identifier ): bool { + // Neither can name a baseline, nor have a header built for it. + if ( '' === $identifier || '0' === $identifier ) { + return false; + } + + $header = build_baseline_header( $identifier ); + $first = substr( $header, 0, (int) strpos( $header, "\n" ) + 1 ); + + return 0 === strpos( read_file( $file ), $first ); +} + /** * Lists the baselines in a directory, stopping the run if it cannot be listed. * From 354a2e75ae74e8d09cab5d46da0e1aaa057cf6fd Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:22:51 -0800 Subject: [PATCH 27/38] Build/Test Tools: Stop when a PHPStan baseline cannot be written. The generator did not check whether writing each baseline succeeded. On a read-only file or a full disk it still printed the file's counts as if it had been written, went on to retire others, and exited with 0, leaving the stale baseline in place. It now stops with an error naming the file, as it already does for the loader. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index a27ce46cedc79..3a9a9c42dd9cc 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -397,11 +397,16 @@ static function () use ( $temp_config, $temp_baseline ): void { } foreach ( $grouped as $identifier => $entries ) { - file_put_contents( + $written = file_put_contents( $output_dir . '/' . $identifier . '.neon', build_baseline( $entries, $output_dir, build_baseline_header( $identifier ) ) ); + if ( false === $written ) { + fwrite( STDERR, "Could not write $output_option/$identifier.neon\n" ); + exit( 1 ); + } + printf( "%s: %d entries, %d errors\n", $output_option . '/' . $identifier . '.neon', From 9c03be1f6582fa70a463241dc09f73266e48e635 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:23:11 -0800 Subject: [PATCH 28/38] Build/Test Tools: Correct what the PHPStan baseline header says regeneration does. Each baseline said that regenerating it reruns the analysis with that file suppressed. The loader suppresses every baseline in its directory while the generator runs, even with `--identifier`, and the generator then writes only the named one. The header now says so, and the baselines are regenerated to carry it. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baselines/argument.type.neon | 3 ++- tests/phpstan/baselines/argument.unresolvableType.neon | 3 ++- tests/phpstan/baselines/assign.propertyType.neon | 3 ++- tests/phpstan/baselines/binaryOp.invalid.neon | 3 ++- tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon | 3 ++- tests/phpstan/baselines/booleanNot.alwaysTrue.neon | 3 ++- tests/phpstan/baselines/class.notFound.neon | 3 ++- tests/phpstan/baselines/deadCode.unreachable.neon | 3 ++- tests/phpstan/baselines/empty.property.neon | 3 ++- tests/phpstan/baselines/empty.variable.neon | 3 ++- tests/phpstan/baselines/function.alreadyNarrowedType.neon | 3 ++- tests/phpstan/baselines/function.resultUnused.neon | 3 ++- tests/phpstan/baselines/identical.alwaysFalse.neon | 3 ++- tests/phpstan/baselines/if.alwaysFalse.neon | 3 ++- tests/phpstan/baselines/if.alwaysTrue.neon | 3 ++- tests/phpstan/baselines/isset.property.neon | 3 ++- tests/phpstan/baselines/method.childParameterType.neon | 3 ++- tests/phpstan/baselines/method.unused.neon | 3 ++- tests/phpstan/baselines/notIdentical.alwaysTrue.neon | 3 ++- tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon | 3 ++- tests/phpstan/baselines/offsetAccess.notFound.neon | 3 ++- tests/phpstan/baselines/offsetAssign.valueType.neon | 3 ++- tests/phpstan/baselines/parameterByRef.type.neon | 3 ++- tests/phpstan/baselines/parameterByRef.unusedType.neon | 3 ++- tests/phpstan/baselines/property.defaultValue.neon | 3 ++- tests/phpstan/baselines/property.nonObject.neon | 3 ++- tests/phpstan/baselines/property.notFound.neon | 3 ++- tests/phpstan/baselines/property.private.neon | 3 ++- tests/phpstan/baselines/return.type.neon | 3 ++- tests/phpstan/baselines/varTag.noVariable.neon | 3 ++- tests/phpstan/baselines/variable.undefined.neon | 3 ++- tests/phpstan/generate-baselines.php | 3 ++- 32 files changed, 64 insertions(+), 32 deletions(-) diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index 44c86e3f775f0..dd184d5a12924 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=argument.type # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/argument.unresolvableType.neon b/tests/phpstan/baselines/argument.unresolvableType.neon index d2dbdbf78b558..8bee3995f6ac5 100644 --- a/tests/phpstan/baselines/argument.unresolvableType.neon +++ b/tests/phpstan/baselines/argument.unresolvableType.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=argument.unresolvableType # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/assign.propertyType.neon b/tests/phpstan/baselines/assign.propertyType.neon index 821971cf7ff0f..4afc274001850 100644 --- a/tests/phpstan/baselines/assign.propertyType.neon +++ b/tests/phpstan/baselines/assign.propertyType.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=assign.propertyType # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/binaryOp.invalid.neon b/tests/phpstan/baselines/binaryOp.invalid.neon index 5361d1c1373d4..57b8bf343662e 100644 --- a/tests/phpstan/baselines/binaryOp.invalid.neon +++ b/tests/phpstan/baselines/binaryOp.invalid.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=binaryOp.invalid # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon b/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon index 6f08065e4598e..23523d7b66739 100644 --- a/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon +++ b/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=booleanAnd.leftAlwaysTrue # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/booleanNot.alwaysTrue.neon b/tests/phpstan/baselines/booleanNot.alwaysTrue.neon index f0596cafb4de5..066ee954ba210 100644 --- a/tests/phpstan/baselines/booleanNot.alwaysTrue.neon +++ b/tests/phpstan/baselines/booleanNot.alwaysTrue.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=booleanNot.alwaysTrue # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/class.notFound.neon b/tests/phpstan/baselines/class.notFound.neon index 308b6a3db00bb..423a42dd3aca5 100644 --- a/tests/phpstan/baselines/class.notFound.neon +++ b/tests/phpstan/baselines/class.notFound.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=class.notFound # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/deadCode.unreachable.neon b/tests/phpstan/baselines/deadCode.unreachable.neon index 9c8d9780eeddf..3dccb140aacc0 100644 --- a/tests/phpstan/baselines/deadCode.unreachable.neon +++ b/tests/phpstan/baselines/deadCode.unreachable.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=deadCode.unreachable # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/empty.property.neon b/tests/phpstan/baselines/empty.property.neon index 56c8df0a0f625..869cadbf2fea7 100644 --- a/tests/phpstan/baselines/empty.property.neon +++ b/tests/phpstan/baselines/empty.property.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=empty.property # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/empty.variable.neon b/tests/phpstan/baselines/empty.variable.neon index 37a3c9fda14fd..fe2adb8dec11e 100644 --- a/tests/phpstan/baselines/empty.variable.neon +++ b/tests/phpstan/baselines/empty.variable.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=empty.variable # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/function.alreadyNarrowedType.neon b/tests/phpstan/baselines/function.alreadyNarrowedType.neon index ed05ca3edde93..467991720b5a7 100644 --- a/tests/phpstan/baselines/function.alreadyNarrowedType.neon +++ b/tests/phpstan/baselines/function.alreadyNarrowedType.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=function.alreadyNarrowedType # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/function.resultUnused.neon b/tests/phpstan/baselines/function.resultUnused.neon index 8084a056a9529..442b207fe196d 100644 --- a/tests/phpstan/baselines/function.resultUnused.neon +++ b/tests/phpstan/baselines/function.resultUnused.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=function.resultUnused # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/identical.alwaysFalse.neon b/tests/phpstan/baselines/identical.alwaysFalse.neon index c52fec18de570..5bd777f130a40 100644 --- a/tests/phpstan/baselines/identical.alwaysFalse.neon +++ b/tests/phpstan/baselines/identical.alwaysFalse.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=identical.alwaysFalse # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/if.alwaysFalse.neon b/tests/phpstan/baselines/if.alwaysFalse.neon index 489ab252ce8e9..30fb317dec4c4 100644 --- a/tests/phpstan/baselines/if.alwaysFalse.neon +++ b/tests/phpstan/baselines/if.alwaysFalse.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=if.alwaysFalse # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/if.alwaysTrue.neon b/tests/phpstan/baselines/if.alwaysTrue.neon index 457b233a9bbd0..1d3af11e446fb 100644 --- a/tests/phpstan/baselines/if.alwaysTrue.neon +++ b/tests/phpstan/baselines/if.alwaysTrue.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=if.alwaysTrue # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/isset.property.neon b/tests/phpstan/baselines/isset.property.neon index aed467fb1e958..327cd1c76aa43 100644 --- a/tests/phpstan/baselines/isset.property.neon +++ b/tests/phpstan/baselines/isset.property.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=isset.property # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/method.childParameterType.neon b/tests/phpstan/baselines/method.childParameterType.neon index 67b5e9342f9c6..4c68bab982fab 100644 --- a/tests/phpstan/baselines/method.childParameterType.neon +++ b/tests/phpstan/baselines/method.childParameterType.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=method.childParameterType # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/method.unused.neon b/tests/phpstan/baselines/method.unused.neon index 220d3bdd22908..054dc94542241 100644 --- a/tests/phpstan/baselines/method.unused.neon +++ b/tests/phpstan/baselines/method.unused.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=method.unused # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/notIdentical.alwaysTrue.neon b/tests/phpstan/baselines/notIdentical.alwaysTrue.neon index a8c4874b90177..1a6284b361ec2 100644 --- a/tests/phpstan/baselines/notIdentical.alwaysTrue.neon +++ b/tests/phpstan/baselines/notIdentical.alwaysTrue.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=notIdentical.alwaysTrue # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon b/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon index 8a50c32209afb..6ea0acc46ac2b 100644 --- a/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon +++ b/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=offsetAccess.nonOffsetAccessible # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/offsetAccess.notFound.neon b/tests/phpstan/baselines/offsetAccess.notFound.neon index 61ed8d3d01353..ed127f9ed48cd 100644 --- a/tests/phpstan/baselines/offsetAccess.notFound.neon +++ b/tests/phpstan/baselines/offsetAccess.notFound.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=offsetAccess.notFound # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/offsetAssign.valueType.neon b/tests/phpstan/baselines/offsetAssign.valueType.neon index 82a2c14537d91..6764804f6d4f1 100644 --- a/tests/phpstan/baselines/offsetAssign.valueType.neon +++ b/tests/phpstan/baselines/offsetAssign.valueType.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=offsetAssign.valueType # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/parameterByRef.type.neon b/tests/phpstan/baselines/parameterByRef.type.neon index c14b642e8556c..40c8748b45974 100644 --- a/tests/phpstan/baselines/parameterByRef.type.neon +++ b/tests/phpstan/baselines/parameterByRef.type.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=parameterByRef.type # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/parameterByRef.unusedType.neon b/tests/phpstan/baselines/parameterByRef.unusedType.neon index 8a77b0a01e582..0704b43af2e54 100644 --- a/tests/phpstan/baselines/parameterByRef.unusedType.neon +++ b/tests/phpstan/baselines/parameterByRef.unusedType.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=parameterByRef.unusedType # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/property.defaultValue.neon b/tests/phpstan/baselines/property.defaultValue.neon index 79dcee9573015..6cda21d8d6b20 100644 --- a/tests/phpstan/baselines/property.defaultValue.neon +++ b/tests/phpstan/baselines/property.defaultValue.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=property.defaultValue # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/property.nonObject.neon b/tests/phpstan/baselines/property.nonObject.neon index 90148e9eaa731..e9f1ae8ec3d98 100644 --- a/tests/phpstan/baselines/property.nonObject.neon +++ b/tests/phpstan/baselines/property.nonObject.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=property.nonObject # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/property.notFound.neon b/tests/phpstan/baselines/property.notFound.neon index f8e4ef760b6c7..a3f3247ffce3e 100644 --- a/tests/phpstan/baselines/property.notFound.neon +++ b/tests/phpstan/baselines/property.notFound.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=property.notFound # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/property.private.neon b/tests/phpstan/baselines/property.private.neon index e4aa11d30d16c..20fdc0268cef3 100644 --- a/tests/phpstan/baselines/property.private.neon +++ b/tests/phpstan/baselines/property.private.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=property.private # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/return.type.neon b/tests/phpstan/baselines/return.type.neon index 9e8bd928c43ff..f0f524e3f0dbd 100644 --- a/tests/phpstan/baselines/return.type.neon +++ b/tests/phpstan/baselines/return.type.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=return.type # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/varTag.noVariable.neon b/tests/phpstan/baselines/varTag.noVariable.neon index 30e819cb96cd8..2593db2cafcd1 100644 --- a/tests/phpstan/baselines/varTag.noVariable.neon +++ b/tests/phpstan/baselines/varTag.noVariable.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=varTag.noVariable # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/baselines/variable.undefined.neon b/tests/phpstan/baselines/variable.undefined.neon index e0b9f272fb643..b1eec349cd4d0 100644 --- a/tests/phpstan/baselines/variable.undefined.neon +++ b/tests/phpstan/baselines/variable.undefined.neon @@ -13,7 +13,8 @@ # # composer phpstan:baselines -- --identifier=variable.undefined # -# which reruns the analysis with this file suppressed so the errors surface again. +# which reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. parameters: ignoreErrors: diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 3a9a9c42dd9cc..aa1b50eb98ad9 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -889,7 +889,8 @@ function build_baseline_header( string $identifier ): string { # # composer phpstan:baselines -- --identifier=$identifier # - # which reruns the analysis with this file suppressed so the errors surface again. + # which reruns the analysis with the baselines in this directory suppressed, so + # that their errors surface again, and rewrites only this file. TEXT; } From 5a5af73f7528627c5e45c601230f9319b8a43f55 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:24:13 -0800 Subject: [PATCH 29/38] Build/Test Tools: Refuse a PHPStan baselines output directory holding other configuration. Retiring and pruning already leave alone a file the generator did not write, but a directory holding one was still written to. Its loader would include such a file as a baseline, such as `base.neon` with `--output-dir=tests/phpstan`, and a reported identifier of the same name would overwrite it. The generator now stops before any analysis when the output directory holds a `.neon` file named like a baseline that does not start with the header it writes, naming the files and asking for a directory of its own. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index aa1b50eb98ad9..e44592c0f332f 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -204,6 +204,34 @@ exit( 0 ); } +/* + * A directory holding a `.neon` file named like a baseline that this script did + * not write is not a baselines directory, such as `tests/phpstan` with its + * `base.neon`. Its loader would include that file as a baseline, and a reported + * identifier of the same name would overwrite it, so the run stops before any + * analysis rather than writing there. + */ +if ( is_dir( $output_dir ) ) { + $foreign = array(); + foreach ( list_baselines( $output_dir ) as $name ) { + if ( ! is_generated_baseline( $output_dir . '/' . $name, basename( $name, '.neon' ) ) ) { + $foreign[] = $name; + } + } + + if ( $foreign ) { + fwrite( + STDERR, + sprintf( + "%s holds files named like baselines that this script did not write: %s\nChoose a directory of its own for --output-dir.\n", + $output_option, + implode( ', ', $foreign ) + ) + ); + exit( 1 ); + } +} + /* * Both temporary files sit beside the configuration, and so inside the * repository, for two separate reasons. From e266df111bf7694e4c5fe5df89035a6e98626e8f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:24:32 -0800 Subject: [PATCH 30/38] Build/Test Tools: Cast substr() when building a PHPStan baselines loader's path. On PHP before 8, substr() returns false rather than an empty string when the start is at the end of the string, so PHPStan at level 10 reported passing its result to ltrim(). That cannot happen here, since the shared file's path is always longer than that of a directory holding it, so the result is cast. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index e44592c0f332f..94498feb1b06a 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -702,7 +702,8 @@ function build_loader( string $output_dir ): string { for ( $dir = $from; null === $relative; $dir = dirname( $dir ), $ups++ ) { for ( $base = dirname( $to ); null === $relative; $base = dirname( $base ) ) { if ( is_same_directory( $dir, $base ) ) { - $relative = str_repeat( '../', $ups ) . ltrim( substr( $to, strlen( $base ) ), '/' ); + // substr() gives false on PHP < 8 when $to is no longer than $base, which a directory's own path cannot be. + $relative = str_repeat( '../', $ups ) . ltrim( (string) substr( $to, strlen( $base ) ), '/' ); } if ( dirname( $base ) === $base ) { From d11b05d2a6a27ab2136b1b2fe45f45134ccc680f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:47:19 -0800 Subject: [PATCH 31/38] Build/Test Tools: Recognize a PHPStan baseline's header with CRLF line endings. is_generated_baseline() compared the start of the file with the header's first line including its LF. In a checkout that converts line endings to CRLF, as Git does on Windows with `core.autocrlf`, every committed baseline then failed the check: the generator refused to run on the default directory as one holding files it did not write, and `--prune` left every emptied baseline in place. Only the first line is now read, and compared without its line ending, which also spares reading each baseline in full to compare one line. Co-Authored-By: Claude Opus 5.5 (high effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 94498feb1b06a..54ef441968d7c 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -548,6 +548,10 @@ function get_usage(): string { * `base.neon` with `--output-dir=tests/phpstan`, or the root `phpstan.neon` with * `--output-dir=.`. * + * Only the first line is read, and compared without its line ending, so that a + * checkout converting line endings to CRLF, as Git does on Windows with + * `core.autocrlf`, still recognizes every baseline. + * * @param non-empty-string $file Absolute path to the file. * @param string $identifier Identifier the file is named for. * @return bool Whether the file starts with the header for that identifier. @@ -559,9 +563,18 @@ function is_generated_baseline( string $file, string $identifier ): bool { } $header = build_baseline_header( $identifier ); - $first = substr( $header, 0, (int) strpos( $header, "\n" ) + 1 ); + $first = substr( $header, 0, (int) strpos( $header, "\n" ) ); + + $handle = fopen( $file, 'r' ); + if ( false === $handle ) { + fwrite( STDERR, "Could not read $file\n" ); + exit( 1 ); + } + + $line = fgets( $handle ); + fclose( $handle ); - return 0 === strpos( read_file( $file ), $first ); + return false !== $line && rtrim( $line, "\r\n" ) === $first; } /** From a91ec5edbca3eb68a58d1c1becdec8671442d0e9 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:48:18 -0800 Subject: [PATCH 32/38] Build/Test Tools: Refuse to replace a `loader.php` the PHPStan baselines generator did not write. The check for files the generator did not write only looked at `.neon` files named like baselines. An output directory holding some other `loader.php`, such as another tool's bootstrap, passed it, and the generator then replaced that file with its own loader. The run now also stops before any analysis when the output directory holds a `loader.php` without the statement every generated loader ends in. That statement is kept in LOADER_RETURN, which build_loader() writes and is_generated_loader() looks for. Co-Authored-By: Claude Opus 5.5 (high effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 36 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 54ef441968d7c..5c7dd0b345f9e 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -50,6 +50,13 @@ */ const DEFAULT_OUTPUT_DIR = 'tests/phpstan/baselines'; +/** + * The statement every loader this script writes ends in. + * + * build_loader() writes it, and is_generated_loader() looks for it. + */ +const LOADER_RETURN = 'return \WordPress\PHPStan\get_baseline_includes( __DIR__ );'; + /** * The configuration analyzed when `--config` does not name one. * @@ -209,7 +216,9 @@ * not write is not a baselines directory, such as `tests/phpstan` with its * `base.neon`. Its loader would include that file as a baseline, and a reported * identifier of the same name would overwrite it, so the run stops before any - * analysis rather than writing there. + * analysis rather than writing there. So it does for a `loader.php` that is not + * one this script writes, such as another tool's bootstrap, which writing the + * loader would replace. */ if ( is_dir( $output_dir ) ) { $foreign = array(); @@ -219,11 +228,15 @@ } } + if ( is_file( $output_dir . '/loader.php' ) && ! is_generated_loader( read_file( $output_dir . '/loader.php' ) ) ) { + $foreign[] = 'loader.php'; + } + if ( $foreign ) { fwrite( STDERR, sprintf( - "%s holds files named like baselines that this script did not write: %s\nChoose a directory of its own for --output-dir.\n", + "%s holds files named like baselines, or a loader, that this script did not write: %s\nChoose a directory of its own for --output-dir.\n", $output_option, implode( ', ', $foreign ) ) @@ -751,13 +764,28 @@ function build_loader( string $output_dir ): string { require_once __DIR__ . %s; - return \WordPress\PHPStan\get_baseline_includes( __DIR__ ); + %s PHP, - var_export( '/' . $relative, true ) + var_export( '/' . $relative, true ), + LOADER_RETURN ); } +/** + * Determines whether a file's contents are a loader this script wrote. + * + * Every loader build_loader() writes ends in LOADER_RETURN, whatever the path it + * requires the shared file by, so a `loader.php` without it belongs to something + * else, and is not to be replaced. + * + * @param string $contents Contents of a `loader.php`. + * @return bool Whether it is a loader this script wrote. + */ +function is_generated_loader( string $contents ): bool { + return false !== strpos( $contents, LOADER_RETURN ); +} + /** * Determines whether a baseline ignores no errors. * From 3bd04cd61f74d4438cc69d02f8a40469f404d768 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:49:11 -0800 Subject: [PATCH 33/38] Build/Test Tools: Name the configuration and directory in a PHPStan baseline's regenerate command. Each baseline's header gives the command that regenerates it, which only named the identifier. For baselines generated with a configuration or an output directory other than the default, following it regenerated the default directory instead: the default baseline was rewritten as only the errors the other directory's baselines leave, and the one the header belonged to was not touched. The command now carries `--config` and `--output-dir` where the run used other than the defaults, so the default baselines are unchanged. The header's first line still names only the identifier, which is what is_generated_baseline() recognizes. Co-Authored-By: Claude Opus 5.5 (high effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 38 +++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 5c7dd0b345f9e..d2ff9564ef96e 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -437,10 +437,23 @@ static function () use ( $temp_config, $temp_baseline ): void { ); } +/* + * The command each header gives for regenerating its baseline has to name the + * configuration and the directory this run used, where they are not the + * defaults. Without them, it would regenerate the default directory instead. + */ +$regenerate_options = ''; +if ( DEFAULT_CONFIG !== $config_relative ) { + $regenerate_options .= ' --config=' . quote_shell_argument( $config_option ); +} +if ( ! is_same_directory( $repo_root . '/' . DEFAULT_OUTPUT_DIR, $output_dir ) ) { + $regenerate_options .= ' --output-dir=' . quote_shell_argument( $output_option ); +} + foreach ( $grouped as $identifier => $entries ) { $written = file_put_contents( $output_dir . '/' . $identifier . '.neon', - build_baseline( $entries, $output_dir, build_baseline_header( $identifier ) ) + build_baseline( $entries, $output_dir, build_baseline_header( $identifier, $regenerate_options ) ) ); if ( false === $written ) { @@ -936,13 +949,32 @@ function quote_neon_value( string $value ): string { return "'" . str_replace( "'", "''", $value ) . "'"; } +/** + * Quotes a command-line argument for the regenerate command in a header. + * + * A value of only the characters a path is usually made of is left as it is, + * so that the command reads naturally. Anything else is quoted for the shell. + * + * @param string $value Argument value. + * @return string Value, quoted where needed. + */ +function quote_shell_argument( string $value ): string { + return 1 === preg_match( '#^[A-Za-z0-9_./-]+$#', $value ) ? $value : escapeshellarg( $value ); +} + /** * Builds the header comment for a per-identifier baseline. * + * The first line names only the identifier, so that is_generated_baseline() can + * recognize the file whatever options it was generated with. + * * @param non-falsy-string $identifier Error identifier, a group followed by a code. + * @param string $options Options other than `--identifier` the + * regenerate command needs, each preceded + * by a space. Default empty. * @return non-falsy-string Comment block. */ -function build_baseline_header( string $identifier ): string { +function build_baseline_header( string $identifier, string $options = '' ): string { return << Date: Tue, 17 Nov 2026 13:49:26 -0800 Subject: [PATCH 34/38] Build/Test Tools: Anchor the PHPStan baseline file name pattern at the very end. BASELINE_FILE_PATTERN ended in `$`, which without the `D` modifier also matches before a final newline, so a file named `argument.type.neon` followed by a newline was taken for a baseline. It now ends in `\z`. Co-Authored-By: Claude Opus 5.5 (high effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baseline-includes.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/baseline-includes.php b/tests/phpstan/baseline-includes.php index 4a7b2d249f456..625a4e76a3d43 100644 --- a/tests/phpstan/baseline-includes.php +++ b/tests/phpstan/baseline-includes.php @@ -41,9 +41,10 @@ * * The name is the error identifier, matched as PHPStan validates identifiers, * followed by `.neon`, so that a stray file such as a merge tool's - * `argument.type_BACKUP_1234.neon` is neither loaded nor retired. + * `argument.type_BACKUP_1234.neon` is neither loaded nor retired. It ends in `\z` + * rather than `$`, which would also match a name ending in a newline. */ -const BASELINE_FILE_PATTERN = '/^[a-zA-Z0-9](?:[a-zA-Z0-9.]*[a-zA-Z0-9])?\.neon$/'; +const BASELINE_FILE_PATTERN = '/^[a-zA-Z0-9](?:[a-zA-Z0-9.]*[a-zA-Z0-9])?\.neon\z/'; /** * Lists the per-identifier baselines present on disk. From 5e5fafa1639510ae2c32564f465df7f5389287ec Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 13:50:00 -0800 Subject: [PATCH 35/38] Build/Test Tools: Drop the PHPStan baselines retire loop's own header check. A run now stops before its analysis when the output directory holds a file named like a baseline that the generator did not write, so the retire loop's check that each file it deletes is one the generator wrote could no longer find one. It is removed, and is_generated_baseline() says where it is used instead. Co-Authored-By: Claude Opus 5.5 (high effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index d2ff9564ef96e..3a5c4479ef2c9 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -491,10 +491,9 @@ static function () use ( $temp_config, $temp_baseline ): void { continue; } + // Every file here was checked to be one this script wrote before the analysis. $file = $output_dir . '/' . $identifier . '.neon'; - if ( is_file( $file ) && ! is_generated_baseline( $file, $identifier ) ) { - fwrite( STDERR, sprintf( "%s: not a baseline written by this script, left untouched.\n", $output_option . '/' . $identifier . '.neon' ) ); - } elseif ( is_file( $file ) && unlink( $file ) ) { + if ( is_file( $file ) && unlink( $file ) ) { printf( "%s: no errors remain, file deleted.\n", $output_option . '/' . $identifier . '.neon' ); } else { printf( "%s: no errors reported.\n", $identifier ); @@ -568,11 +567,12 @@ function get_usage(): string { /** * Determines whether a file is a baseline this script wrote. * - * A file is only retired or pruned when it starts with the header this script - * writes for its identifier. Any `.neon` file whose name passes for an identifier - * would otherwise be deleted when that identifier reports nothing, such as - * `base.neon` with `--output-dir=tests/phpstan`, or the root `phpstan.neon` with - * `--output-dir=.`. + * A file is only written, retired, or pruned when it starts with the header this + * script writes for its identifier: a run stops before its analysis when the + * output directory holds any other, and `--prune` leaves any other in place. Any + * `.neon` file whose name passes for an identifier would otherwise be deleted + * when that identifier reports nothing, such as `base.neon` with + * `--output-dir=tests/phpstan`, or the root `phpstan.neon` with `--output-dir=.`. * * Only the first line is read, and compared without its line ending, so that a * checkout converting line endings to CRLF, as Git does on Windows with From 76d02fad3fdbe73edc308107356216e9e0ab1d10 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 14:47:24 -0800 Subject: [PATCH 36/38] Build/Test Tools: Drop the PHPStan baselines hardening against misuse. The baselines tooling had grown guards against ways of running it that the workflows it serves never do, all of them only reachable by pointing it at the wrong place, and all of them recoverable from version control: * is_same_directory(), comparing directories by inode so that an `--output-dir` typed in the wrong case on a case-insensitive file system still matched the loader. The loader now compares the real path it is given with its __DIR__, and build_loader() works out its require path with get_relative_path() between the real paths, rather than by walking the ancestors of both. * The check before the analysis that the output directory holds no `.neon` file or `loader.php` the generator did not write, along with is_generated_baseline(), its handling of CRLF line endings, is_generated_loader(), LOADER_RETURN, and `--prune`'s own header check. * quote_shell_argument(), for the regenerate command in a header, whose options are repository-relative paths. * list_baselines(), which turned find_baselines()'s exception into a message. The uncaught exception carries the same message. Co-Authored-By: Claude Opus 5.5 (high effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/baseline-includes.php | 45 +----- tests/phpstan/generate-baselines.php | 203 +++------------------------ 2 files changed, 23 insertions(+), 225 deletions(-) diff --git a/tests/phpstan/baseline-includes.php b/tests/phpstan/baseline-includes.php index 625a4e76a3d43..c4a68eb381cda 100644 --- a/tests/phpstan/baseline-includes.php +++ b/tests/phpstan/baseline-includes.php @@ -23,9 +23,9 @@ * * `composer phpstan:baselines` sets it to the directory it is writing baselines * to while it analyzes, so that the errors those baselines suppress are reported - * again for it to write back out. Each loader compares it with its own directory, - * with is_same_directory(), so this works however the directory is spelled, and - * however the configuration includes the loader, even through another + * again for it to write back out. It is the directory's real path, which is what + * each loader's __DIR__ is, and each loader compares it with its own, so this + * works however the configuration includes the loader, even through another * configuration that includes the one that includes it. * * The baselines in any other directory stay in force. Those regenerated are then @@ -79,42 +79,6 @@ function find_baselines( string $directory ): array { return $names; } -/** - * Determines whether two paths name the same directory. - * - * They are compared by the device and inode the file system reports, not by how - * they are spelled. realpath() resolves symbolic links but keeps the case it was - * given, so on a case-insensitive file system `Tests/PHPStan/Baselines` and - * `tests/phpstan/baselines` resolve to two strings naming one directory. Were - * the loader's check to miss that, the baselines being regenerated would stay in - * force, report nothing, and all be retired. - * - * A file system that reports no inode, as some on Windows do, falls back to - * comparing the resolved paths. - * - * @param string $a A path. - * @param string $b Another path. - * @return bool Whether both are directories, and the same one. - */ -function is_same_directory( string $a, string $b ): bool { - if ( '' === $a || '' === $b || ! is_dir( $a ) || ! is_dir( $b ) ) { - return false; - } - - $stat_a = stat( $a ); - $stat_b = stat( $b ); - - if ( false === $stat_a || false === $stat_b ) { - return false; - } - - if ( 0 === $stat_a['ino'] || 0 === $stat_b['ino'] ) { - return realpath( $a ) === realpath( $b ); - } - - return $stat_a['dev'] === $stat_b['dev'] && $stat_a['ino'] === $stat_b['ino']; -} - /** * Returns the configuration a baselines loader hands to PHPStan. * @@ -131,8 +95,7 @@ function is_same_directory( string $a, string $b ): bool { * @throws \RuntimeException When the directory cannot be listed. */ function get_baseline_includes( string $directory ): array { - $generating = getenv( GENERATING_BASELINES_ENV ); - if ( false !== $generating && is_same_directory( $generating, $directory ) ) { + if ( getenv( GENERATING_BASELINES_ENV ) === $directory ) { return array( 'includes' => array() ); } diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 3a5c4479ef2c9..b2a39b9a213c7 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -50,13 +50,6 @@ */ const DEFAULT_OUTPUT_DIR = 'tests/phpstan/baselines'; -/** - * The statement every loader this script writes ends in. - * - * build_loader() writes it, and is_generated_loader() looks for it. - */ -const LOADER_RETURN = 'return \WordPress\PHPStan\get_baseline_includes( __DIR__ );'; - /** * The configuration analyzed when `--config` does not name one. * @@ -170,20 +163,14 @@ $pruned = 0; // A directory not written to yet has no baselines to prune. - foreach ( is_dir( $output_dir ) ? list_baselines( $output_dir ) : array() as $name ) { + foreach ( is_dir( $output_dir ) ? find_baselines( $output_dir ) : array() as $name ) { $identifier = basename( $name, '.neon' ); if ( $only_identifiers && ! in_array( $identifier, $only_identifiers, true ) ) { continue; } - $file = $output_dir . '/' . $name; - - if ( ! is_generated_baseline( $file, $identifier ) ) { - fwrite( STDERR, sprintf( "%s: not a baseline written by this script, left untouched.\n", $output_option . '/' . $name ) ); - continue; - } - + $file = $output_dir . '/' . $name; $is_empty = is_empty_baseline( read_file( $file ) ); if ( null === $is_empty ) { @@ -211,40 +198,6 @@ exit( 0 ); } -/* - * A directory holding a `.neon` file named like a baseline that this script did - * not write is not a baselines directory, such as `tests/phpstan` with its - * `base.neon`. Its loader would include that file as a baseline, and a reported - * identifier of the same name would overwrite it, so the run stops before any - * analysis rather than writing there. So it does for a `loader.php` that is not - * one this script writes, such as another tool's bootstrap, which writing the - * loader would replace. - */ -if ( is_dir( $output_dir ) ) { - $foreign = array(); - foreach ( list_baselines( $output_dir ) as $name ) { - if ( ! is_generated_baseline( $output_dir . '/' . $name, basename( $name, '.neon' ) ) ) { - $foreign[] = $name; - } - } - - if ( is_file( $output_dir . '/loader.php' ) && ! is_generated_loader( read_file( $output_dir . '/loader.php' ) ) ) { - $foreign[] = 'loader.php'; - } - - if ( $foreign ) { - fwrite( - STDERR, - sprintf( - "%s holds files named like baselines, or a loader, that this script did not write: %s\nChoose a directory of its own for --output-dir.\n", - $output_option, - implode( ', ', $foreign ) - ) - ); - exit( 1 ); - } -} - /* * Both temporary files sit beside the configuration, and so inside the * repository, for two separate reasons. @@ -351,10 +304,11 @@ static function () use ( $temp_config, $temp_baseline ): void { fwrite( STDERR, "Analyzing with $config_option, baselines in $output_option suppressed...\n" ); /* - * PHPStan inherits it, as do the worker processes it starts. An output directory - * not created yet holds no loader to match, so it is not set at all then. It is - * cleared first either way, so that a value exported in the shell, perhaps naming - * another directory, cannot switch off baselines this run means to keep. + * PHPStan inherits it, as do the worker processes it starts. It is the real path, + * since that is what the loader's __DIR__ is. An output directory not created yet + * holds no loader to match, so it is not set at all then. It is cleared first + * either way, so that a value exported in the shell, perhaps naming another + * directory, cannot switch off baselines this run means to keep. */ putenv( GENERATING_BASELINES_ENV ); $resolved_output_dir = realpath( $output_dir ); @@ -362,6 +316,8 @@ static function () use ( $temp_config, $temp_baseline ): void { putenv( GENERATING_BASELINES_ENV . '=' . $resolved_output_dir ); } +$is_default_output_dir = DEFAULT_OUTPUT_DIR === trim( normalize_path( $output_option ), '/' ); + /* * The baselines in any other directory the configuration includes stay in force, * so a run expecting every error, rather than those the others leave, would come @@ -369,9 +325,8 @@ static function () use ( $temp_config, $temp_baseline ): void { * default directory's, so that is the one combination with nothing to say. Any * other, whether a configuration including the default one alongside a loader of * its own, or a directory other than the default, may well leave some in force. - * The directories are compared as the loader compares them. */ -if ( DEFAULT_CONFIG !== $config_relative || ! is_same_directory( $repo_root . '/' . DEFAULT_OUTPUT_DIR, $output_dir ) ) { +if ( DEFAULT_CONFIG !== $config_relative || ! $is_default_output_dir ) { fwrite( STDERR, "Baselines in any other directory the configuration includes stay in force, so only the errors they leave are written.\n" ); } @@ -444,10 +399,10 @@ static function () use ( $temp_config, $temp_baseline ): void { */ $regenerate_options = ''; if ( DEFAULT_CONFIG !== $config_relative ) { - $regenerate_options .= ' --config=' . quote_shell_argument( $config_option ); + $regenerate_options .= ' --config=' . $config_option; } -if ( ! is_same_directory( $repo_root . '/' . DEFAULT_OUTPUT_DIR, $output_dir ) ) { - $regenerate_options .= ' --output-dir=' . quote_shell_argument( $output_option ); +if ( ! $is_default_output_dir ) { + $regenerate_options .= ' --output-dir=' . $output_option; } foreach ( $grouped as $identifier => $entries ) { @@ -481,7 +436,7 @@ static function () use ( $temp_config, $temp_baseline ): void { $retired = $only_identifiers; if ( ! $only_identifiers ) { - foreach ( list_baselines( $output_dir ) as $name ) { + foreach ( find_baselines( $output_dir ) as $name ) { $retired[] = basename( $name, '.neon' ); } } @@ -491,7 +446,6 @@ static function () use ( $temp_config, $temp_baseline ): void { continue; } - // Every file here was checked to be one this script wrote before the analysis. $file = $output_dir . '/' . $identifier . '.neon'; if ( is_file( $file ) && unlink( $file ) ) { printf( "%s: no errors remain, file deleted.\n", $output_option . '/' . $identifier . '.neon' ); @@ -564,63 +518,6 @@ function get_usage(): string { TEXT; } -/** - * Determines whether a file is a baseline this script wrote. - * - * A file is only written, retired, or pruned when it starts with the header this - * script writes for its identifier: a run stops before its analysis when the - * output directory holds any other, and `--prune` leaves any other in place. Any - * `.neon` file whose name passes for an identifier would otherwise be deleted - * when that identifier reports nothing, such as `base.neon` with - * `--output-dir=tests/phpstan`, or the root `phpstan.neon` with `--output-dir=.`. - * - * Only the first line is read, and compared without its line ending, so that a - * checkout converting line endings to CRLF, as Git does on Windows with - * `core.autocrlf`, still recognizes every baseline. - * - * @param non-empty-string $file Absolute path to the file. - * @param string $identifier Identifier the file is named for. - * @return bool Whether the file starts with the header for that identifier. - */ -function is_generated_baseline( string $file, string $identifier ): bool { - // Neither can name a baseline, nor have a header built for it. - if ( '' === $identifier || '0' === $identifier ) { - return false; - } - - $header = build_baseline_header( $identifier ); - $first = substr( $header, 0, (int) strpos( $header, "\n" ) ); - - $handle = fopen( $file, 'r' ); - if ( false === $handle ) { - fwrite( STDERR, "Could not read $file\n" ); - exit( 1 ); - } - - $line = fgets( $handle ); - fclose( $handle ); - - return false !== $line && rtrim( $line, "\r\n" ) === $first; -} - -/** - * Lists the baselines in a directory, stopping the run if it cannot be listed. - * - * find_baselines() throws, which stops PHPStan when a loader calls it. Here the - * same failure is reported the way the rest of this script reports one. - * - * @param non-empty-string $output_dir Absolute path to the baseline directory. - * @return list File names, sorted. - */ -function list_baselines( string $output_dir ): array { - try { - return find_baselines( $output_dir ); - } catch ( \RuntimeException $e ) { - fwrite( STDERR, $e->getMessage() . "\n" ); - exit( 1 ); - } -} - /** * Reads a file, failing loudly rather than continuing with false. * @@ -712,18 +609,11 @@ function read_baseline( string $path ): array { * there, so a fix to that reaches every directory's loader alike. * `baselines/loader.php` is the one this builds for the default directory. * - * The relative path is worked out between the resolved directories, since that is - * how the loader's __DIR__ is resolved when the path is followed. Worked out from - * the directory as given, a symbolic link in it would send the loader looking + * The relative path is worked out between the real paths, since that is how the + * loader's __DIR__ is resolved when the path is followed. Worked out from the + * directory as given, a symbolic link in it would send the loader looking * somewhere else. * - * Their common ancestor is found with is_same_directory() rather than by - * comparing the paths as strings, since realpath() keeps the case it was given. - * On a case-insensitive file system, `--output-dir=Tests/PHPStan/Baselines` - * would otherwise share no ancestor with `tests/phpstan` short of the repository - * root, and the loader written for the default directory would change with the - * spelling of the option. - * * @param non-empty-string $output_dir Absolute path to the baseline directory, which exists. * @return non-falsy-string Loader contents. */ @@ -736,30 +626,6 @@ function build_loader( string $output_dir ): string { exit( 1 ); } - $relative = null; - $ups = 0; - for ( $dir = $from; null === $relative; $dir = dirname( $dir ), $ups++ ) { - for ( $base = dirname( $to ); null === $relative; $base = dirname( $base ) ) { - if ( is_same_directory( $dir, $base ) ) { - // substr() gives false on PHP < 8 when $to is no longer than $base, which a directory's own path cannot be. - $relative = str_repeat( '../', $ups ) . ltrim( (string) substr( $to, strlen( $base ) ), '/' ); - } - - if ( dirname( $base ) === $base ) { - break; - } - } - - if ( dirname( $dir ) === $dir ) { - break; - } - } - - if ( null === $relative ) { - fwrite( STDERR, "Could not relate $output_dir to the shared baseline includes.\n" ); - exit( 1 ); - } - return sprintf( <<<'PHP' Date: Tue, 17 Nov 2026 14:55:03 -0800 Subject: [PATCH 37/38] Build/Test Tools: Say which PHPStan baselines the generator suppresses. The generator's file docblock still said it analyzes with the existing baselines suppressed. Only those being regenerated are; the rest stay in force. Co-Authored-By: Claude Opus 5.5 (high effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 7fbbd88f-e571-4959-a07e-802b483fab5a --- tests/phpstan/generate-baselines.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index b2a39b9a213c7..5a4ee33d9d5f9 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -7,7 +7,7 @@ * refreshing one baseline among several: rerunning it would sweep every other * kind of error into the same file. * - * This wrapper runs the analysis with the existing per-identifier baselines + * This wrapper runs the analysis with the baselines being regenerated * suppressed, so their errors surface again, then splits the result by * identifier and writes one file per kind. Each file is self-describing and is * meant to shrink to nothing and then be deleted. From aaa1cd18e502de11f3e2ddcd39a272bfec058aef Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Nov 2026 15:01:57 -0800 Subject: [PATCH 38/38] Use import RuntimeException --- tests/phpstan/baseline-includes.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/phpstan/baseline-includes.php b/tests/phpstan/baseline-includes.php index c4a68eb381cda..5086ffab34017 100644 --- a/tests/phpstan/baseline-includes.php +++ b/tests/phpstan/baseline-includes.php @@ -18,6 +18,8 @@ namespace WordPress\PHPStan; +use RuntimeException; + /** * Environment variable that tells a baselines loader to include nothing. * @@ -60,13 +62,13 @@ * @param non-empty-string $directory Absolute path to the baseline directory. * @return list File names, sorted. * - * @throws \RuntimeException When the directory cannot be listed. + * @throws RuntimeException When the directory cannot be listed. */ function find_baselines( string $directory ): array { $entries = is_dir( $directory ) ? scandir( $directory ) : false; if ( false === $entries ) { - throw new \RuntimeException( "Could not list the PHPStan baselines in $directory." ); + throw new RuntimeException( "Could not list the PHPStan baselines in $directory." ); } $names = array(); @@ -92,7 +94,7 @@ function find_baselines( string $directory ): array { * @param non-empty-string $directory The loader's directory, as its __DIR__. * @return array{includes: list} Configuration. * - * @throws \RuntimeException When the directory cannot be listed. + * @throws RuntimeException When the directory cannot be listed. */ function get_baseline_includes( string $directory ): array { if ( getenv( GENERATING_BASELINES_ENV ) === $directory ) {