Skip to content

PHPStan: Generate the WordPress stubs on demand instead of committing them - #2

Merged
westonruter merged 15 commits into
justlevine:tools/phpstanfrom
westonruter:tools/phpstan-generate-stubs-on-demand
Oct 30, 2026
Merged

westonruter merged 15 commits into
justlevine:tools/phpstanfrom
westonruter:tools/phpstan-generate-stubs-on-demand

Conversation

@westonruter

@westonruter westonruter commented Oct 30, 2026 •

Copy link
Copy Markdown

What?

See WordPress#66693 (comment)

An experiment, for PR 66693 (WordPress#66693): stop committing the generated WordPress stubs, and generate them on demand from the core commit that tools/phpstan/wordpress-stubs/composer.json records. This is meant to show what the proposal in the linked review thread looks like before deciding on it.

Why?

tools/phpstan/wordpress-stubs/wordpress-stubs.stub is a 6.3 MB generated file, which every pull request that updates the stubs carries as a diff, including the planned scheduled bumps to the latest trunk. Committing only a pointer to the core commit matches where this is headed anyway: once php-stubs/wordpress-stubs publishes builds from trunk, the repository would hold a pinned package version, not the file.

How?

  • The stub is ignored, and its source is committed. The package's composer.json already recorded the commit (and pull request) that the stubs were generated from, so it becomes the pointer. Only --commit, --pr, and --checkout write it.
  • The generator is a PHP script. Since composer phpstan now generates the stubs, every contributor who runs PHPStan runs the generator, including on Windows without a Unix shell. generate-wordpress-stubs.sh is replaced by generate-wordpress-stubs.php, which needs only PHP, Git, and Composer. It runs commands without a shell, passing their arguments as arrays, so nothing needs quoting on any platform. It does what php-stubs/wordpress-stubs' generate.sh does for trunk itself, rather than running that Bash script, which comes down to appending the $wpdb declaration after generating. The script's other trunk step, correcting the binary string types in core's AVIF info class, was made unnecessary by Correct PHPDoc types in the AVIF info parser WordPress/wordpress-develop#15198, which fixed those types in core in r65047.
  • composer phpstan generates the stubs first. Run with no option, the script exits at once when the stubs are up to date, and generates them otherwise. The stub header records a generation key for the source, the generator and its pinned packages, and the script itself, so a pull that changes any of them generates the stubs again. The script reports on standard error, so a report read from standard output, as in CI, stays intact.
  • Uncommitted changes are local only. Stubs generated with --checkout from uncommitted changes are used as they are locally, with a note to generate them with --pr or --commit before pushing, since CI cannot reproduce them. CI fails with that message if the recorded source is uncommitted changes.
  • The path repository is now required to symlink, since the stub is generated next to the package's composer.json, after Composer installs it. wordpress-stubs.php throws an explanatory exception when the stub is missing, such as when vendor/bin/phpstan is run directly, as an IDE does.
  • CI caches the stub by the pointer and the script, restoring it on every run and saving it only on trunk, like the result cache. On a miss, it generates the stub, which took 24 seconds from scratch locally.
  • The generator's output packages are pinned. Since CI now generates the stubs, a new release of a package that determines the generator's output could otherwise change them and break the baselines without any commit changing. Like Gutenberg, which has no Composer lock file but pins its development tools to exact versions in composer.json, the script requires exact versions of php-stubs/generator, nikic/php-parser, phpdocumentor/reflection-docblock, phpdocumentor/type-resolver, and phpstan/phpdoc-parser before resolving the rest, which are the generator's own development tools, such as PHPUnit and PHPCS. They can be overridden with the WORDPRESS_STUBS_GENERATOR_PACKAGES environment variable. (An earlier commit committed a lock file for the generator instead.)

Not tested: Windows itself, and npm run typecheck:php, which runs composer phpstan in the wp-env CLI container. Generating the stubs there needs Git and network access in that container. Once the stubs are generated on the host, the container would find them up to date, since the repository is mounted.

The stubs are also bumped to r65047, to pick up that fix. The newer stubs type WP_Block::$inner_blocks as a WP_Block_List, which adds a baseline entry for the empty() check on it in packages/block-library/src/navigation/index.php. Regenerating the baseline also corrects an entry that was already outdated on the base branch: the next_tag( 'img' ) error in packages/block-library/src/image/index.php occurs 4 times since a41faf7, not 3.

Testing Instructions

  1. Check out this branch. The stub committed on the base branch is now untracked and ignored.
  2. Run composer phpstan:generate-wordpress-stubs. Since the stub has no generation key, it is generated again, reading the commit from the enclosing wordpress-develop checkout when there is one, and fetching it otherwise. Only the header differs from the committed stub.
  3. Run it again. It exits at once, without output.
  4. Run composer phpstan -- --error-format=json > report.json, and check that report.json holds only the JSON report.
  5. Delete the stub, and run vendor/bin/phpstan analyse. It fails with a message explaining how to generate the stub.
  6. Run composer phpstan. It generates the stub, then runs the analysis.
  7. To check the CI path, copy tools/phpstan to a directory outside any wordpress-develop checkout, delete the stub, and run WORDPRESS_STUBS_CACHE_DIR=<empty directory> php tools/phpstan/generate-wordpress-stubs.php there. It fetches the commit and the generator, installs the generator with the pinned packages, and generates a byte-identical stub.
  8. On Windows, repeat steps 2 and 6 from PowerShell or Command Prompt.

Testing Instructions for Keyboard

Not applicable: no user interface changes.

Screenshots or screencast

Not applicable.

Use of AI Tools

Written with Claude Code (Claude Opus 5.5), which implemented the changes, tested them locally, and drafted this description, under review by the author.

🤖 Generated with Claude Code

https://claude.ai/code/session_0196gPggS2BAiTAjpcwJ95Wd

westonruter and others added 2 commits October 29, 2026 20:25
The stubs are a 6 MB generated file, which every pull request that
updates them carries as a diff. The package's composer.json already
records the core commit or pull request they are generated from, so
commit only that, and ignore the stubs.

Run with no option, the generator now does nothing when the stubs were
generated from that source, and generates them otherwise. The stubs
record a key for the source, the generator, and the script itself, so a
pull that changes any of them generates them again. Only the options
write the recorded source, so a regeneration leaves composer.json as it
is. Stubs generated from uncommitted changes in a checkout are used as
they are, since they cannot be generated again.

`composer phpstan` runs the generator first, which reports on standard
error so that a report read from standard output stays intact. The path
repository is now required to be a symlink, since the stubs are
generated next to the package's composer.json, and the bootstrap file
explains how to generate them when they are missing, such as when
PHPStan is run directly.

CI caches the stubs by the recorded source and the script, so it only
generates them when either changes, which took 24 seconds from scratch
locally.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Now that CI generates the stubs, a cache miss would install whatever
versions of the generator's dependencies are current, such as of
php-stubs/generator, nikic/php-parser, and
phpdocumentor/reflection-docblock, which determine its output. A new
release could then change the stubs and break the baselines without any
commit changing. The generator does not commit a lock file, so commit
one here, and install from it.

When the lock file is missing, such as after changing the generator's
commit, the dependencies are updated and the lock file is written for
committing. The cached generator and the generation key include the lock
file, so a change to it generates the stubs again, as does the CI cache
key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
westonruter and others added 11 commits October 29, 2026 20:50
Gutenberg has no Composer lock file. It pins its development tools that
affect the results to exact versions in composer.json, such as PHPStan,
and lets their dependencies float. Do the same for the generator in
place of the 148 KB lock file: require exact versions of the packages
that determine its output, php-stubs/generator, nikic/php-parser,
phpdocumentor/reflection-docblock, phpdocumentor/type-resolver, and
phpstan/phpdoc-parser, before resolving the rest. Its other dependencies
are its own development tools, such as PHPUnit and PHPCS.

The pinned versions are those that the lock file had, and the stubs are
unchanged. They can be overridden with an environment variable, like
the generator's commit, and are part of the cached generator's directory
and of the generation key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Now that `composer phpstan` generates the stubs first, every analysis
depends on the generator script. Composer runs scripts through cmd.exe
on Windows, which cannot run a bash script, so the analysis no longer
ran there, while it did when the stubs were committed. The generator's
own generate.sh is bash too.

The script is now PHP, which Composer always has, and runs Git and
Composer without a shell, passing arguments as arrays, so that nothing
needs quoting on any platform. It does what generate.sh does for trunk
itself: it fixes the `binary string` type in the AVIF class, runs
generate-stubs with the generator's finder and visitor, and appends the
`$wpdb` shim. Its Requests 1.x step only applies to versions of
WordPress that bundle that library, which are now rejected. Since the
generator reads the WordPress source from its working directory, it
runs in the work directory against the cached generator, which no
longer needs copying.

A commit in the enclosing checkout is checked out with an index of its
own, rather than with `git archive` and `tar`, and the generator is set
up inside the cache directory before being moved into place, so that
the move never crosses file systems.

The stubs are byte-identical to those of the bash script, apart from
the script's name and the generation key in the header, for a trunk
commit read locally and fetched with a cold cache, and the `--pr` and
`--checkout` modes behave as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Match the usage block by its line breaks, rather than with `^` inside
  the pattern, which PhpStorm flags as an anchor in an unexpected
  position.
- Interpolate array elements without braces where none are needed.
- Name the temporary directories with `uniqid()` in place of
  `random_bytes()`, which can throw. The names only need to be unique,
  and `mkdir()` fails when one is not. Document that
  `create_directory()` throws then.
- Have `read_recorded()` return the recorded commit, pull request, and
  whether the changes were uncommitted as a typed shape, checking each
  value's type in one place rather than in `main()`.

PHPStan at level 9 and PHPCS report nothing for the file either.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Use "afterward", the American English form.
- Format the `source/wordpress` path as code, so that it does not read
  as a misspelling of WordPress.
- Register the cleanup of the temporary directories as closures, rather
  than as the string `__NAMESPACE__ . '\remove_path'`, in which the
  spell checker reads `\r` as an escape, leaving "emove". A closure also
  makes the call checkable by static analysis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PhpStorm's grammar check still flags `source/wordpress` as a misspelling
of WordPress when formatted as code, so describe what the path is for
rather than naming it. The code beside the first comment names it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Declare what each function relies on: non-empty strings for paths,
repositories, commits, and settings, which fall back to their defaults
when empty; non-empty lists for commands and sparse checkout patterns;
the three output modes of `execute()` as literal strings; a positive
revision; and a numeric pull request number in the recorded source.
`usage()` is declared to never return, so that the analysis knows the
values it guards.

The checks that this needs are real ones:

- A new `resolve_commit()` verifies that a ref names a commit and
  returns its hash, for HEAD of the checkout and of the fetched clone.
- A commit that is still empty after the options are resolved is
  rejected before anything is fetched.
- A checkout whose path does not resolve is rejected, rather than cast
  to an empty string.
- A revision or pull request number of 0 is not accepted.

The revision lookup, which was repeated for the checkout and the clone,
now follows them once. PHPStan at its maximum level, PHPCS, and
PhpStorm report nothing, and generating from a trunk commit, locally
and with a cold cache, from a pull request, and from a checkout with
uncommitted changes, behaves as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The narrowed types of `execute()` and `run()` left their variable names
and descriptions out of line with the longest type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`composer()` and `setting()` each returned a ternary whose condition
was two comparisons, which made the line hard to read. Return the
fallback early instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With the flexible heredoc syntax of PHP 7.3, which the plugin's minimum
of 7.4 allows, the header's body and closing marker can be indented. Do
so by one level more than the surrounding code, so that the header text
stands apart from it. The closing marker's indentation is removed from
each line, so the generated header is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Once the PHPDoc types in core's AVIF info class are corrected, the
generator no longer needs to rewrite them, and the recorded commit
should be bumped to one that includes the fix. Mark the rewrite as
removable then, and add a TODO beside the recorded commit. JSON has no
comments, so it goes under `extra`, which the generator does not read
beyond `extra.source`. Regenerating the package drops it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@westonruter
westonruter marked this pull request as ready for review October 30, 2026 06:13
@github-actions

Copy link
Copy Markdown

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Technical Prototype, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core, Gutenberg Plugin.
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@github-actions

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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <westonruter@git.wordpress.org>

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

westonruter and others added 2 commits October 30, 2026 08:57
Bump the recorded core commit to r65047, which corrects the PHPDoc
types in the AVIF info class, and drop the note that asked for the
bump.

The new stubs type `WP_Block::$inner_blocks` as a `WP_Block_List`, so
the `empty()` check on it in the Navigation block is now reported as
never falsy, and is added to the baseline. Regenerating the baseline
also corrects the count of the `next_tag( 'img' )` error in the Image
block, which was already out of date on the base branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since r65047, core's AVIF info class uses valid PHPDoc types, and the
recorded commit includes it, so the generator no longer rewrites its
`binary string` tags. Note why that step of generate.sh is left out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@westonruter
westonruter merged commit aa27419 into justlevine:tools/phpstan Oct 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant