Skip to content

Build/Test Tools: Add tests for the PHPStan extensions - #15850

Closed
johnbillion wants to merge 18 commits into
WordPress:trunkfrom
johnbillion:66634-phpstan-extension-tests
Closed

johnbillion wants to merge 18 commits into
WordPress:trunkfrom
johnbillion:66634-phpstan-extension-tests

Conversation

@johnbillion

@johnbillion johnbillion commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

✅ 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:test or npm 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.

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-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@johnbillion
johnbillion marked this pull request as draft November 9, 2026 16:58
@johnbillion
johnbillion marked this pull request as ready for review November 9, 2026 16:58
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

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:

Props johnbillion, lucatume, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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

@westonruter westonruter left a comment

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.

Thanks for doing this.

* @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.

Comment thread tests/phpstan/Tests/data/functions.php Resolved
Co-authored-by: theAverageDev (Luca Tumedei) <2749650+lucatume@users.noreply.github.com>
@johnbillion
johnbillion requested a review from lucatume November 13, 2026 01:29
# Conflicts:
#	composer.json
#	package.json
westonruter and others added 7 commits November 27, 2026 09:46
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
westonruter and others added 6 commits November 27, 2026 10:59
…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

@westonruter westonruter left a comment •

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.

🤖 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 new phpstan:fix was kept alongside phpstan:test.
  • In the workflow path filters, trunk's tests/phpstan/baselines-strict/** was kept alongside the paths of the extension tests.

Fixes

  • 6f03dc1 — HookDocsVisitorTest cast 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 a Name, and the fixture gains a dynamic call to cover it.
  • 7ea685d — An unused use PHPStan\Analyser\Error; was removed from HookDocumentationRuleTest.
  • 3ee8e76 — HashNotationVisitorTest found the derived tags with array_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 — ReflectionParserTest was added. PHPStan's test harness already parses every file with the rich parser (currentPhpVersionSimpleParser! in its src/Testing/TestCase.neon), so no harness-based test could detect the redefinition in base.neon breaking. The test instead runs a real analysis with base.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 to array and the test fails. tests.neon also gained the phpVersion and treatPhpDocTypesAsCertain parameters of base.neon.
  • 6243357 — The README was updated to say where the extension tests now run in CI.
  • 387d35f — The configuration ReflectionParserTest runs 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 covers tests/phpstan/Tests/**.
  • bf59296 — test_state_is_reset_between_traversals could 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 in beforeTraverse() removed.
  • 1ba9c3d — The suite peaks at about 100M, close to PHP's default limit of 128M, so phpunit.xml.dist now sets memory_limit to 1G. With PHP started at 64M, the suite passes with this and runs out of memory without it.
  • ffbbbe5 — ReflectionParserTest read 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.neon said HashNotationVisitor rewrites @var docblocks. It rewrites only @param and @return.

Findings left as they are

  • tests.neon registers the extensions by hand rather than taking them from base.neon, so the two can drift apart. That is a known trade-off, which the file's header explains.
  • convertDeprecationsToExceptions fails the suite on a deprecation inside PHPStan or plugin.php under a newer PHP, which is worth knowing about.
  • Loading src/wp-includes/plugin.php through bootstrapFiles runs its top-level code. The comment in tests.neon explains why it is loaded rather than scanned.
  • A guard in ReflectionParserTest::tearDown() against a directory setUp() failed to create was tried and dropped. PHPUnit 9 already reports the setUp() error and discards a later one from tearDown(), so nothing was masked.

Checked

  • The harness's test cache does not serve stale results after a visitor change. With the cache warm, making @param keys required failed the assertions in the analyzed fixture, and making return keys optional failed the call sites reflected from bootstrapFiles.
  • The suite passes locally: 113 tests, 268 assertions.

wporg-sync pushed a commit that referenced this pull request Nov 27, 2026
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
@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 65808
GitHub commit: c4b144d

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Nov 27, 2026
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 27, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

4 participants