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/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..7dd1df431d4a9 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 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: ```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/baseline-includes.php b/tests/phpstan/baseline-includes.php new file mode 100644 index 0000000000000..5086ffab34017 --- /dev/null +++ b/tests/phpstan/baseline-includes.php @@ -0,0 +1,105 @@ + File names, sorted. + * + * @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." ); + } + + $names = array(); + foreach ( $entries as $name ) { + if ( '' !== $name && 1 === preg_match( BASELINE_FILE_PATTERN, $name ) ) { + $names[] = $name; + } + } + + return $names; +} + +/** + * 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. + * + * @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() ); + } + + return array( 'includes' => find_baselines( $directory ) ); +} diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index aece929f18751..dd184d5a12924 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -7,14 +7,14 @@ # 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 # # 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 3e8cdcb76c85d..8bee3995f6ac5 100644 --- a/tests/phpstan/baselines/argument.unresolvableType.neon +++ b/tests/phpstan/baselines/argument.unresolvableType.neon @@ -7,14 +7,14 @@ # 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 # # 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 80b4b8b9b43c1..4afc274001850 100644 --- a/tests/phpstan/baselines/assign.propertyType.neon +++ b/tests/phpstan/baselines/assign.propertyType.neon @@ -7,14 +7,14 @@ # 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 # # 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 167f159cffb34..57b8bf343662e 100644 --- a/tests/phpstan/baselines/binaryOp.invalid.neon +++ b/tests/phpstan/baselines/binaryOp.invalid.neon @@ -7,14 +7,14 @@ # 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 # # 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 10ed28f6b433d..23523d7b66739 100644 --- a/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon +++ b/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon @@ -7,14 +7,14 @@ # 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 # # 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 b61a98cef358e..066ee954ba210 100644 --- a/tests/phpstan/baselines/booleanNot.alwaysTrue.neon +++ b/tests/phpstan/baselines/booleanNot.alwaysTrue.neon @@ -7,14 +7,14 @@ # 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 # # 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 560ceafb56573..423a42dd3aca5 100644 --- a/tests/phpstan/baselines/class.notFound.neon +++ b/tests/phpstan/baselines/class.notFound.neon @@ -7,14 +7,14 @@ # 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 # # 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 ef8ca56577602..3dccb140aacc0 100644 --- a/tests/phpstan/baselines/deadCode.unreachable.neon +++ b/tests/phpstan/baselines/deadCode.unreachable.neon @@ -7,14 +7,14 @@ # 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 # # 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 5bc6d4e3cbdd3..869cadbf2fea7 100644 --- a/tests/phpstan/baselines/empty.property.neon +++ b/tests/phpstan/baselines/empty.property.neon @@ -7,14 +7,14 @@ # 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 # # 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 c9a94ecd02d9f..fe2adb8dec11e 100644 --- a/tests/phpstan/baselines/empty.variable.neon +++ b/tests/phpstan/baselines/empty.variable.neon @@ -7,14 +7,14 @@ # 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 # # 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 a7c36b7d7d0b8..467991720b5a7 100644 --- a/tests/phpstan/baselines/function.alreadyNarrowedType.neon +++ b/tests/phpstan/baselines/function.alreadyNarrowedType.neon @@ -7,14 +7,14 @@ # 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 # # 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 1ad6a84dbfa4c..442b207fe196d 100644 --- a/tests/phpstan/baselines/function.resultUnused.neon +++ b/tests/phpstan/baselines/function.resultUnused.neon @@ -7,14 +7,14 @@ # 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 # # 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 d8d766d45827f..5bd777f130a40 100644 --- a/tests/phpstan/baselines/identical.alwaysFalse.neon +++ b/tests/phpstan/baselines/identical.alwaysFalse.neon @@ -7,14 +7,14 @@ # 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 # # 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 15f01b77b4cf4..30fb317dec4c4 100644 --- a/tests/phpstan/baselines/if.alwaysFalse.neon +++ b/tests/phpstan/baselines/if.alwaysFalse.neon @@ -7,14 +7,14 @@ # 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 # # 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 6f5169ee0b1e6..1d3af11e446fb 100644 --- a/tests/phpstan/baselines/if.alwaysTrue.neon +++ b/tests/phpstan/baselines/if.alwaysTrue.neon @@ -7,14 +7,14 @@ # 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 # # 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 5278005df2869..327cd1c76aa43 100644 --- a/tests/phpstan/baselines/isset.property.neon +++ b/tests/phpstan/baselines/isset.property.neon @@ -7,14 +7,14 @@ # 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 # # 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/loader.php b/tests/phpstan/baselines/loader.php new file mode 100644 index 0000000000000..829e4ac4880d4 --- /dev/null +++ b/tests/phpstan/baselines/loader.php @@ -0,0 +1,16 @@ + $entries ) { - file_put_contents( + $written = 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, $regenerate_options ) ) ); + 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', @@ -359,8 +436,8 @@ static function () use ( $temp_config, $temp_stripped, $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' ); } } @@ -377,8 +454,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 +463,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 [-- ] @@ -406,14 +481,16 @@ 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 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. --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. @@ -470,6 +547,15 @@ function read_file( string $path ): string { function read_baseline( string $path ): array { $data = require $path; + /* + * 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; @@ -516,95 +602,52 @@ function read_baseline( string $path ): array { } /** - * 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. + * Builds the loader for a baselines directory. * - * 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. + * 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. * - * 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. + * 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. * - * @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. + * @param non-empty-string $output_dir Absolute path to the baseline directory, which exists. + * @return non-falsy-string Loader contents. */ -function strip_baseline_includes( string $config_path, string $output_dir ): string { - $config_dir = dirname( $config_path ); - $in_block = false; - $in_managed = 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; - $in_managed = false; - } - - if ( $in_block ) { - if ( BASELINES_START_MARKER === trim( $line ) ) { - $in_managed = true; - } elseif ( BASELINES_END_MARKER === trim( $line ) ) { - $in_managed = false; - } - } - - if ( $in_block && 1 === preg_match( '/^\s*-\s*(\S+)\s*$/', $line, $matches ) ) { - if ( $in_managed ) { - continue; - } - - $included = $matches[1]; - $absolute = ( '/' === $included[0] ) ? $included : $config_dir . '/' . $included; - - if ( 0 === strpos( normalize_path( $absolute ), normalize_path( $output_dir ) . '/' ) ) { - continue; - } - } +function build_loader( string $output_dir ): string { + $from = realpath( $output_dir ); + $to = realpath( __DIR__ . '/baseline-includes.php' ); - $kept[] = $line; + if ( false === $from || false === $to ) { + fwrite( STDERR, "Could not resolve $output_dir or the shared baseline includes.\n" ); + exit( 1 ); } - return implode( "\n", $kept ); -} - -/** - * Lists the per-identifier baselines present on disk. - * - * @param non-empty-string $output_dir Absolute path to the baseline directory. - * @return list Absolute paths, sorted by name. - */ -function find_baselines( string $output_dir ): array { - $found = glob( $output_dir . '/*.neon' ); - - if ( false === $found ) { - return array(); - } + return sprintf( + <<<'PHP' + $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 +804,12 @@ 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. + * @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 $config ): string { +function build_baseline_header( string $identifier, string $options = '' ): string { return <<