Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
d2bce4b
Build/Test Tools: Add tests for the PHPStan extensions.
johnbillion Nov 9, 2026
78475e8
Merge branch 'trunk' into 66634-phpstan-extension-tests
johnbillion Nov 9, 2026
08e229a
Build/Test Tools: Test that `@phpstan-param` types a filtered value.
johnbillion Nov 9, 2026
d2a6b74
Apply suggestions from code review
johnbillion Nov 10, 2026
05661e3
Merge branch 'trunk' into 66634-phpstan-extension-tests
johnbillion Nov 13, 2026
7ec762d
Merge branch 'trunk' into 66634-phpstan-extension-tests
westonruter Nov 27, 2026
6f03dc1
Build/Test Tools: Avoid casting a dynamic call name to string.
westonruter Nov 27, 2026
7ea685d
Build/Test Tools: Remove an unused import from `HookDocumentationRule…
westonruter Nov 27, 2026
3ee8e76
Build/Test Tools: Catch duplicated tags in the hash notation tests.
westonruter Nov 27, 2026
ee23bc4
Build/Test Tools: Run the PHPStan extension tests after reporting.
westonruter Nov 27, 2026
728278a
Build/Test Tools: Test the parser redefinition in `base.neon`.
westonruter Nov 27, 2026
6243357
Build/Test Tools: Say where the extension tests now run in CI.
westonruter Nov 27, 2026
387d35f
Build/Test Tools: Add the configuration `ReflectionParserTest` runs w…
westonruter Nov 27, 2026
bf59296
Build/Test Tools: Make the traversal reset test able to fail.
westonruter Nov 27, 2026
1ba9c3d
Build/Test Tools: Raise the memory limit for the PHPStan extension te…
westonruter Nov 27, 2026
ffbbbe5
Build/Test Tools: Keep `ReflectionParserTest` from deadlocking on PHP…
westonruter Nov 27, 2026
0832a5f
Build/Test Tools: Skip the PHPStan extension tests when setup failed.
westonruter Nov 27, 2026
b550d15
Build/Test Tools: Correct which docblocks `HashNotationVisitor` rewri…
westonruter Nov 27, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/phpstan-static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ on:
- 'tests/phpstan/base.neon'
- 'tests/phpstan/baselines/**'
- 'tests/phpstan/baselines-strict/**'
# These files configure the tests of the PHPStan extensions, and hold their fixtures.
- 'tests/phpstan/phpunit.xml.dist'
- 'tests/phpstan/Tests/**'
# Confirm any changes to relevant workflow files.
- '.github/workflows/phpstan-static-analysis.yml'
- '.github/workflows/reusable-phpstan-static-analysis-v2.yml'
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/reusable-phpstan-static-analysis-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
# - 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.
Expand Down Expand Up @@ -277,6 +278,20 @@ 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, 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() && 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
# 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
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"phpstan:strict": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/bin/phpstan analyse --configuration=phpstan-strict.neon.dist --memory-limit=2G" ],
"phpstan:baselines": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/generate-baselines.php" ],
"phpstan:fix": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/analyse.php --memory-limit=2G --fix" ],
"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",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"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:fix": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:fix --",
"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"
Expand Down
20 changes: 20 additions & 0 deletions tests/phpstan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@ 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.

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
npm run typecheck:php:test
```

or, outside the Docker environment:

```bash
composer phpstan:test
```

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

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Tests for ApplyFiltersDynamicFunctionReturnTypeExtension.
*
* @package WordPress
*/

declare(strict_types=1);

namespace WordPress\PHPStan\Tests;

use PHPStan\Testing\TypeInferenceTestCase;

/**
* Checks the type PHPStan infers for a filtered value against the `assertType()`
* calls in the fixtures.
*/
final class ApplyFiltersDynamicFunctionReturnTypeExtensionTest extends TypeInferenceTestCase {

/**
* Gathers the assertions from the fixtures.
*
* @return iterable<string, mixed[]>
*/
public function data_file_asserts(): iterable {
yield from self::gatherAssertTypes( __DIR__ . '/data/apply-filters.php' );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooo. Cool language feature I haven't seen before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah unfortunately PHPUnit collects all data provider data for all tests before starting (so it can show progress) so it doesn't actually provide the memory benefit that a generator usually does. Neat though.

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<string>
*/
public static function getAdditionalConfigFiles(): array {
return array( __DIR__ . '/tests.neon' );
}
}
49 changes: 49 additions & 0 deletions tests/phpstan/Tests/GlobalDocBlockVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Tests for GlobalDocBlockVisitor.
*
* @package WordPress
*/

declare(strict_types=1);

namespace WordPress\PHPStan\Tests;

use PHPStan\Testing\TypeInferenceTestCase;

/**
* Checks the type PHPStan infers for an imported global against the `assertType()`
* calls in the fixtures.
*/
final class GlobalDocBlockVisitorTest extends TypeInferenceTestCase {

/**
* Gathers the assertions from the fixtures.
*
* @return iterable<string, mixed[]>
*/
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<string>
*/
public static function getAdditionalConfigFiles(): array {
return array( __DIR__ . '/tests.neon' );
}
}
47 changes: 47 additions & 0 deletions tests/phpstan/Tests/HashNotationTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Tests the types PHPStan infers once HashNotationVisitor has translated a hash.
*
* @package WordPress
*/

declare(strict_types=1);

namespace WordPress\PHPStan\Tests;

use PHPStan\Testing\TypeInferenceTestCase;

/**
* Checks the `assertType()` calls in the hash notation fixture.
*/
final class HashNotationTypeTest extends TypeInferenceTestCase {

/**
* Gathers the assertions from the fixture.
*
* @return iterable<string, mixed[]>
*/
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<string>
*/
public static function getAdditionalConfigFiles(): array {
return array( __DIR__ . '/tests.neon' );
}
}
Loading
Loading