Repository navigation
PHPStan: Generate the WordPress stubs on demand instead of committing them - #2
Conversation
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>
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>
|
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.
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. |
|
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. 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:
|
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>
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.jsonrecords. 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.stubis 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?
composer.jsonalready recorded the commit (and pull request) that the stubs were generated from, so it becomes the pointer. Only--commit,--pr, and--checkoutwrite it.composer phpstannow generates the stubs, every contributor who runs PHPStan runs the generator, including on Windows without a Unix shell.generate-wordpress-stubs.shis replaced bygenerate-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.shdoes for trunk itself, rather than running that Bash script, which comes down to appending the$wpdbdeclaration after generating. The script's other trunk step, correcting thebinary stringtypes 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 phpstangenerates 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.--checkoutfrom uncommitted changes are used as they are locally, with a note to generate them with--pror--commitbefore pushing, since CI cannot reproduce them. CI fails with that message if the recorded source is uncommitted changes.composer.json, after Composer installs it.wordpress-stubs.phpthrows an explanatory exception when the stub is missing, such as whenvendor/bin/phpstanis run directly, as an IDE does.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 theWORDPRESS_STUBS_GENERATOR_PACKAGESenvironment variable. (An earlier commit committed a lock file for the generator instead.)Not tested: Windows itself, and
npm run typecheck:php, which runscomposer phpstanin 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_blocksas aWP_Block_List, which adds a baseline entry for theempty()check on it inpackages/block-library/src/navigation/index.php. Regenerating the baseline also corrects an entry that was already outdated on the base branch: thenext_tag( 'img' )error inpackages/block-library/src/image/index.phpoccurs 4 times since a41faf7, not 3.Testing Instructions
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.composer phpstan -- --error-format=json > report.json, and check thatreport.jsonholds only the JSON report.vendor/bin/phpstan analyse. It fails with a message explaining how to generate the stub.composer phpstan. It generates the stub, then runs the analysis.tools/phpstanto a directory outside any wordpress-develop checkout, delete the stub, and runWORDPRESS_STUBS_CACHE_DIR=<empty directory> php tools/phpstan/generate-wordpress-stubs.phpthere. It fetches the commit and the generator, installs the generator with the pinned packages, and generates a byte-identical stub.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