From d2bce4b5ae165ecbe5e773f4eb2579ae8b1ff09e Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Mon, 9 Nov 2026 16:30:48 +0000 Subject: [PATCH 01/15] Build/Test Tools: Add tests for the PHPStan extensions. Adds a PHPUnit suite for the custom PHPStan extensions in `tests/phpstan`, built on PHPStan's test harness. The suite runs with `composer phpstan:test` or `npm run typecheck:php:test`, and runs in CI ahead of the static analysis. See #66634. --- .github/workflows/phpstan-static-analysis.yml | 3 + .../reusable-phpstan-static-analysis-v1.yml | 7 + composer.json | 1 + package.json | 1 + tests/phpstan/README.md | 18 + ...DynamicFunctionReturnTypeExtensionTest.php | 49 ++ .../Tests/GlobalDocBlockVisitorTest.php | 49 ++ tests/phpstan/Tests/HashNotationTypeTest.php | 47 ++ .../phpstan/Tests/HashNotationVisitorTest.php | 535 ++++++++++++++++++ tests/phpstan/Tests/HookDocBlockTest.php | 104 ++++ tests/phpstan/Tests/HookDocsVisitorTest.php | 165 ++++++ .../Tests/HookDocumentationRuleTest.php | 131 +++++ .../phpstan/Tests/HookParamCountRuleTest.php | 76 +++ tests/phpstan/Tests/bootstrap.php | 17 + .../Tests/data/ApplyFiltersTestClass.php | 75 +++ .../Tests/data/GlobalDocBlockTestClass.php | 49 ++ tests/phpstan/Tests/data/apply-filters.php | 205 +++++++ tests/phpstan/Tests/data/functions.php | 19 + tests/phpstan/Tests/data/global-docblock.php | 124 ++++ tests/phpstan/Tests/data/hash-notation.php | 118 ++++ .../phpstan/Tests/data/hook-documentation.php | 121 ++++ tests/phpstan/Tests/data/hook-param-count.php | 144 +++++ tests/phpstan/Tests/data/outside.php | 20 + .../Tests/data/wordpress/wp-admin/admin.php | 22 + .../wp-content/themes/wptests/author.php | 13 + .../wp-content/themes/wptests/index.php | 18 + .../data/wordpress/wp-includes/canonical.php | 61 ++ .../data/wordpress/wp-includes/other.php | 20 + tests/phpstan/Tests/tests.neon | 49 ++ tests/phpstan/base.neon | 3 + tests/phpstan/phpunit.xml.dist | 28 + 31 files changed, 2292 insertions(+) create mode 100644 tests/phpstan/Tests/ApplyFiltersDynamicFunctionReturnTypeExtensionTest.php create mode 100644 tests/phpstan/Tests/GlobalDocBlockVisitorTest.php create mode 100644 tests/phpstan/Tests/HashNotationTypeTest.php create mode 100644 tests/phpstan/Tests/HashNotationVisitorTest.php create mode 100644 tests/phpstan/Tests/HookDocBlockTest.php create mode 100644 tests/phpstan/Tests/HookDocsVisitorTest.php create mode 100644 tests/phpstan/Tests/HookDocumentationRuleTest.php create mode 100644 tests/phpstan/Tests/HookParamCountRuleTest.php create mode 100644 tests/phpstan/Tests/bootstrap.php create mode 100644 tests/phpstan/Tests/data/ApplyFiltersTestClass.php create mode 100644 tests/phpstan/Tests/data/GlobalDocBlockTestClass.php create mode 100644 tests/phpstan/Tests/data/apply-filters.php create mode 100644 tests/phpstan/Tests/data/functions.php create mode 100644 tests/phpstan/Tests/data/global-docblock.php create mode 100644 tests/phpstan/Tests/data/hash-notation.php create mode 100644 tests/phpstan/Tests/data/hook-documentation.php create mode 100644 tests/phpstan/Tests/data/hook-param-count.php create mode 100644 tests/phpstan/Tests/data/outside.php create mode 100644 tests/phpstan/Tests/data/wordpress/wp-admin/admin.php create mode 100644 tests/phpstan/Tests/data/wordpress/wp-content/themes/wptests/author.php create mode 100644 tests/phpstan/Tests/data/wordpress/wp-content/themes/wptests/index.php create mode 100644 tests/phpstan/Tests/data/wordpress/wp-includes/canonical.php create mode 100644 tests/phpstan/Tests/data/wordpress/wp-includes/other.php create mode 100644 tests/phpstan/Tests/tests.neon create mode 100644 tests/phpstan/phpunit.xml.dist diff --git a/.github/workflows/phpstan-static-analysis.yml b/.github/workflows/phpstan-static-analysis.yml index 3258b56ff3baa..1c461a9ac54dd 100644 --- a/.github/workflows/phpstan-static-analysis.yml +++ b/.github/workflows/phpstan-static-analysis.yml @@ -19,6 +19,9 @@ on: - 'phpstan.neon.dist' - 'tests/phpstan/base.neon' - 'tests/phpstan/baselines/**' + # These files configure the tests of the PHPStan extensions. + - 'tests/phpstan/phpunit.xml.dist' + - 'tests/phpstan/Tests/tests.neon' # Confirm any changes to relevant workflow files. - '.github/workflows/phpstan-static-analysis.yml' - '.github/workflows/reusable-phpstan-static-analysis-v1.yml' diff --git a/.github/workflows/reusable-phpstan-static-analysis-v1.yml b/.github/workflows/reusable-phpstan-static-analysis-v1.yml index 321f98189cdf9..843000bb660e0 100644 --- a/.github/workflows/reusable-phpstan-static-analysis-v1.yml +++ b/.github/workflows/reusable-phpstan-static-analysis-v1.yml @@ -30,6 +30,7 @@ jobs: # - Logs debug information. # - Installs Composer dependencies. # - Make Composer packages available globally. + # - Runs the tests of the WordPress-specific PHPStan extensions. # - Builds WordPress. # - Configures caching for PHPStan static analysis scans. # - Runs PHPStan static analysis. @@ -75,6 +76,12 @@ jobs: - name: Make Composer packages available globally run: echo "${PWD}/vendor/bin" >> "$GITHUB_PATH" + # The extensions in `tests/phpstan` are tested against fixtures rather than against + # core, so a defect in one is reported here as a failing test rather than as a + # change in what the analysis below reports. + - name: Run the tests of the PHPStan extensions + run: composer run phpstan:test + # The directory holds more than the analysis results. PHPStan also stores what it read out of # each source file there, the docblocks and signatures it found, keyed by that file's contents # and nothing else. The extensions in `tests/phpstan` change what reading a file yields without diff --git a/composer.json b/composer.json index e20f4c7806b6e..81037a4be45cb 100644 --- a/composer.json +++ b/composer.json @@ -77,6 +77,7 @@ "scripts": { "phpstan": "@php ./vendor/bin/phpstan analyse --memory-limit=2G", "phpstan:baselines": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/generate-baselines.php" ], + "phpstan:test": "@php ./vendor/phpunit/phpunit/phpunit -c tests/phpstan/phpunit.xml.dist", "compat": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source", "format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source", "lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source", diff --git a/package.json b/package.json index d8e955f0d9809..130c7b9278843 100644 --- a/package.json +++ b/package.json @@ -154,6 +154,7 @@ "test:visual": "wp-scripts test-playwright --config tests/visual-regression/playwright.config.js", "typecheck:php": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan --", "typecheck:php:baselines": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:baselines --", + "typecheck:php:test": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:test --", "gutenberg:copy": "node tools/gutenberg/copy.js", "gutenberg:verify": "node tools/gutenberg/utils.js", "gutenberg:download": "node tools/gutenberg/download.js && npm run build:gutenberg" diff --git a/tests/phpstan/README.md b/tests/phpstan/README.md index e263cd409036d..fbf8317779ba9 100644 --- a/tests/phpstan/README.md +++ b/tests/phpstan/README.md @@ -186,6 +186,24 @@ These identifiers are specific to WordPress, and can be ignored or baselined lik | `wordpress.hookDocReferenceHookMissing` | The referenced file exists, but documents no hook of that name. Either the reference is stale, or the canonical docblock has moved. | | `wordpress.hookParamCountMismatch` | The call passes a different number of arguments than the docblock documents `@param` tags for. Passing fewer risks an `ArgumentCountError` in a callback registered for the documented count; passing more silently drops the extra argument and leaves the documentation misleading. | +### Testing the extensions + +The extensions have tests of their own under [`Tests/`](Tests), built on PHPStan's test harness. The two rules are run over fixture files and the errors they report are compared with the ones expected. The visitors and the return type extension are checked through the types PHPStan infers for the `assertType()` calls in their fixtures. Reference comments resolve against a small WordPress tree of fixtures in [`Tests/data/wordpress`](Tests/data/wordpress) rather than against `src`, so a broken reference can be tested without one existing in core. + +Run them with: + +```bash +npm run typecheck:php:test +``` + +or, outside the Docker environment: + +```bash +composer phpstan:test +``` + +They also run in CI, ahead of the analysis itself. + ## Ignoring and baselining errors As we adopt PHPStan iteratively, you may be faced with false positives due to legacy code, or code that is not worth changing at this time. diff --git a/tests/phpstan/Tests/ApplyFiltersDynamicFunctionReturnTypeExtensionTest.php b/tests/phpstan/Tests/ApplyFiltersDynamicFunctionReturnTypeExtensionTest.php new file mode 100644 index 0000000000000..b4169ad0cdc8e --- /dev/null +++ b/tests/phpstan/Tests/ApplyFiltersDynamicFunctionReturnTypeExtensionTest.php @@ -0,0 +1,49 @@ + + */ + public function data_file_asserts(): iterable { + yield from self::gatherAssertTypes( __DIR__ . '/data/apply-filters.php' ); + yield from self::gatherAssertTypes( __DIR__ . '/data/ApplyFiltersTestClass.php' ); + } + + /** + * @dataProvider data_file_asserts + * + * @param string $assert_type Kind of assertion. + * @param string $file Fixture file. + * @param mixed ...$args Assertion arguments. + */ + public function test_file_asserts( string $assert_type, string $file, ...$args ): void { + $this->assertFileAsserts( $assert_type, $file, ...$args ); + } + + /** + * Returns the configuration registering the extension and the visitors it relies on. + * + * @return list + */ + public static function getAdditionalConfigFiles(): array { + return array( __DIR__ . '/tests.neon' ); + } +} diff --git a/tests/phpstan/Tests/GlobalDocBlockVisitorTest.php b/tests/phpstan/Tests/GlobalDocBlockVisitorTest.php new file mode 100644 index 0000000000000..afadddefdadd2 --- /dev/null +++ b/tests/phpstan/Tests/GlobalDocBlockVisitorTest.php @@ -0,0 +1,49 @@ + + */ + public function data_file_asserts(): iterable { + yield from self::gatherAssertTypes( __DIR__ . '/data/global-docblock.php' ); + yield from self::gatherAssertTypes( __DIR__ . '/data/GlobalDocBlockTestClass.php' ); + } + + /** + * @dataProvider data_file_asserts + * + * @param string $assert_type Kind of assertion. + * @param string $file Fixture file. + * @param mixed ...$args Assertion arguments. + */ + public function test_file_asserts( string $assert_type, string $file, ...$args ): void { + $this->assertFileAsserts( $assert_type, $file, ...$args ); + } + + /** + * Returns the configuration registering the visitor. + * + * @return list + */ + public static function getAdditionalConfigFiles(): array { + return array( __DIR__ . '/tests.neon' ); + } +} diff --git a/tests/phpstan/Tests/HashNotationTypeTest.php b/tests/phpstan/Tests/HashNotationTypeTest.php new file mode 100644 index 0000000000000..7ea3d4518af50 --- /dev/null +++ b/tests/phpstan/Tests/HashNotationTypeTest.php @@ -0,0 +1,47 @@ + + */ + public function data_file_asserts(): iterable { + yield from self::gatherAssertTypes( __DIR__ . '/data/hash-notation.php' ); + } + + /** + * @dataProvider data_file_asserts + * + * @param string $assert_type Kind of assertion. + * @param string $file Fixture file. + * @param mixed ...$args Assertion arguments. + */ + public function test_file_asserts( string $assert_type, string $file, ...$args ): void { + $this->assertFileAsserts( $assert_type, $file, ...$args ); + } + + /** + * Returns the configuration registering the visitor. + * + * @return list + */ + public static function getAdditionalConfigFiles(): array { + return array( __DIR__ . '/tests.neon' ); + } +} diff --git a/tests/phpstan/Tests/HashNotationVisitorTest.php b/tests/phpstan/Tests/HashNotationVisitorTest.php new file mode 100644 index 0000000000000..14051ea837f8a --- /dev/null +++ b/tests/phpstan/Tests/HashNotationVisitorTest.php @@ -0,0 +1,535 @@ + $expected Tag lines the docblocks are expected to gain. + */ + public function test_derived_tags( string $code, array $expected ): void { + $this->assertSame( $expected, self::derived_tags( $code ) ); + } + + /** + * @return array}> + */ + public function data_derived_tags(): array { + return array( + 'param hash is open with optional keys' => array( + self::fn( + ' + * @param array $args { + * Arguments. + * + * @type string $post_type Post type. Default \'post\'. + * @type int $post_author Post author ID. + * } + ' + ), + array( '@phpstan-param array{post_type?: string, post_author?: int, ...} $args' ), + ), + 'return hash is sealed with required keys' => array( + self::fn( + ' + * @return array { + * @type string $name Name. + * @type int $count Count. Default 0. + * } + ' + ), + array( '@phpstan-return array{name: string, count: int}' ), + ), + 'numbered keys are required in a param hash' => array( + self::fn( + ' + * @param array $size { + * @type int $0 Width. + * @type int $1 Height. + * } + ', + '( $size )' + ), + array( '@phpstan-param array{0: int, 1: int, ...} $size' ), + ), + 'Optional. marks a return key optional' => array( + self::fn( + ' + * @return array { + * @type string $name Name. + * @type int $count Optional. Count. + * @type string $slash Optional self closing slash, which is prose. + * } + ' + ), + array( '@phpstan-return array{name: string, count?: int, slash: string}' ), + ), + 'Optional. marks a numbered param key optional' => array( + self::fn( + ' + * @param array $size { + * @type int $0 Width. + * @type int $1 Optional. Height. + * } + ', + '( $size )' + ), + array( '@phpstan-param array{0: int, 1?: int, ...} $size' ), + ), + 'a lone variadic entry describes the values' => array( + self::fn( + ' + * @param array $callbacks { + * @type callable ...$0 Callbacks. + * } + ', + '( $callbacks )' + ), + array( '@phpstan-param array $callbacks' ), + ), + 'a variadic entry beside named keys opens a return hash' => array( + self::fn( + ' + * @return array { + * @type string $name Name. + * @type mixed ...$1 The rest. + * } + ' + ), + array( '@phpstan-return array{name: string, ...}' ), + ), + 'nested hashes nest' => array( + self::fn( + ' + * @param array $args { + * @type array $labels { + * @type string $name Name. + * @type string $singular Singular name. + * } + * @type bool $public Whether public. + * } + ' + ), + array( '@phpstan-param array{labels?: array{name?: string, singular?: string, ...}, public?: bool, ...} $args' ), + ), + 'a nested hash in a return is sealed too' => array( + self::fn( + ' + * @return array { + * @type array $labels { + * @type string $name Name. + * } + * } + ' + ), + array( '@phpstan-return array{labels: array{name: string}}' ), + ), + 'keys that are not identifiers are quoted' => array( + self::fn( + ' + * @return array { + * @type string $mime-type MIME type. + * @type string $ext Extension. + * } + ' + ), + array( "@phpstan-return array{'mime-type': string, ext: string}" ), + ), + 'Array is array' => array( + self::fn( + ' + * @param Array $args { + * @type string $name Name. + * } + ' + ), + array( '@phpstan-param array{name?: string, ...} $args' ), + ), + 'the bare member of a union takes the shape' => array( + self::fn( + ' + * @param string|array $args { + * @type string $name Name. + * } + * @return array|WP_Error { + * @type string $name Name. + * } + ' + ), + array( + '@phpstan-param string|array{name?: string, ...} $args', + '@phpstan-return array{name: string}|WP_Error', + ), + ), + 'a class takes the shape through an intersection' => array( + self::fn( + ' + * @return stdClass { + * @type string $name Name. + * } + ' + ), + array( '@phpstan-return stdClass&object{name: string}' ), + ), + 'an intersection inside a union is parenthesized' => array( + self::fn( + ' + * @return stdClass|false { + * @type string $name Name. + * } + ' + ), + array( '@phpstan-return (stdClass&object{name: string})|false' ), + ), + 'a bare object return is a shape' => array( + self::fn( + ' + * @return object { + * @type string $name Name. + * } + ' + ), + array( '@phpstan-return object{name: string}' ), + ), + 'a bare object param would have to stay open, so is skipped' => array( + self::fn( + ' + * @param object $args { + * @type string $name Name. + * } + ' + ), + array(), + ), + 'a class param stays open through the class' => array( + self::fn( + ' + * @param stdClass $args { + * @type string $name Name. + * } + ' + ), + array( '@phpstan-param stdClass&object{name?: string} $args' ), + ), + 'two bare members leave the target a guess' => array( + self::fn( + ' + * @return array|object { + * @type string $name Name. + * } + ' + ), + array(), + ), + 'two classes and no bare member leave the target a guess' => array( + self::fn( + ' + * @return WP_Post|WP_Term { + * @type string $name Name. + * } + ' + ), + array(), + ), + 'a type more specific than the hash is left alone' => array( + self::fn( + ' + * @param array $args { + * @type string $name Name. + * } + ' + ), + array(), + ), + 'a PHPDoc keyword is not a class' => array( + self::fn( + ' + * @return mixed { + * @type string $name Name. + * } + ' + ), + array(), + ), + 'a hand-written counterpart wins for its tag only' => array( + self::fn( + ' + * @param array $args { + * @type string $name Name. + * } + * @param array $other { + * @type string $name Name. + * } + * @phpstan-param array{name: non-empty-string} $args + * @return array { + * @type string $name Name. + * } + * @phpstan-return array{name: non-empty-string} + ', + '( $args, $other )' + ), + array( '@phpstan-param array{name?: string, ...} $other' ), + ), + 'a multi-line hand-written counterpart covers the name it ends with' => array( + self::fn( + ' + * @param array $args { + * @type string $name Name. + * } + * @phpstan-param array{ + * name: non-empty-string, + * } $args + ' + ), + array(), + ), + 'a conditional return naming a parameter is not a shape for it' => array( + self::fn( + ' + * @param array $args { + * @type string $name Name. + * } + * @phpstan-return ($args is array ? int : string) + ' + ), + array( '@phpstan-param array{name?: string, ...} $args' ), + ), + 'a by-reference parameter is skipped' => array( + self::fn( + ' + * @param array $args { + * @type string $name Name. + * } + ', + '( &$args )' + ), + array(), + ), + 'a param hash without a variable is skipped' => array( + self::fn( + ' + * @param array { + * @type string $name Name. + * } + ' + ), + array(), + ), + 'an unclosed hash is skipped' => array( + self::fn( + ' + * @param array $args { + * @type string $name Name. + * @return void + ' + ), + array(), + ), + 'an entry without a name is skipped' => array( + self::fn( + ' + * @param array $args { + * @type string Name. + * } + ' + ), + array(), + ), + 'an entry whose type does not parse is skipped' => array( + self::fn( + ' + * @param array $args { + * @type string, $name Name. + * } + ' + ), + array(), + ), + 'an entry with a nullable type is kept' => array( + self::fn( + ' + * @param array $args { + * @type ?string $name Name. + * @type (string|null)[] $names Names. + * } + ' + ), + array( '@phpstan-param array{name?: ?string, names?: (string|null)[], ...} $args' ), + ), + 'a var hash is left alone' => array( + ' array( + ' array( + self::fn( + ' + * @param array $args Arguments. See the @type tags elsewhere. + * @return array + ' + ), + array(), + ), + ); + } + + /** + * Tests that the rewritten docblock keeps everything it had, and gains its tags + * before the closing marker. + */ + public function test_rewritten_docblock_keeps_the_original_text(): void { + $code = self::fn( + ' + * Does a thing. + * + * @since 1.0.0 + * + * @param array $args { + * @type string $name Name. + * } + * @return void + ' + ); + + $docblocks = self::docblocks_after_visiting( $code ); + + $this->assertCount( 1, $docblocks ); + $this->assertStringContainsString( "Does a thing.\n", $docblocks[0] ); + $this->assertStringContainsString( "@since 1.0.0\n", $docblocks[0] ); + $this->assertStringContainsString( "@return void\n", $docblocks[0] ); + $this->assertStringEndsWith( " * @phpstan-param array{name?: string, ...} \$args\n */", $docblocks[0] ); + } + + /** + * Wraps a docblock body in a function declaration. + * + * @param string $body Docblock lines, each starting with ` * `. + * @param string $signature Parameter list. + * @return string + */ + private static function fn( string $body, string $signature = '( $args )' ): string { + return " + */ + private static function derived_tags( string $code ): array { + $before = self::phpstan_lines( self::docblocks( $code, false ) ); + $after = self::phpstan_lines( self::docblocks_after_visiting( $code ) ); + + return array_values( array_diff( $after, $before ) ); + } + + /** + * Returns the docblocks in a snippet after the visitor has run. + * + * @param string $code PHP source. + * @return list + */ + private static function docblocks_after_visiting( string $code ): array { + return self::docblocks( $code, true ); + } + + /** + * Returns the text of every docblock in a snippet. + * + * @param string $code PHP source. + * @param bool $visit Whether to run the visitor first. + * @return list + */ + private static function docblocks( string $code, bool $visit ): array { + $stmts = ( new ParserFactory() )->createForHostVersion()->parse( $code ); + self::assertNotNull( $stmts ); + + $collector = new class() extends NodeVisitorAbstract { + /** + * @var list + */ + public array $docblocks = array(); + + public function enterNode( Node $node ): ?Node { + $doc = $node->getDocComment(); + if ( null !== $doc ) { + $this->docblocks[] = $doc->getText(); + } + return null; + } + }; + + $traverser = new NodeTraverser(); + if ( $visit ) { + $traverser->addVisitor( new HashNotationVisitor() ); + } + $traverser->addVisitor( $collector ); + $traverser->traverse( $stmts ); + + return $collector->docblocks; + } + + /** + * Extracts the `@phpstan-*` tag lines from docblocks, without their furniture. + * + * @param list $docblocks Docblock texts. + * @return list + */ + private static function phpstan_lines( array $docblocks ): array { + $lines = array(); + foreach ( $docblocks as $docblock ) { + if ( preg_match_all( '#^\s*\*\s*(@phpstan-\S+ .*)$#m', $docblock, $matches ) > 0 ) { + foreach ( $matches[1] as $line ) { + $lines[] = rtrim( $line ); + } + } + } + return $lines; + } +} diff --git a/tests/phpstan/Tests/HookDocBlockTest.php b/tests/phpstan/Tests/HookDocBlockTest.php new file mode 100644 index 0000000000000..4a7975758ec5b --- /dev/null +++ b/tests/phpstan/Tests/HookDocBlockTest.php @@ -0,0 +1,104 @@ + + */ + public static function getAdditionalConfigFiles(): array { + return array( __DIR__ . '/tests.neon' ); + } + + /** + * @dataProvider data_hook_names + * + * @param string $call Hook call expression. + * @param bool $identifiable Whether the hook name carries literal text. + * @param string $display How the name is rendered in messages. + */ + public function test_hook_names( string $call, bool $identifiable, string $display ): void { + $node = self::call( $call ); + + $this->assertSame( $identifiable, HookDocBlock::hasIdentifiableHookName( $node ) ); + $this->assertSame( $display, HookDocBlock::getHookNameDisplay( $node ) ); + } + + /** + * @return array + */ + public function data_hook_names(): array { + return array( + 'literal' => array( "apply_filters( 'the_content', \$c )", true, 'the_content' ), + 'interpolated with literal' => array( 'apply_filters( "pre_option_{$option}", $v )', true, 'pre_option_{$option}' ), + 'interpolated without literal' => array( 'apply_filters( "{$hook}", $v )', false, '{$hook}' ), + 'interpolated separators only' => array( 'apply_filters( "{$taxonomy}_{$field}", $v )', true, '{$taxonomy}_{$field}' ), + 'interpolated property fetch' => array( 'apply_filters( "manage_{$this->screen->id}_columns", $v )', true, 'manage_{...}_columns' ), + 'concatenated with literal' => array( "apply_filters( 'prefix_' . \$name, \$v )", true, 'prefix_$name' ), + 'concatenated variables' => array( 'apply_filters( $a . $b, $v )', false, '$a$b' ), + 'variable' => array( 'apply_filters( $hook_name, $v )', false, '$hook_name' ), + 'other expression' => array( 'apply_filters( wp_tests_name(), $v )', false, '...' ), + 'no arguments' => array( 'apply_filters()', false, '' ), + ); + } + + /** + * Tests that a filter resolving to a docblock without parameters is flagged, + * and an action is not. + */ + public function test_is_filter_missing_param_docs(): void { + $without = array( + 'kind' => 'inline', + 'resolved' => null, + 'paramCount' => 0, + 'problem' => null, + ); + $with = array_merge( $without, array( 'paramCount' => 1 ) ); + $unknown = array_merge( $without, array( 'paramCount' => null ) ); + + $this->assertTrue( HookDocBlock::isFilterMissingParamDocs( self::call( "apply_filters( 'a', \$v )" ), $without ) ); + $this->assertTrue( HookDocBlock::isFilterMissingParamDocs( self::call( "apply_filters_ref_array( 'a', \$v )" ), $without ) ); + $this->assertTrue( HookDocBlock::isFilterMissingParamDocs( self::call( "apply_filters_deprecated( 'a', \$v, '1.0' )" ), $without ) ); + $this->assertFalse( HookDocBlock::isFilterMissingParamDocs( self::call( "do_action( 'a', \$v )" ), $without ) ); + $this->assertFalse( HookDocBlock::isFilterMissingParamDocs( self::call( "apply_filters( 'a', \$v )" ), $with ) ); + $this->assertFalse( HookDocBlock::isFilterMissingParamDocs( self::call( "apply_filters( 'a', \$v )" ), $unknown ) ); + } + + /** + * Parses a hook call expression. + * + * @param string $call PHP expression. + * @return FuncCall + */ + private static function call( string $call ): FuncCall { + $stmts = ( new ParserFactory() )->createForHostVersion()->parse( 'findInstanceOf( $stmts, FuncCall::class ); + self::assertNotEmpty( $calls ); + + return $calls[0]; + } +} diff --git a/tests/phpstan/Tests/HookDocsVisitorTest.php b/tests/phpstan/Tests/HookDocsVisitorTest.php new file mode 100644 index 0000000000000..f238589d5c442 --- /dev/null +++ b/tests/phpstan/Tests/HookDocsVisitorTest.php @@ -0,0 +1,165 @@ + apply_filters( 'first_item', $value ), + 'second' => apply_filters( 'second_item', $value ), + ); + + /** + * Function docblock. + */ + function wp_tests_fn( $value ) { + return apply_filters( 'in_function', $value ); + } + + /** + * Argument docblock. + */ + wp_tests_fn( + /** + * Inner argument docblock. + */ + apply_filters( 'in_argument', $value ), + apply_filters( 'sibling_argument', $value ) + ); + + /** This filter is documented in wp-includes/canonical.php */ + $value = apply_filters( 'referenced', $value ); + PHP; + + $this->assertSame( + array( + 'on_statement' => 'Statement docblock.', + 'nested_in_statement' => 'Nested docblock.', + 'undocumented' => null, + 'in_condition' => 'Condition docblock.', + 'in_body' => null, + 'first_item' => 'First item docblock.', + 'second_item' => null, + 'in_function' => null, + 'in_argument' => 'Inner argument docblock.', + 'sibling_argument' => 'Argument docblock.', + 'referenced' => 'This filter is documented in wp-includes/canonical.php', + ), + self::attached_docblocks( $code ) + ); + } + + /** + * Tests that state does not leak from one traversal into the next. + */ + public function test_state_is_reset_between_traversals(): void { + $parser = ( new ParserFactory() )->createForHostVersion(); + $visitor = new HookDocsVisitor(); + $traverser = new NodeTraverser(); + $traverser->addVisitor( $visitor ); + + $documented = $parser->parse( "assertNotNull( $documented ); + $traverser->traverse( $documented ); + + $undocumented = $parser->parse( "assertNotNull( $undocumented ); + $traverser->traverse( $undocumented ); + + $calls = ( new NodeFinder() )->findInstanceOf( $undocumented, FuncCall::class ); + $this->assertCount( 1, $calls ); + $this->assertNull( $calls[0]->getAttribute( 'latestDocComment' ) ); + } + + /** + * Returns the summary line of the docblock attached to each hook call, keyed + * by hook name. + * + * @param string $code PHP source. + * @return array + */ + private static function attached_docblocks( string $code ): array { + $stmts = ( new ParserFactory() )->createForHostVersion()->parse( $code ); + self::assertNotNull( $stmts ); + + $traverser = new NodeTraverser(); + $traverser->addVisitor( new HookDocsVisitor() ); + $traverser->traverse( $stmts ); + + $result = array(); + foreach ( ( new NodeFinder() )->findInstanceOf( $stmts, FuncCall::class ) as $call ) { + $args = $call->getArgs(); + if ( ! isset( $args[0] ) || ! $args[0]->value instanceof String_ || 'wp_tests_fn' === (string) $call->name ) { + continue; + } + + $doc = $call->getAttribute( 'latestDocComment' ); + self::assertTrue( null === $doc || $doc instanceof Doc ); + + $result[ $args[0]->value->value ] = null === $doc ? null : self::summary( $doc ); + } + + return $result; + } + + /** + * Returns the first line of prose in a docblock. + * + * @param Doc $doc Docblock. + * @return string + */ + private static function summary( Doc $doc ): string { + $text = preg_replace( '#^/\*\*\s*|\s*\*/$#', '', $doc->getText() ); + $text = preg_replace( '#^\s*\*\s?#m', '', (string) $text ); + + return trim( (string) $text ); + } +} diff --git a/tests/phpstan/Tests/HookDocumentationRuleTest.php b/tests/phpstan/Tests/HookDocumentationRuleTest.php new file mode 100644 index 0000000000000..bce2fe671a7ba --- /dev/null +++ b/tests/phpstan/Tests/HookDocumentationRuleTest.php @@ -0,0 +1,131 @@ + + */ +final class HookDocumentationRuleTest extends RuleTestCase { + + /** + * Returns the rule under test, with its documentation resolved against the + * fixture WordPress root. + * + * @return HookDocumentationRule + */ + protected function getRule(): Rule { + return new HookDocumentationRule( self::getContainer()->getByType( HookDocBlock::class ) ); + } + + /** + * Returns the configuration registering the visitors and services the rule relies on. + * + * @return list + */ + public static function getAdditionalConfigFiles(): array { + return array( __DIR__ . '/tests.neon' ); + } + + /** + * Tests the errors reported for a file outside the WordPress root, whose + * reference comments resolve against the root directly. + */ + public function test_rule(): void { + $missing = 'is not preceded by a docblock documenting the hook, nor by a "This filter/action is documented in " reference comment.'; + $no_params = 'is preceded by a docblock that documents no parameters. A filter is documented with a `@param` tag for the value being filtered, plus one for each further argument passed.'; + + $this->analyse( + array( __DIR__ . '/data/hook-documentation.php' ), + array( + array( 'apply_filters() call for hook "undocumented_filter" ' . $missing, 15 ), + array( 'do_action() call for hook "undocumented_action" ' . $missing, 17 ), + array( 'apply_filters() call for hook "paramless_filter" ' . $no_params, 34 ), + array( 'apply_filters() call for hook "var_annotated_filter" ' . $no_params, 37 ), + array( 'apply_filters() call for hook "documented_filter" references documentation in "wp-includes/missing.php", but no such file exists in the tree being analyzed.', 46 ), + array( 'apply_filters() call for hook "documented_filter" references documentation in "wp-includes/other.php", but no documented "documented_filter" hook is found there.', 49 ), + array( 'apply_filters() call for hook "only_referenced_here" references documentation in "wp-includes/canonical.php", but no documented "only_referenced_here" hook is found there.', 52 ), + array( 'apply_filters() call for hook "foo_bar" references documentation in "wp-includes/canonical.php", but no documented "foo_bar" hook is found there.', 61 ), + array( 'apply_filters() call for hook "{$prefix}_bar" references documentation in "wp-includes/canonical.php", but no documented "{$prefix}_bar" hook is found there.', 64 ), + array( 'apply_filters() call for hook "pre_option_{$option}" ' . $missing, 73 ), + array( 'apply_filters() call for hook "prefix_$context" ' . $missing, 75 ), + array( 'apply_filters() call for hook "second_item" ' . $missing, 93 ), + array( 'apply_filters_deprecated() call for hook "old_filter" ' . $missing, 103 ), + array( 'do_action_ref_array() call for hook "ref_action" ' . $missing, 105 ), + array( 'apply_filters() call for hook "inside_function" ' . $missing, 120 ), + ) + ); + } + + /** + * Tests that a reference is resolved by walking up from the file holding it, + * so a theme template can reference a sibling as well as a core file. + */ + public function test_references_resolve_relative_to_the_referencing_file(): void { + $this->analyse( + array( __DIR__ . '/data/wordpress/wp-content/themes/wptests/index.php' ), + array( + array( 'do_action() call for hook "wptests_missing_theme_hook" references documentation in "author.php", but no documented "wptests_missing_theme_hook" hook is found there.', 15 ), + array( 'apply_filters() call for hook "documented_filter" references documentation in "single.php", but no such file exists in the tree being analyzed.', 18 ), + ) + ); + } + + /** + * Tests that a reference written with a dot segment resolves inside the tree, + * and that one leading outside the tree is reported as missing. + */ + public function test_references_never_resolve_outside_the_root(): void { + $this->analyse( + array( __DIR__ . '/data/wordpress/wp-admin/admin.php' ), + array( + array( 'apply_filters() call for hook "outside_filter" references documentation in "../outside.php", but no such file exists in the tree being analyzed.', 19 ), + ) + ); + } + + /** + * Tests that each kind of problem is reported under its own identifier. + */ + public function test_error_identifiers(): void { + $errors = $this->gatherAnalyserErrors( array( __DIR__ . '/data/hook-documentation.php' ) ); + + $identifiers = array(); + foreach ( $errors as $error ) { + $identifiers[ (int) $error->getLine() ] = $error->getIdentifier(); + } + + $this->assertSame( + array( + 15 => 'wordpress.hookDocMissing', + 17 => 'wordpress.hookDocMissing', + 34 => 'wordpress.hookDocNoParams', + 37 => 'wordpress.hookDocNoParams', + 46 => 'wordpress.hookDocReferenceFileMissing', + 49 => 'wordpress.hookDocReferenceHookMissing', + 52 => 'wordpress.hookDocReferenceHookMissing', + 61 => 'wordpress.hookDocReferenceHookMissing', + 64 => 'wordpress.hookDocReferenceHookMissing', + 73 => 'wordpress.hookDocMissing', + 75 => 'wordpress.hookDocMissing', + 93 => 'wordpress.hookDocMissing', + 103 => 'wordpress.hookDocMissing', + 105 => 'wordpress.hookDocMissing', + 120 => 'wordpress.hookDocMissing', + ), + $identifiers + ); + } +} diff --git a/tests/phpstan/Tests/HookParamCountRuleTest.php b/tests/phpstan/Tests/HookParamCountRuleTest.php new file mode 100644 index 0000000000000..5982bd52c1538 --- /dev/null +++ b/tests/phpstan/Tests/HookParamCountRuleTest.php @@ -0,0 +1,76 @@ + + */ +final class HookParamCountRuleTest extends RuleTestCase { + + /** + * Returns the rule under test, with its documentation resolved against the + * fixture WordPress root. + * + * @return HookParamCountRule + */ + protected function getRule(): Rule { + return new HookParamCountRule( self::getContainer()->getByType( HookDocBlock::class ) ); + } + + /** + * Returns the configuration registering the visitors and services the rule relies on. + * + * @return list + */ + public static function getAdditionalConfigFiles(): array { + return array( __DIR__ . '/tests.neon' ); + } + + /** + * Tests the mismatches reported, and the calls left alone. + */ + public function test_rule(): void { + $this->analyse( + array( __DIR__ . '/data/hook-param-count.php' ), + array( + array( 'apply_filters() for hook "two_params_short" provides 1 argument, but the hook is documented with 2 parameters.', 22 ), + array( 'apply_filters() for hook "one_param_long" provides 2 arguments, but the hook is documented with 1 parameter.', 29 ), + array( 'do_action() for hook "no_params_extra" provides 1 argument, but its docblock documents no parameters.', 39 ), + array( 'do_action() for hook "no_params_extra_two" provides 2 arguments, but its docblock documents no parameters.', 44 ), + array( 'do_action_ref_array() for hook "ref_array_long" provides 2 arguments, but the hook is documented with 1 parameter.', 51 ), + array( 'apply_filters_deprecated() for hook "deprecated_short" provides 1 argument, but the hook is documented with 2 parameters.', 59 ), + array( 'apply_filters() for hook "spread_known_long" provides 3 arguments, but the hook is documented with 1 parameter.', 90 ), + array( 'apply_filters() for hook "documented_filter" provides 1 argument, but the hook is documented with 2 parameters.', 113 ), + array( 'do_action() for hook "documented_action" provides 2 arguments, but the hook is documented with 1 parameter.', 116 ), + array( 'apply_filters() for hook "index_template_hierarchy" provides 2 arguments, but the hook is documented with 1 parameter.', 119 ), + array( 'apply_filters() for hook "malformed_tag" provides 2 arguments, but the hook is documented with 1 parameter.', 135 ), + array( 'do_action() for hook "wp_tests_{$type}_fired" provides 2 arguments, but the hook is documented with 1 parameter.', 144 ), + ) + ); + } + + /** + * Tests that every mismatch is reported under the same identifier. + */ + public function test_error_identifier(): void { + $errors = $this->gatherAnalyserErrors( array( __DIR__ . '/data/hook-param-count.php' ) ); + + $this->assertNotEmpty( $errors ); + + foreach ( $errors as $error ) { + $this->assertSame( 'wordpress.hookParamCountMismatch', $error->getIdentifier(), sprintf( 'Line %d', $error->getLine() ) ); + } + } +} diff --git a/tests/phpstan/Tests/bootstrap.php b/tests/phpstan/Tests/bootstrap.php new file mode 100644 index 0000000000000..725d7b3ed4897 --- /dev/null +++ b/tests/phpstan/Tests/bootstrap.php @@ -0,0 +1,17 @@ +" references against, in place of `src`. + * Read by tests.neon, which has no other way to learn where this directory is. + */ +putenv( 'WP_PHPSTAN_TESTS_WORDPRESS_ROOT=' . __DIR__ . '/data/wordpress' ); + +require_once dirname( __DIR__, 3 ) . '/vendor/autoload.php'; diff --git a/tests/phpstan/Tests/data/ApplyFiltersTestClass.php b/tests/phpstan/Tests/data/ApplyFiltersTestClass.php new file mode 100644 index 0000000000000..2210079675cbe --- /dev/null +++ b/tests/phpstan/Tests/data/ApplyFiltersTestClass.php @@ -0,0 +1,75 @@ +slug ) ? apply_filters( 'editable_slug', $tag->slug, $tag ) : 123; +assertType( '123|string', $slug ); + +/** + * Return type for `apply_filters_ref_array()`. + * + * @param string $foo Hello. + * @param int $bar World. + */ +$value = apply_filters_ref_array( 'foo', array( 'Hello', 123 ) ); +assertType( 'string', $value ); + +/** + * Return type for `apply_filters_deprecated()`. + * + * @param string $foo Hello. + * @param bool $bar World. + */ +$value = apply_filters_deprecated( 'foo', array( 'Hello', true ), '6.0.0' ); +assertType( 'string', $value ); + +/** + * An action is not a filter: its docblock does not type its return value, which + * stays whatever `do_action()` itself declares. + * + * @param string $foo Hello. + */ +$value = do_action( 'foo', 'Hello' ); +assertType( 'mixed', $value ); + +/** This filter is documented in wp-includes/missing.php */ +$value = apply_filters( 'documented_filter', 123 ); +assertType( 'mixed', $value ); + +/** This filter is documented in wp-includes/canonical.php */ +$value = apply_filters( 'documented_filter', 123, $context ); +assertType( 'string', $value ); + +/** This filter is documented in wp-includes/canonical.php */ +$value = apply_filters( 'not_documented_there', 123 ); +assertType( 'mixed', $value ); + +/** This filter is documented in wp-includes/canonical.php */ +$value = apply_filters( 'index_template_hierarchy', array() ); +assertType( 'array', $value ); + +/** This filter is documented in wp-includes/canonical.php */ +$value = apply_filters( 'documented_hash_filter', array() ); +assertType( 'array{post_type?: string, per_page?: int, ...}', $value ); + +/** This filter is documented in wp-includes/canonical.php */ +$value = apply_filters( $hook_name, 123 ); +assertType( 'mixed', $value ); diff --git a/tests/phpstan/Tests/data/functions.php b/tests/phpstan/Tests/data/functions.php new file mode 100644 index 0000000000000..176536568cf7d --- /dev/null +++ b/tests/phpstan/Tests/data/functions.php @@ -0,0 +1,19 @@ + $wp_tests_map A type containing whitespace. + */ +function wp_tests_documented_globals(): void { + global $wpdb, $pagenow, $wp_tests_ids, $wp_tests_map, $wp_tests_undocumented; + + assertType( 'wpdb', $wpdb ); + assertType( 'string', $pagenow ); + assertType( 'array|null', $wp_tests_ids ); + assertType( 'array', $wp_tests_map ); + assertType( 'mixed', $wp_tests_undocumented ); +} + +/** + * Documents nothing. + */ +function wp_tests_no_global_tags(): void { + global $wpdb; + + assertType( 'mixed', $wpdb ); +} + +function wp_tests_no_docblock(): void { + global $wpdb; + + assertType( 'mixed', $wpdb ); +} + +/** + * A hand-written `@var` on the statement is left alone, per variable. + * + * @global string $pagenow Current admin page. + * @global string $wp_tests_other Something else. + */ +function wp_tests_handwritten_var(): void { + /** @var int $pagenow */ + global $pagenow, $wp_tests_other; + + assertType( 'int', $pagenow ); + assertType( 'string', $wp_tests_other ); +} + +/** + * A `@global` tag on the statement itself takes precedence. + * + * @global string $pagenow Current admin page. + */ +function wp_tests_statement_level_tag(): void { + /** @global int $pagenow */ + global $pagenow; + + assertType( 'int', $pagenow ); +} + +/** + * A closure is its own frame, documented or not. + * + * @global string $pagenow Current admin page. + */ +function wp_tests_closure_frames(): void { + $undocumented = function (): void { + global $pagenow; + + assertType( 'mixed', $pagenow ); + }; + + /** + * A docblock on the assignment belongs to the statement, not to the closure. + * + * @global int $pagenow Current admin page, as the closure sees it. + */ + $documented_statement = function (): void { + global $pagenow; + + assertType( 'mixed', $pagenow ); + }; + + $documented_import = function (): void { + /** @global int $pagenow Current admin page, as the closure sees it. */ + global $pagenow; + + assertType( 'int', $pagenow ); + }; + + global $pagenow; + + assertType( 'string', $pagenow ); +} + +/** + * A tag for a variable the function does not import changes nothing. + * + * @global string $pagenow Current admin page. + */ +function wp_tests_unused_tag(): void { + global $wpdb; + + assertType( 'mixed', $wpdb ); +} + +/** @global \WP_Query $wp_query WordPress Query object. */ +global $wp_query; + +assertType( 'WP_Query', $wp_query ); + +global $wp_the_query; + +assertType( 'mixed', $wp_the_query ); diff --git a/tests/phpstan/Tests/data/hash-notation.php b/tests/phpstan/Tests/data/hash-notation.php new file mode 100644 index 0000000000000..a335fc203fb59 --- /dev/null +++ b/tests/phpstan/Tests/data/hash-notation.php @@ -0,0 +1,118 @@ + 'x', + 'count' => 1, + ); +} + +/** + * A hash on a class produces an intersection. + * + * @return stdClass { + * @type string $name Name. + * } + */ +function wp_tests_hash_object_return() { + $object = new stdClass(); + $object->name = 'x'; + + return $object; +} + +/** + * The bare member of a union takes the shape. + * + * @return array|WP_Error { + * @type string $name Name. + * } + */ +function wp_tests_hash_union_return() { + return array( 'name' => 'x' ); +} + +/** + * A hand-written `@phpstan-return` wins. + * + * @return array { + * @type string $name Name. + * } + * @phpstan-return array{name: non-empty-string} + */ +function wp_tests_hash_handwritten_return() { + return array( 'name' => 'x' ); +} + +/** + * A by-reference parameter keeps its declared type. + * + * @param array $args { + * @type string $name Name. + * } + */ +function wp_tests_hash_by_reference( &$args ) { + assertType( 'array', $args ); +} + +/** + * A hook documented with a hash is typed from the shape. + * + * @param array $args { + * @type string $post_type Post type. + * } + */ +function wp_tests_hash_hook( $args ) { + /** + * Filters the arguments. + * + * @param array $args { + * Arguments. + * + * @type string $post_type Post type. + * @type int $per_page Per page. + * } + */ + $args = apply_filters( 'wp_tests_hash_hook', $args ); + + assertType( 'array{post_type?: string, per_page?: int, ...}', $args ); +} + +/** + * The return shapes as a caller sees them. + */ +function wp_tests_hash_call_sites() { + assertType( 'array{name: string, count: int}', wp_tests_hash_param_and_return( array() ) ); + assertType( 'object{name: string}&stdClass', wp_tests_hash_object_return() ); + assertType( 'array{name: string}|WP_Error', wp_tests_hash_union_return() ); + assertType( 'array{name: non-empty-string}', wp_tests_hash_handwritten_return() ); +} diff --git a/tests/phpstan/Tests/data/hook-documentation.php b/tests/phpstan/Tests/data/hook-documentation.php new file mode 100644 index 0000000000000..6f6f4fabbaba9 --- /dev/null +++ b/tests/phpstan/Tests/data/hook-documentation.php @@ -0,0 +1,121 @@ + apply_filters( 'first_item', $value ), + 'second' => apply_filters( 'second_item', $value ), +); + +/** + * Filters a value passed through another call. + * + * @param string $value Value. + */ +$value = wp_tests_return_value( apply_filters( 'wrapped_filter', $value ) ); + +$value = apply_filters_deprecated( 'old_filter', array( $value ), '6.0.0' ); + +do_action_ref_array( 'ref_action', array( $value ) ); + +/** + * Fires a deprecated action. + * + * @param string $value Value. + */ +do_action_deprecated( 'old_action', array( $value ), '6.0.0' ); + +/** + * This docblock belongs to the function, not to the hook inside it. + * + * @param string $value Value. + */ +function wp_tests_function_docblock_does_not_reach_hook( $value ) { + return apply_filters( 'inside_function', $value ); +} diff --git a/tests/phpstan/Tests/data/hook-param-count.php b/tests/phpstan/Tests/data/hook-param-count.php new file mode 100644 index 0000000000000..bbcdfe1619c4d --- /dev/null +++ b/tests/phpstan/Tests/data/hook-param-count.php @@ -0,0 +1,144 @@ + + + + + + Tests + + + From 08e229aad1a673466201b440705fb79fed6ec402 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Mon, 9 Nov 2026 21:25:18 +0000 Subject: [PATCH 02/15] Build/Test Tools: Test that `@phpstan-param` types a filtered value. See #66634. --- tests/phpstan/Tests/data/apply-filters.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/phpstan/Tests/data/apply-filters.php b/tests/phpstan/Tests/data/apply-filters.php index d042601bac809..c4cbf22fe0004 100644 --- a/tests/phpstan/Tests/data/apply-filters.php +++ b/tests/phpstan/Tests/data/apply-filters.php @@ -32,6 +32,15 @@ $value = apply_filters( 'filter', $foo ); assertType( 'string', $value ); +/** + * A PHPStan-specific type, which takes precedence over the one in `@param`. + * + * @param string $foo Hello, World. + * @phpstan-param non-empty-string $foo + */ +$value = apply_filters( 'filter', $foo ); +assertType( 'non-empty-string', $value ); + /** * Multiple lines and multiple parameters. * From d2a6b7447d3103accc4da500d04061b5a6784540 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Tue, 10 Nov 2026 21:45:19 +0000 Subject: [PATCH 03/15] Apply suggestions from code review Co-authored-by: theAverageDev (Luca Tumedei) <2749650+lucatume@users.noreply.github.com> --- .../reusable-phpstan-static-analysis-v1.yml | 1 + tests/phpstan/Tests/HashNotationVisitorTest.php | 13 +++++++++++++ tests/phpstan/Tests/HookParamCountRuleTest.php | 2 ++ tests/phpstan/Tests/data/functions.php | 2 +- tests/phpstan/Tests/data/hook-param-count.php | 14 ++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/.github/workflows/reusable-phpstan-static-analysis-v1.yml b/.github/workflows/reusable-phpstan-static-analysis-v1.yml index 843000bb660e0..8b30babe2e44e 100644 --- a/.github/workflows/reusable-phpstan-static-analysis-v1.yml +++ b/.github/workflows/reusable-phpstan-static-analysis-v1.yml @@ -80,6 +80,7 @@ jobs: # core, so a defect in one is reported here as a failing test rather than as a # change in what the analysis below reports. - name: Run the tests of the PHPStan extensions + if: ${{ hashFiles('tests/phpstan/phpunit.xml.dist') != '' }} run: composer run phpstan:test # The directory holds more than the analysis results. PHPStan also stores what it read out of diff --git a/tests/phpstan/Tests/HashNotationVisitorTest.php b/tests/phpstan/Tests/HashNotationVisitorTest.php index 14051ea837f8a..931a52820cd70 100644 --- a/tests/phpstan/Tests/HashNotationVisitorTest.php +++ b/tests/phpstan/Tests/HashNotationVisitorTest.php @@ -53,6 +53,19 @@ public function data_derived_tags(): array { ), array( '@phpstan-param array{post_type?: string, post_author?: int, ...} $args' ), ), + 'param key marked required is required' => array( + self::fn( + ' + * @param array $args { + * Arguments. + * + * @type string $hostname Required. Server hostname. + * @type int $port Port. + * } + ' + ), + array( '@phpstan-param array{hostname: string, port?: int, ...} $args' ), + ), 'return hash is sealed with required keys' => array( self::fn( ' diff --git a/tests/phpstan/Tests/HookParamCountRuleTest.php b/tests/phpstan/Tests/HookParamCountRuleTest.php index 5982bd52c1538..48561ccf63f1e 100644 --- a/tests/phpstan/Tests/HookParamCountRuleTest.php +++ b/tests/phpstan/Tests/HookParamCountRuleTest.php @@ -57,6 +57,8 @@ public function test_rule(): void { array( 'apply_filters() for hook "index_template_hierarchy" provides 2 arguments, but the hook is documented with 1 parameter.', 119 ), array( 'apply_filters() for hook "malformed_tag" provides 2 arguments, but the hook is documented with 1 parameter.', 135 ), array( 'do_action() for hook "wp_tests_{$type}_fired" provides 2 arguments, but the hook is documented with 1 parameter.', 144 ), + array( 'apply_filters_ref_array() for hook "ref_array_filter_long" provides 2 arguments, but the hook is documented with 1 parameter.', 151 ), + array( 'do_action_deprecated() for hook "deprecated_action_long" provides 2 arguments, but the hook is documented with 1 parameter.', 158 ), ) ); } diff --git a/tests/phpstan/Tests/data/functions.php b/tests/phpstan/Tests/data/functions.php index 176536568cf7d..dc594cf106a7c 100644 --- a/tests/phpstan/Tests/data/functions.php +++ b/tests/phpstan/Tests/data/functions.php @@ -1,6 +1,6 @@ Date: Fri, 27 Nov 2026 09:58:53 -0800 Subject: [PATCH 04/15] Build/Test Tools: Avoid casting a dynamic call name to string. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The helper in `HookDocsVisitorTest` compared each call's name to `wp_tests_fn` by casting it to a string, but the name of a dynamic call such as `$callback( … )` is an expression, which cannot be converted to a string, so the test errored on one. The name is now only compared when it is a `Name`, and the fixture gains a dynamic call to cover it. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/Tests/HookDocsVisitorTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/phpstan/Tests/HookDocsVisitorTest.php b/tests/phpstan/Tests/HookDocsVisitorTest.php index f238589d5c442..0790f77df414d 100644 --- a/tests/phpstan/Tests/HookDocsVisitorTest.php +++ b/tests/phpstan/Tests/HookDocsVisitorTest.php @@ -11,6 +11,7 @@ use PhpParser\Comment\Doc; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; use PhpParser\NodeFinder; use PhpParser\NodeTraverser; @@ -77,6 +78,8 @@ function wp_tests_fn( $value ) { /** This filter is documented in wp-includes/canonical.php */ $value = apply_filters( 'referenced', $value ); + + $callback( 'dynamic_call', $value ); PHP; $this->assertSame( @@ -92,6 +95,7 @@ function wp_tests_fn( $value ) { 'in_argument' => 'Inner argument docblock.', 'sibling_argument' => 'Argument docblock.', 'referenced' => 'This filter is documented in wp-includes/canonical.php', + 'dynamic_call' => null, ), self::attached_docblocks( $code ) ); @@ -137,7 +141,7 @@ private static function attached_docblocks( string $code ): array { $result = array(); foreach ( ( new NodeFinder() )->findInstanceOf( $stmts, FuncCall::class ) as $call ) { $args = $call->getArgs(); - if ( ! isset( $args[0] ) || ! $args[0]->value instanceof String_ || 'wp_tests_fn' === (string) $call->name ) { + if ( ! isset( $args[0] ) || ! $args[0]->value instanceof String_ || ( $call->name instanceof Name && 'wp_tests_fn' === $call->name->toString() ) ) { continue; } From 7ea685dcf8f8ee5e3069ce250a47d1c80243ae6a Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 09:59:08 -0800 Subject: [PATCH 05/15] Build/Test Tools: Remove an unused import from `HookDocumentationRuleTest`. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/Tests/HookDocumentationRuleTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpstan/Tests/HookDocumentationRuleTest.php b/tests/phpstan/Tests/HookDocumentationRuleTest.php index bce2fe671a7ba..2a9f12ede4780 100644 --- a/tests/phpstan/Tests/HookDocumentationRuleTest.php +++ b/tests/phpstan/Tests/HookDocumentationRuleTest.php @@ -9,7 +9,6 @@ namespace WordPress\PHPStan\Tests; -use PHPStan\Analyser\Error; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; use WordPress\PHPStan\HookDocBlock; From 3ee8e768f82d32dbba708f88b3d22aedeabd9038 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 09:59:30 -0800 Subject: [PATCH 06/15] Build/Test Tools: Catch duplicated tags in the hash notation tests. The tags a docblock gains from `HashNotationVisitor` were found with `array_diff()`, which drops every line equal to one present beforehand. A tag the visitor wrote a second time, or wrote identically to a hand-written one, therefore went unnoticed. Each preexisting line is now removed only once. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/Tests/HashNotationVisitorTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/phpstan/Tests/HashNotationVisitorTest.php b/tests/phpstan/Tests/HashNotationVisitorTest.php index 931a52820cd70..43a6d776a8d87 100644 --- a/tests/phpstan/Tests/HashNotationVisitorTest.php +++ b/tests/phpstan/Tests/HashNotationVisitorTest.php @@ -472,6 +472,9 @@ private static function fn( string $body, string $signature = '( $args )' ): str /** * Returns the `@phpstan-*` lines the docblocks in a snippet gain from the visitor. * + * Each line present beforehand is removed only once, rather than every line equal to it, + * so that a tag the visitor writes a second time is still returned. + * * @param string $code PHP source. * @return list */ @@ -479,7 +482,14 @@ private static function derived_tags( string $code ): array { $before = self::phpstan_lines( self::docblocks( $code, false ) ); $after = self::phpstan_lines( self::docblocks_after_visiting( $code ) ); - return array_values( array_diff( $after, $before ) ); + foreach ( $before as $line ) { + $key = array_search( $line, $after, true ); + if ( false !== $key ) { + unset( $after[ $key ] ); + } + } + + return array_values( $after ); } /** From ee23bc4a71887189418a9fe4c4d8fdf9ca7fca19 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 10:00:02 -0800 Subject: [PATCH 07/15] Build/Test Tools: Run the PHPStan extension tests after reporting. The tests of the extensions ran before the analysis, so a failing one ended the job there: the pull request then got no annotations, and the comment workflow no results from the analysis of the whole codebase. The step now runs after the results are reported and uploaded, and also when the analysis failed, since the two are independent. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- .../reusable-phpstan-static-analysis-v2.yml | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/.github/workflows/reusable-phpstan-static-analysis-v2.yml b/.github/workflows/reusable-phpstan-static-analysis-v2.yml index f00f150529ecb..4b20ae235c9fc 100644 --- a/.github/workflows/reusable-phpstan-static-analysis-v2.yml +++ b/.github/workflows/reusable-phpstan-static-analysis-v2.yml @@ -33,12 +33,12 @@ jobs: # - Logs debug information. # - Installs Composer dependencies. # - Make Composer packages available globally. - # - Runs the tests of the WordPress-specific PHPStan extensions (default analysis only). # - Builds WordPress. # - Configures caching for PHPStan static analysis scans. # - Runs PHPStan static analysis. # - Reports the results as annotations. # - Uploads the results as an artifact (pull requests only). + # - Runs the tests of the WordPress-specific PHPStan extensions (default analysis only). # - Checks for errors that cannot be reported as warnings, such as out-of-date baselines. # - Saves the PHPStan result cache. # - Ensures version-controlled files are not modified or deleted. @@ -101,15 +101,6 @@ jobs: - name: Make Composer packages available globally run: echo "${PWD}/vendor/bin" >> "$GITHUB_PATH" - # The extensions in `tests/phpstan` are tested against fixtures rather than against - # core, so a defect in one is reported here as a failing test rather than as a - # change in what the analysis below reports. - # - # Both analyses load the same extensions, so the tests run in only one of them. - - name: Run the tests of the PHPStan extensions - if: ${{ matrix.analysis == 'default' && hashFiles('tests/phpstan/phpunit.xml.dist') != '' }} - run: composer run phpstan:test - # The directory holds more than the analysis results. PHPStan also stores what it read out of # each source file there, the docblocks and signatures it found, keyed by that file's contents # and nothing else. The extensions in `tests/phpstan` change what reading a file yields without @@ -287,6 +278,18 @@ jobs: name: ${{ matrix.artifact }} path: ${{ runner.temp }}/phpstan-results/ + # The extensions in `tests/phpstan` are tested against fixtures rather than against + # core, so a defect in one is reported here as a failing test rather than as a + # change in what the analysis above reports. + # + # This runs after the results are reported and uploaded, so that a failing test + # does not also keep the pull request from being annotated and commented on. It + # runs even when the analysis failed, since the two are independent. Both analyses + # load the same extensions, so the tests run in only one of them. + - name: Run the tests of the PHPStan extensions + if: ${{ !cancelled() && matrix.analysis == 'default' && hashFiles('tests/phpstan/phpunit.xml.dist') != '' }} + run: composer run phpstan:test + # Some errors cannot be ignored, so they cannot be reported as warnings either. Most often # this is an ignored error that no longer occurs, or occurs a different number of times, # which is reported under an `ignore.*` identifier. That is not something to fix in the From 728278a3716930aadeacd5809d4e63ab54da3a99 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 10:04:35 -0800 Subject: [PATCH 08/15] Build/Test Tools: Test the parser redefinition in `base.neon`. `base.neon` redefines PHPStan's `currentPhpVersionSimpleParser`, so that a file only scanned for reflection goes through the same node visitors as an analyzed one, and a hash notation shape is reflected the same way from either. It names that service an undocumented extension point whose renaming would quietly undo this, but nothing would have noticed. The extension tests cannot, since PHPStan's test harness already parses every file with the rich parser. `ReflectionParserTest` therefore runs a real analysis with `base.neon`, in a cache directory of its own, over a fixture calling a function declared in a scanned file, and checks the dumped return type is the documented shape. With the redefinition renamed away, the type is the plain `array` and the test fails. The test configuration also takes the `phpVersion` and `treatPhpDocTypesAsCertain` parameters of `base.neon`, so that the types inferred in the fixtures are those the analysis of core sees. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/README.md | 2 + tests/phpstan/Tests/ReflectionParserTest.php | 132 ++++++++++++++++++ .../Tests/data/reflection/analyzed.php | 11 ++ .../phpstan/Tests/data/reflection/scanned.php | 24 ++++ tests/phpstan/Tests/tests.neon | 7 + tests/phpstan/base.neon | 1 + 6 files changed, 177 insertions(+) create mode 100644 tests/phpstan/Tests/ReflectionParserTest.php create mode 100644 tests/phpstan/Tests/data/reflection/analyzed.php create mode 100644 tests/phpstan/Tests/data/reflection/scanned.php diff --git a/tests/phpstan/README.md b/tests/phpstan/README.md index 3058d1c16ae87..79eba189a098a 100644 --- a/tests/phpstan/README.md +++ b/tests/phpstan/README.md @@ -213,6 +213,8 @@ These identifiers are specific to WordPress, and can be ignored or baselined lik The extensions have tests of their own under [`Tests/`](Tests), built on PHPStan's test harness. The two rules are run over fixture files and the errors they report are compared with the ones expected. The visitors and the return type extension are checked through the types PHPStan infers for the `assertType()` calls in their fixtures. Reference comments resolve against a small WordPress tree of fixtures in [`Tests/data/wordpress`](Tests/data/wordpress) rather than against `src`, so a broken reference can be tested without one existing in core. +The harness parses every file the way an analyzed file is parsed, so one test, `ReflectionParserTest`, runs a real analysis with `base.neon` instead. It checks that a function declared in a file that is only scanned is reflected with the shape its hash notation describes, which depends on the redefinition of `currentPhpVersionSimpleParser` there. + Run them with: ```bash diff --git a/tests/phpstan/Tests/ReflectionParserTest.php b/tests/phpstan/Tests/ReflectionParserTest.php new file mode 100644 index 0000000000000..bdec967f44e00 --- /dev/null +++ b/tests/phpstan/Tests/ReflectionParserTest.php @@ -0,0 +1,132 @@ +tmp_dir = sys_get_temp_dir() . '/wp-phpstan-reflection-' . bin2hex( random_bytes( 8 ) ); + $this->assertTrue( mkdir( $this->tmp_dir ) ); + } + + /** + * Removes the cache directory. + */ + protected function tearDown(): void { + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( $this->tmp_dir, FilesystemIterator::SKIP_DOTS ), + RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ( $files as $file ) { + if ( ! $file instanceof SplFileInfo ) { + continue; + } + if ( $file->isDir() && ! $file->isLink() ) { + rmdir( $file->getPathname() ); + } else { + unlink( $file->getPathname() ); + } + } + rmdir( $this->tmp_dir ); + + parent::tearDown(); + } + + /** + * Tests that the shape documented in a scanned file is reflected. + */ + public function test_scanned_file_is_parsed_with_the_visitors(): void { + $data = __DIR__ . '/data/reflection'; + $command = array( + PHP_BINARY, + dirname( __DIR__, 3 ) . '/vendor/phpstan/phpstan/phpstan', + 'analyse', + '--configuration=' . $data . '/phpstan.neon', + '--no-progress', + '--error-format=json', + $data . '/analyzed.php', + ); + + $env = getenv(); + $env['WP_PHPSTAN_TESTS_TMP_DIR'] = $this->tmp_dir; + + $process = proc_open( + $command, + array( + 1 => array( 'pipe', 'w' ), + 2 => array( 'pipe', 'w' ), + ), + $pipes, + null, + $env + ); + $this->assertIsResource( $process ); + + $output = (string) stream_get_contents( $pipes[1] ); + $errors = (string) stream_get_contents( $pipes[2] ); + fclose( $pipes[1] ); + fclose( $pipes[2] ); + $status = proc_close( $process ); + + // PHPStan exits 1 when it reports errors, as it does for the dumped type. + $this->assertSame( 1, $status, "PHPStan did not complete.\n" . $output . $errors ); + + $report = json_decode( $output, true ); + $this->assertIsArray( $report, "PHPStan did not print a report.\n" . $output . $errors ); + + $this->assertSame( + array( + $data . '/analyzed.php' => array( + 'errors' => 1, + 'messages' => array( + array( + 'message' => 'Dumped type: array{name: string, count: int}', + 'line' => 11, + 'ignorable' => false, + 'identifier' => 'phpstan.dumpType', + ), + ), + ), + ), + $report['files'] ?? null + ); + } +} diff --git a/tests/phpstan/Tests/data/reflection/analyzed.php b/tests/phpstan/Tests/data/reflection/analyzed.php new file mode 100644 index 0000000000000..6230a2d517122 --- /dev/null +++ b/tests/phpstan/Tests/data/reflection/analyzed.php @@ -0,0 +1,11 @@ + 'x', + 'count' => 1, + ); +} diff --git a/tests/phpstan/Tests/tests.neon b/tests/phpstan/Tests/tests.neon index 20499dc540dd7..1d6702effe171 100644 --- a/tests/phpstan/Tests/tests.neon +++ b/tests/phpstan/Tests/tests.neon @@ -8,6 +8,13 @@ # core. parameters: + # The parameters of base.neon that change what type PHPStan infers, kept the + # same here, so that a fixture's types are the ones the analysis of core sees. + phpVersion: + min: 70400 + max: 80600 + treatPhpDocTypesAsCertain: false + # The hook functions are reflected from core itself, so the tests see the same # signatures the analysis does. The helpers are declared for the fixtures that # pass a filtered value through another call. diff --git a/tests/phpstan/base.neon b/tests/phpstan/base.neon index 3045c14477cc7..16cfadb23a83a 100644 --- a/tests/phpstan/base.neon +++ b/tests/phpstan/base.neon @@ -91,6 +91,7 @@ services: # point. Should a future release rename either, this definition stops being wired # into anything and the behavior above returns; the errors it prevents are all of # the quiet sort, so check this first when a hash notation type looks wrong. + # Tests/ReflectionParserTest.php fails when that happens. # # `arguments!` replaces the inherited arguments rather than merging with them. # Without the `!`, the original `wrappedParser` argument survives and the container From 624335704e506ebf0b8e2d19c19bd50a7b120848 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 10:04:49 -0800 Subject: [PATCH 09/15] Build/Test Tools: Say where the extension tests now run in CI. Follow-up to running them after the analysis is reported, which left the README still saying they run ahead of it. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpstan/README.md b/tests/phpstan/README.md index 79eba189a098a..0183c8b5ae70c 100644 --- a/tests/phpstan/README.md +++ b/tests/phpstan/README.md @@ -227,7 +227,7 @@ or, outside the Docker environment: composer phpstan:test ``` -They also run in CI, ahead of the analysis itself. +They also run in CI, after the analysis of the whole codebase, so that a failing test does not keep its results from being reported. ## Ignoring and baselining errors From 387d35ff9da3b8ece874c4bad06c927810635543 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 10:59:31 -0800 Subject: [PATCH 10/15] Build/Test Tools: Add the configuration `ReflectionParserTest` runs with. The test analyzes its fixture with `Tests/data/reflection/phpstan.neon`, which was left out when the test was added, so it failed in any fresh checkout. The workflow's path filter now covers everything under `tests/phpstan/Tests`, so that a change to that configuration or to any fixture also runs the workflow. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- .github/workflows/phpstan-static-analysis.yml | 4 ++-- .../Tests/data/reflection/phpstan.neon | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/phpstan/Tests/data/reflection/phpstan.neon diff --git a/.github/workflows/phpstan-static-analysis.yml b/.github/workflows/phpstan-static-analysis.yml index 3c882f7a44197..75f61e02a2f32 100644 --- a/.github/workflows/phpstan-static-analysis.yml +++ b/.github/workflows/phpstan-static-analysis.yml @@ -21,9 +21,9 @@ on: - 'tests/phpstan/base.neon' - 'tests/phpstan/baselines/**' - 'tests/phpstan/baselines-strict/**' - # These files configure the tests of the PHPStan extensions. + # These files configure the tests of the PHPStan extensions, and hold their fixtures. - 'tests/phpstan/phpunit.xml.dist' - - 'tests/phpstan/Tests/tests.neon' + - 'tests/phpstan/Tests/**' # Confirm any changes to relevant workflow files. - '.github/workflows/phpstan-static-analysis.yml' - '.github/workflows/reusable-phpstan-static-analysis-v2.yml' diff --git a/tests/phpstan/Tests/data/reflection/phpstan.neon b/tests/phpstan/Tests/data/reflection/phpstan.neon new file mode 100644 index 0000000000000..9fee01cd3236c --- /dev/null +++ b/tests/phpstan/Tests/data/reflection/phpstan.neon @@ -0,0 +1,24 @@ +# Configuration for ReflectionParserTest. +# +# This is the configuration core is analyzed with, so that the test runs against +# what base.neon actually sets up rather than against a copy of it. Only the cache +# directory, the excluded paths, and the file holding the function to reflect are +# changed. + +includes: + - ../../../base.neon + +parameters: + # A directory of the test's own, so that the run neither reads nor writes the + # cache of the analysis of core. + tmpDir: %env.WP_PHPSTAN_TESTS_TMP_DIR% + + level: 0 + + # base.neon excludes the fixtures from the analysis of core, these among them. + excludePaths!: + analyse: [] + analyseAndScan: [] + + scanFiles: + - scanned.php From bf59296df4bd251bcf65fa9992afd3cb5177729d Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 11:00:20 -0800 Subject: [PATCH 11/15] Build/Test Tools: Make the traversal reset test able to fail. `test_state_is_reset_between_traversals` ran two complete traversals, but a complete traversal leaves no state behind, since every node entered is also left, and the second snippet's statement started an undocumented scope regardless. Removing the reset in `HookDocsVisitor::beforeTraverse()` left it passing. The first traversal is now aborted inside a documented statement, by a visitor that throws, and the second traverses a bare call with no statement around it. The test now fails without the reset. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/Tests/HookDocsVisitorTest.php | 51 ++++++++++++++++----- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/tests/phpstan/Tests/HookDocsVisitorTest.php b/tests/phpstan/Tests/HookDocsVisitorTest.php index 0790f77df414d..774770d8d9ad1 100644 --- a/tests/phpstan/Tests/HookDocsVisitorTest.php +++ b/tests/phpstan/Tests/HookDocsVisitorTest.php @@ -10,13 +10,16 @@ namespace WordPress\PHPStan\Tests; use PhpParser\Comment\Doc; +use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; use PhpParser\NodeFinder; use PhpParser\NodeTraverser; +use PhpParser\NodeVisitorAbstract; use PhpParser\ParserFactory; use PHPUnit\Framework\TestCase; +use RuntimeException; use WordPress\PHPStan\HookDocsVisitor; /** @@ -103,24 +106,50 @@ function wp_tests_fn( $value ) { /** * Tests that state does not leak from one traversal into the next. + * + * A traversal that runs to completion leaves no state behind, since every node + * entered is also left. So the first traversal is aborted inside a documented + * statement, as when another visitor throws, leaving that statement's docblock + * current. The second traverses a bare call, with no statement around it to + * start an undocumented scope, so only the reset at the start of a traversal + * keeps that docblock from being attached to it. */ public function test_state_is_reset_between_traversals(): void { - $parser = ( new ParserFactory() )->createForHostVersion(); - $visitor = new HookDocsVisitor(); - $traverser = new NodeTraverser(); - $traverser->addVisitor( $visitor ); + $parser = ( new ParserFactory() )->createForHostVersion(); + $visitor = new HookDocsVisitor(); + + $aborting = new NodeTraverser(); + $aborting->addVisitor( $visitor ); + $aborting->addVisitor( + new class() extends NodeVisitorAbstract { + public function enterNode( Node $node ): ?Node { + if ( $node instanceof FuncCall ) { + throw new RuntimeException( 'Aborted.' ); + } + return null; + } + } + ); - $documented = $parser->parse( "parse( "assertNotNull( $documented ); - $traverser->traverse( $documented ); + try { + $aborting->traverse( $documented ); + $this->fail( 'The traversal was not aborted.' ); + } catch ( RuntimeException $e ) { + $this->assertSame( 'Aborted.', $e->getMessage() ); + } - $undocumented = $parser->parse( "parse( "assertNotNull( $undocumented ); - $traverser->traverse( $undocumented ); + $call = ( new NodeFinder() )->findFirstInstanceOf( $undocumented, FuncCall::class ); + $this->assertInstanceOf( FuncCall::class, $call ); + + $traverser = new NodeTraverser(); + $traverser->addVisitor( $visitor ); + $traverser->traverse( array( $call ) ); - $calls = ( new NodeFinder() )->findInstanceOf( $undocumented, FuncCall::class ); - $this->assertCount( 1, $calls ); - $this->assertNull( $calls[0]->getAttribute( 'latestDocComment' ) ); + $this->assertNull( $call->getAttribute( 'latestDocComment' ) ); } /** From 1ba9c3d32e7991f27e5918dfae6bec212981d5bf Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 11:00:50 -0800 Subject: [PATCH 12/15] Build/Test Tools: Raise the memory limit for the PHPStan extension tests. The suite peaks at about 100M, most of it PHPStan's container and reflection, which leaves little headroom under PHP's default of 128M. Unlike `composer phpstan`, which runs at 2G, `phpstan:test` set no limit of its own, so a few more fixtures or a PHPStan upgrade could take a run over it. The PHPUnit configuration now sets 1G. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/phpunit.xml.dist | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpstan/phpunit.xml.dist b/tests/phpstan/phpunit.xml.dist index 837cf6abd0236..8ae5b23ce5522 100644 --- a/tests/phpstan/phpunit.xml.dist +++ b/tests/phpstan/phpunit.xml.dist @@ -20,6 +20,13 @@ convertNoticesToExceptions="true" convertDeprecationsToExceptions="true" > + + + + Tests From ffbbbe5aabb9625530ca3a4a96eade75bdb42970 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 11:15:09 -0800 Subject: [PATCH 13/15] Build/Test Tools: Keep `ReflectionParserTest` from deadlocking on PHPStan. The test read PHPStan's standard output to its end before reading standard error from a second pipe. Should PHPStan ever write more to standard error than a pipe holds, such as a flood of deprecations under a newer PHP, it would block on that pipe while the test waited on the other, and the run would hang until the job timed out. Standard error now goes to a file in the test's own directory, read once PHPStan exits. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/Tests/ReflectionParserTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/phpstan/Tests/ReflectionParserTest.php b/tests/phpstan/Tests/ReflectionParserTest.php index bdec967f44e00..a9ccafac65b4c 100644 --- a/tests/phpstan/Tests/ReflectionParserTest.php +++ b/tests/phpstan/Tests/ReflectionParserTest.php @@ -88,11 +88,16 @@ public function test_scanned_file_is_parsed_with_the_visitors(): void { $env = getenv(); $env['WP_PHPSTAN_TESTS_TMP_DIR'] = $this->tmp_dir; + // Standard error goes to a file rather than a second pipe. Reading one pipe to + // its end while the other fills up would leave both processes waiting on each + // other, should PHPStan ever write more to standard error than a pipe holds. + $errors_file = $this->tmp_dir . '/stderr.log'; + $process = proc_open( $command, array( 1 => array( 'pipe', 'w' ), - 2 => array( 'pipe', 'w' ), + 2 => array( 'file', $errors_file, 'w' ), ), $pipes, null, @@ -101,10 +106,9 @@ public function test_scanned_file_is_parsed_with_the_visitors(): void { $this->assertIsResource( $process ); $output = (string) stream_get_contents( $pipes[1] ); - $errors = (string) stream_get_contents( $pipes[2] ); fclose( $pipes[1] ); - fclose( $pipes[2] ); $status = proc_close( $process ); + $errors = (string) file_get_contents( $errors_file ); // PHPStan exits 1 when it reports errors, as it does for the dumped type. $this->assertSame( 1, $status, "PHPStan did not complete.\n" . $output . $errors ); From 0832a5f9ed071cc35c2845b1acb06fd9801b83ef Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 11:16:04 -0800 Subject: [PATCH 14/15] Build/Test Tools: Skip the PHPStan extension tests when setup failed. The step ran on `!cancelled()`, so that a failing analysis would not stop it, but that also ran it when an earlier step setting up the job failed, such as installing the Composer dependencies. It could then only fail too, with an error about PHPUnit that obscured the real one. The step now also requires that the analysis step was not skipped. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- .github/workflows/reusable-phpstan-static-analysis-v2.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/reusable-phpstan-static-analysis-v2.yml b/.github/workflows/reusable-phpstan-static-analysis-v2.yml index 4b20ae235c9fc..73c21bc21d23d 100644 --- a/.github/workflows/reusable-phpstan-static-analysis-v2.yml +++ b/.github/workflows/reusable-phpstan-static-analysis-v2.yml @@ -284,10 +284,12 @@ jobs: # # This runs after the results are reported and uploaded, so that a failing test # does not also keep the pull request from being annotated and commented on. It - # runs even when the analysis failed, since the two are independent. Both analyses - # load the same extensions, so the tests run in only one of them. + # runs even when the analysis failed, since the two are independent, but not when + # the analysis was skipped: that means a step setting up the job, such as installing + # the Composer dependencies, failed, and the tests could only fail along with it. + # Both analyses load the same extensions, so the tests run in only one of them. - name: Run the tests of the PHPStan extensions - if: ${{ !cancelled() && matrix.analysis == 'default' && hashFiles('tests/phpstan/phpunit.xml.dist') != '' }} + if: ${{ !cancelled() && steps.phpstan.outcome != 'skipped' && matrix.analysis == 'default' && hashFiles('tests/phpstan/phpunit.xml.dist') != '' }} run: composer run phpstan:test # Some errors cannot be ignored, so they cannot be reported as warnings either. Most often From b550d151c63cebd91533d8cde729aab6dae77a18 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Nov 2026 11:16:24 -0800 Subject: [PATCH 15/15] Build/Test Tools: Correct which docblocks `HashNotationVisitor` rewrites. The comment on the parser redefinition in `base.neon` said the visitor rewrites `@var` docblocks as well, but it leaves `@var` alone and only rewrites `@param` and `@return`, as its own documentation explains and a test case asserts. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e --- tests/phpstan/base.neon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpstan/base.neon b/tests/phpstan/base.neon index 16cfadb23a83a..756745fac2af2 100644 --- a/tests/phpstan/base.neon +++ b/tests/phpstan/base.neon @@ -75,8 +75,8 @@ services: # reflected out of a file does not depend on whether that file was analyzed, and # the per-file reflection cache can be keyed on the file's contents alone. # - # HashNotationVisitor breaks that agreement. It rewrites `@param`, `@return` and - # `@var` docblocks, which are exactly what reflection exposes, so a file read + # HashNotationVisitor breaks that agreement. It rewrites `@param` and `@return` + # docblocks, which are exactly what reflection exposes, so a file read # through the simple parser reflects the documented `array` where the same file # analyzed reflects the shape its hash notation describes. Analyzing a subset of # the tree then reports against types no longer derived, and writes that reading