Repository navigation
Build/Test Tools: Add tests for the PHPStan extensions - #15850
johnbillion wants to merge 18 commits into
Conversation
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.
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
There was a problem hiding this comment.
🔵 Needs a closer look
The pinned PHPStan test harness was unavailable for inspection, so the new CI gate’s bootstrap and reflection behavior needs validation via composer phpstan:test with installed dependencies before approval.
No open issues 🎉
What changed in this PR
Adds a dedicated PHPUnit suite for WordPress’s custom PHPStan extensions, covering their behavior independently of core analysis.
Changes:
- Tests hook documentation, parameter counts, docblock visitors, and inferred types using isolated fixtures.
- Adds test configuration and Composer/npm commands.
- Runs the suite before PHPStan analysis in CI and documents local usage.
| File | Description |
|---|---|
tests/phpstan/Tests/tests.neon |
Registers extensions and fixture dependencies. |
tests/phpstan/Tests/HookParamCountRuleTest.php |
Checks parameter-count diagnostics and identifiers. |
tests/phpstan/Tests/HookDocumentationRuleTest.php |
Checks documentation diagnostics and reference resolution. |
tests/phpstan/Tests/HookDocsVisitorTest.php |
Tests docblock attachment and traversal isolation. |
tests/phpstan/Tests/HookDocBlockTest.php |
Tests hook-name and parameter-documentation helpers. |
tests/phpstan/Tests/HashNotationVisitorTest.php |
Tests hash-notation translation and text preservation. |
tests/phpstan/Tests/HashNotationTypeTest.php |
Checks inferred hash-notation types. |
tests/phpstan/Tests/GlobalDocBlockVisitorTest.php |
Checks types inferred from global documentation. |
tests/phpstan/Tests/data/wordpress/wp-includes/other.php |
Supplies unrelated hook documentation. |
tests/phpstan/Tests/data/wordpress/wp-includes/canonical.php |
Supplies canonical hook-reference targets. |
tests/phpstan/Tests/data/wordpress/wp-content/themes/wptests/index.php |
Exercises theme-relative documentation references. |
tests/phpstan/Tests/data/wordpress/wp-content/themes/wptests/author.php |
Supplies a sibling theme reference target. |
tests/phpstan/Tests/data/wordpress/wp-admin/admin.php |
Exercises relative paths and root boundaries. |
tests/phpstan/Tests/data/outside.php |
Supplies an out-of-root reference target. |
tests/phpstan/Tests/data/hook-param-count.php |
Provides matching and mismatched argument counts. |
tests/phpstan/Tests/data/hook-documentation.php |
Provides valid and invalid hook documentation. |
tests/phpstan/Tests/data/hash-notation.php |
Provides hash-shape type assertions. |
tests/phpstan/Tests/data/GlobalDocBlockTestClass.php |
Covers globals within namespaced methods. |
tests/phpstan/Tests/data/global-docblock.php |
Covers global types, precedence, and scope. |
tests/phpstan/Tests/data/functions.php |
Supplies a generic fixture helper. |
tests/phpstan/Tests/data/ApplyFiltersTestClass.php |
Covers filter typing within classes. |
tests/phpstan/Tests/data/apply-filters.php |
Covers filter return-type inference. |
tests/phpstan/Tests/bootstrap.php |
Sets the fixture root and loads dependencies. |
tests/phpstan/Tests/ApplyFiltersDynamicFunctionReturnTypeExtensionTest.php |
Checks filter return-type assertions. |
tests/phpstan/README.md |
Documents the suite and execution commands. |
tests/phpstan/phpunit.xml.dist |
Configures test discovery and strict checks. |
tests/phpstan/base.neon |
Scans tests while excluding intentional-error fixtures. |
package.json |
Adds the Docker-backed test command. |
composer.json |
Adds the PHPUnit suite command. |
.github/workflows/reusable-phpstan-static-analysis-v1.yml |
Runs extension tests before analysis. |
.github/workflows/phpstan-static-analysis.yml |
Triggers CI for test-configuration changes. |
Review effort: Balanced
| * @return iterable<string, mixed[]> | ||
| */ | ||
| public function data_file_asserts(): iterable { | ||
| yield from self::gatherAssertTypes( __DIR__ . '/data/apply-filters.php' ); |
There was a problem hiding this comment.
Oooo. Cool language feature I haven't seen before.
There was a problem hiding this comment.
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.
Co-authored-by: theAverageDev (Luca Tumedei) <2749650+lucatume@users.noreply.github.com>
# Conflicts: # composer.json # package.json
Trunk split the analysis into a default and a strict run, and moved `trunk` onto a new `reusable-phpstan-static-analysis-v2.yml`, leaving v1 for the 7.0 and 7.1 branches. The step that runs the tests of the PHPStan extensions moves from v1 to v2, where it runs only in the default analysis, since both load the same extensions. v1 is left as on trunk. In `composer.json`, trunk's new `phpstan:fix` is kept alongside the `phpstan:test` script added here. The workflow path filters keep both the strict baselines and the extension test configuration. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
…Test`. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
`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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
…ith. 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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
`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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
…sts. 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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
…Stan. 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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
…tes. 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) <noreply@anthropic.com> Claude-Code-Session-Id: 01dcbf24-d0f9-4895-b5f2-e91787b6742e
There was a problem hiding this comment.
🤖 Comment by Claude Opus 5.5 (medium effort)
Trunk was merged in, and twelve commits were added on top to address findings from three review rounds: one by Claude Opus 5.5, then two by Claude Fable 5.2. The most notable additions are a test for the currentPhpVersionSimpleParser redefinition in base.neon, which no harness-based test could cover, and moving the extension tests in CI to after the analysis results are reported.
Details
Merge of trunk (7ec762d)
- Trunk moved its own analysis to a new
reusable-phpstan-static-analysis-v2.yml, which runs a default and a strict analysis in parallel, and left v1 for the 7.0 and 7.1 branches. The step running the extension tests was moved from v1 to v2, where it runs in the default analysis only, and v1 was left as on trunk. - In
composer.json, trunk's newphpstan:fixwas kept alongsidephpstan:test. - In the workflow path filters, trunk's
tests/phpstan/baselines-strict/**was kept alongside the paths of the extension tests.
Fixes
- 6f03dc1 —
HookDocsVisitorTestcast a call's name to a string, which errors on a dynamic call such as$callback( … ), since its name is an expression. The name is now compared only when it is aName, and the fixture gains a dynamic call to cover it. - 7ea685d — An unused
use PHPStan\Analyser\Error;was removed fromHookDocumentationRuleTest. - 3ee8e76 —
HashNotationVisitorTestfound the derived tags witharray_diff(), which hid a tag the visitor wrote twice. Each preexisting line is now removed only once. - ee23bc4 — The extension tests ran ahead of the analysis, so a failing test left the pull request without annotations or results for its PHPStan comment. They now run after the results are reported and uploaded.
- 728278a —
ReflectionParserTestwas added. PHPStan's test harness already parses every file with the rich parser (currentPhpVersionSimpleParser!in itssrc/Testing/TestCase.neon), so no harness-based test could detect the redefinition inbase.neonbreaking. The test instead runs a real analysis withbase.neon, in a cache directory of its own, over a fixture calling a function declared in a file that is only scanned. With the redefinition renamed away, the dumped type falls back toarrayand the test fails.tests.neonalso gained thephpVersionandtreatPhpDocTypesAsCertainparameters ofbase.neon. - 6243357 — The README was updated to say where the extension tests now run in CI.
- 387d35f — The configuration
ReflectionParserTestruns with,Tests/data/reflection/phpstan.neon, had been left out of the commit by a local ignore rule, and was added. The workflow path filter now coverstests/phpstan/Tests/**. - bf59296 —
test_state_is_reset_between_traversalscould not fail, since a complete traversal leaves no state behind. The first traversal is now aborted inside a documented statement, and the second visits a bare call. The test now fails with the reset inbeforeTraverse()removed. - 1ba9c3d — The suite peaks at about 100M, close to PHP's default limit of 128M, so
phpunit.xml.distnow setsmemory_limitto 1G. With PHP started at 64M, the suite passes with this and runs out of memory without it. - ffbbbe5 —
ReflectionParserTestread standard output to its end before standard error, which could deadlock if PHPStan wrote more to standard error than a pipe holds. Standard error now goes to a file. - 0832a5f — The test step ran on
!cancelled()even when job setup failed, such as installing the Composer dependencies, adding a misleading second failure. It now also requires that the analysis step was not skipped. - b550d15 — A comment in
base.neonsaidHashNotationVisitorrewrites@vardocblocks. It rewrites only@paramand@return.
Findings left as they are
tests.neonregisters the extensions by hand rather than taking them frombase.neon, so the two can drift apart. That is a known trade-off, which the file's header explains.convertDeprecationsToExceptionsfails the suite on a deprecation inside PHPStan orplugin.phpunder a newer PHP, which is worth knowing about.- Loading
src/wp-includes/plugin.phpthroughbootstrapFilesruns its top-level code. The comment intests.neonexplains why it is loaded rather than scanned. - A guard in
ReflectionParserTest::tearDown()against a directorysetUp()failed to create was tried and dropped. PHPUnit 9 already reports thesetUp()error and discards a later one fromtearDown(), so nothing was masked.
Checked
- The harness's test cache does not serve stale results after a visitor change. With the cache warm, making
@paramkeys required failed the assertions in the analyzed fixture, and making return keys optional failed the call sites reflected frombootstrapFiles. - The suite passes locally: 113 tests, 268 assertions.
The extensions in `tests/phpstan` change what PHPStan reads out of core, deriving types from hash notation and `@global` tags, typing filtered values, and checking hook documentation, so a defect in one surfaces only as a quiet change in what the analysis reports. They are now covered by a PHPUnit suite built on PHPStan's test harness, which runs them against fixtures rather than against core, with hook reference comments resolving against a small fixture tree instead of `src`. It is run with `composer phpstan:test` or `npm run typecheck:php:test`. Since the harness parses every file as an analyzed one, it cannot see whether the redefinition of the simple parser in `base.neon` still applies, which is what gives a file that is only scanned the same derived types. One test therefore runs a real analysis with `base.neon` to cover it, so that a PHPStan upgrade renaming the service it redefines fails a test rather than quietly undoing it. In CI, the suite runs in the analysis of the whole codebase, after its results are reported, so that a failing test does not also leave a pull request without its annotations. It is skipped on branches that do not have it. Developed in #15850. Follow-up to r62292, r62939, r63420, r63462. Props johnbillion, lucatume, westonruter. Fixes #66634. git-svn-id: https://develop.svn.wordpress.org/trunk@65808 602fd350-edb4-49c9-b593-d223f7449a82
The extensions in `tests/phpstan` change what PHPStan reads out of core, deriving types from hash notation and `@global` tags, typing filtered values, and checking hook documentation, so a defect in one surfaces only as a quiet change in what the analysis reports. They are now covered by a PHPUnit suite built on PHPStan's test harness, which runs them against fixtures rather than against core, with hook reference comments resolving against a small fixture tree instead of `src`. It is run with `composer phpstan:test` or `npm run typecheck:php:test`. Since the harness parses every file as an analyzed one, it cannot see whether the redefinition of the simple parser in `base.neon` still applies, which is what gives a file that is only scanned the same derived types. One test therefore runs a real analysis with `base.neon` to cover it, so that a PHPStan upgrade renaming the service it redefines fails a test rather than quietly undoing it. In CI, the suite runs in the analysis of the whole codebase, after its results are reported, so that a failing test does not also leave a pull request without its annotations. It is skipped on branches that do not have it. Developed in WordPress/wordpress-develop#15850. Follow-up to r62292, r62939, r63420, r63462. Props johnbillion, lucatume, westonruter. Fixes #66634. Built from https://develop.svn.wordpress.org/trunk@65808 git-svn-id: http://core.svn.wordpress.org/trunk@64837 1a063a9b-81f0-0310-95a4-ce76da25c4cd
✅ Committed in r65808 (c4b144d).
Adds a PHPUnit suite for the custom PHPStan extensions in
tests/phpstan, built on PHPStan's test harness.Run with
composer phpstan:testornpm run typecheck:php:test. The suite also runs in the PHPStan workflow.Trac ticket: https://core.trac.wordpress.org/ticket/66634
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Writing the tests and preparing this PR.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.