From 7fb027e4cc90b178f519d442dd778043606b1515 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 29 Oct 2026 20:25:37 -0700 Subject: [PATCH 01/15] Generate the WordPress stubs on demand instead of committing them 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 --- .gitattributes | 3 - .github/workflows/unit-test.yml | 22 + .gitignore | 3 + composer.json | 7 +- docs/contributors/code/coding-guidelines.md | 2 +- tools/phpstan/base.neon | 3 +- tools/phpstan/generate-wordpress-stubs.sh | 93 +- .../wordpress-stubs/wordpress-stubs.php | 7 +- .../wordpress-stubs/wordpress-stubs.stub | 176196 --------------- 9 files changed, 113 insertions(+), 176223 deletions(-) delete mode 100644 tools/phpstan/wordpress-stubs/wordpress-stubs.stub diff --git a/.gitattributes b/.gitattributes index effcd041235ea6..72ab49a65c7253 100644 --- a/.gitattributes +++ b/.gitattributes @@ -26,6 +26,3 @@ tsconfig*.json linguist-language=jsonc # PHPStan stub files are PHP. *.stub linguist-language=PHP diff=php - -# The WordPress stubs generated for PHPStan by tools/phpstan/generate-wordpress-stubs.sh. -tools/phpstan/wordpress-stubs/wordpress-stubs.stub linguist-generated diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 38c665097287e0..f0d02e8814c6a6 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -555,6 +555,28 @@ jobs: name: build-assets path: build + # The WordPress stubs are not committed, and generating them from the core commit that + # tools/phpstan/wordpress-stubs/composer.json records takes a while, so they are cached by + # that file and the script that generates them. The script generates them again when the + # cached ones were generated from another source. As with the result cache, only runs on + # trunk save a cache. + - name: Restore the WordPress stubs + id: restore-wordpress-stubs + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: tools/phpstan/wordpress-stubs/wordpress-stubs.stub + key: ${{ runner.os }}-wordpress-stubs-${{ hashFiles('tools/phpstan/wordpress-stubs/composer.json', 'tools/phpstan/generate-wordpress-stubs.sh') }} + + - name: Generate the WordPress stubs + run: composer phpstan:generate-wordpress-stubs + + - name: Save the WordPress stubs + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/trunk' && steps.restore-wordpress-stubs.outputs.cache-hit != 'true' }} + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: tools/phpstan/wordpress-stubs/wordpress-stubs.stub + key: ${{ steps.restore-wordpress-stubs.outputs.cache-primary-key }} + # Restores the latest cache saved on trunk for the same configuration and dependencies. # PHPStan invalidates the parts of the cache that the changed files affect. The baseline is # left out of the key, since it only filters the errors and does not change the analysis, diff --git a/.gitignore b/.gitignore index 4953dfeb9474f8..62190da368f0ab 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,9 @@ yarn.lock /perf-envs /composer.lock +# The WordPress stubs generated for PHPStan by tools/phpstan/generate-wordpress-stubs.sh. +/tools/phpstan/wordpress-stubs/wordpress-stubs.stub + # The /.cache folder is needed for phpcs to cache results between runs, while other .cache folders must be ignored # It is not possible to re-include a file if a parent directory of that file is excluded # So, both /.cache and /.cache/.gitkeep must be re-included diff --git a/composer.json b/composer.json index 0b19c04eeb775d..b3c07939a93c96 100644 --- a/composer.json +++ b/composer.json @@ -54,7 +54,10 @@ }, { "type": "path", - "url": "./tools/phpstan/wordpress-stubs" + "url": "./tools/phpstan/wordpress-stubs", + "options": { + "symlink": true + } } ], "require": { @@ -64,7 +67,7 @@ "scripts": { "format": "phpcbf --standard=phpcs.xml.dist --report-summary --report-source", "lint": "phpcs --standard=phpcs.xml.dist", - "phpstan": "phpstan analyse --memory-limit=2G", + "phpstan": "tools/phpstan/generate-wordpress-stubs.sh && phpstan analyse --memory-limit=2G", "phpstan:baselines": "@phpstan --configuration=phpstan.neon.dist --generate-baseline=tools/phpstan/baseline.neon --allow-empty-baseline", "phpstan:generate-wordpress-stubs": "tools/phpstan/generate-wordpress-stubs.sh", "test": "phpunit", diff --git a/docs/contributors/code/coding-guidelines.md b/docs/contributors/code/coding-guidelines.md index b37067187bf01c..777c9848bb1a57 100644 --- a/docs/contributors/code/coding-guidelines.md +++ b/docs/contributors/code/coding-guidelines.md @@ -816,7 +816,7 @@ The easiest way to use PHPCS is [local environment](/docs/contributors/code/gett If you prefer to install PHPCS locally, you should use `composer`. [Install `composer`](https://getcomposer.org/download/) on your computer, then run `composer install`. This will install `phpcs` and `WordPress-Coding-Standards` which you can then run via `composer lint`. -PHP code is also statically analyzed with [PHPStan](https://phpstan.org/), using the [WordPress extension](https://github.com/szepeviktor/phpstan-wordpress). The analysis covers the plugin's own PHP in `lib/`, and all the PHP that WordPress core ships from this repository: the PHP in packages such as `block-library`, and the PHP templates in `packages/wp-build/templates`. The templates are rendered with sample values for their placeholders before they are analyzed, keeping each line in place, so errors are reported on the template lines themselves. After running `composer install` and `npm run build`, which generates PHP files that the analysis needs, run `composer phpstan`, or `npm run typecheck:php` to run it in the [local environment](/docs/contributors/code/getting-started-with-code-contribution.md#local-wordpress-environment). Errors that predate the analysis are recorded in `tools/phpstan/baseline.neon`. When a change fixes one of them, PHPStan reports the baseline entry as unmatched; regenerate the baseline with `composer phpstan:baselines` (or `npm run typecheck:php:baselines`) and commit the result. The analysis runs against WordPress stubs generated from a pinned commit of core trunk, in `tools/phpstan/wordpress-stubs/`, since Gutenberg uses functions that no released WordPress version has yet. To update them to the latest trunk, run `composer phpstan:generate-wordpress-stubs -- --commit=trunk`, then `composer update` and `composer phpstan:baselines`. When a change depends on a wordpress-develop pull request, generate the stubs from that pull request with `--pr=`, or with `--checkout` from the wordpress-develop checkout that Gutenberg is in, so that the analysis knows what the pull request adds, and commit them with the change. Once the core change is committed, regenerate them with `--commit=trunk` before merging. To adjust the configuration locally, such as to try a higher rule level, create a `phpstan.neon`, which is ignored by Git and used in place of `phpstan.neon.dist`. The tests in `phpunit/` are not analyzed by default, but the PHPUnit extension and stubs for the WordPress test library are installed, so a `phpstan.neon` can opt in to them: +PHP code is also statically analyzed with [PHPStan](https://phpstan.org/), using the [WordPress extension](https://github.com/szepeviktor/phpstan-wordpress). The analysis covers the plugin's own PHP in `lib/`, and all the PHP that WordPress core ships from this repository: the PHP in packages such as `block-library`, and the PHP templates in `packages/wp-build/templates`. The templates are rendered with sample values for their placeholders before they are analyzed, keeping each line in place, so errors are reported on the template lines themselves. After running `composer install` and `npm run build`, which generates PHP files that the analysis needs, run `composer phpstan`, or `npm run typecheck:php` to run it in the [local environment](/docs/contributors/code/getting-started-with-code-contribution.md#local-wordpress-environment). Errors that predate the analysis are recorded in `tools/phpstan/baseline.neon`. When a change fixes one of them, PHPStan reports the baseline entry as unmatched; regenerate the baseline with `composer phpstan:baselines` (or `npm run typecheck:php:baselines`) and commit the result. The analysis runs against WordPress stubs generated from a pinned commit of core trunk, since Gutenberg uses functions that no released WordPress version has yet. The stubs are not committed: `tools/phpstan/wordpress-stubs/composer.json` records the commit, and `composer phpstan` generates the stubs from it first when they are missing or out of date, which needs Git and network access. To update them to the latest trunk, run `composer phpstan:generate-wordpress-stubs -- --commit=trunk`, then `composer update` and `composer phpstan:baselines`, and commit the updated `composer.json`. When a change depends on a wordpress-develop pull request, generate the stubs from that pull request with `--pr=`, so that the analysis knows what the pull request adds, and commit the `composer.json` with the change. While working on both, `--checkout` generates them from the wordpress-develop checkout that Gutenberg is in, including its uncommitted changes, but those stubs cannot be generated in CI, so use `--pr` before pushing. Once the core change is committed, regenerate them with `--commit=trunk` before merging. To adjust the configuration locally, such as to try a higher rule level, create a `phpstan.neon`, which is ignored by Git and used in place of `phpstan.neon.dist`. The tests in `phpunit/` are not analyzed by default, but the PHPUnit extension and stubs for the WordPress test library are installed, so a `phpstan.neon` can opt in to them: ```neon includes: diff --git a/tools/phpstan/base.neon b/tools/phpstan/base.neon index ef6ad28237b8f2..51bcd77b127e21 100644 --- a/tools/phpstan/base.neon +++ b/tools/phpstan/base.neon @@ -45,7 +45,8 @@ parameters: - ../../packages/block-serialization-default-parser/test # WordPress itself is declared by the stubs that szepeviktor/phpstan-wordpress loads, which are # generated from core trunk, or from a core pull request that a change depends on, by - # generate-wordpress-stubs.sh, and installed from wordpress-stubs/. + # generate-wordpress-stubs.sh, and installed from wordpress-stubs/. `composer phpstan` generates + # them first, when they are missing or out of date. bootstrapFiles: - bootstrap.php # The polyfills that WordPress provides for newer PHP functions. diff --git a/tools/phpstan/generate-wordpress-stubs.sh b/tools/phpstan/generate-wordpress-stubs.sh index 539e0eeee182da..c0384dc0548d32 100755 --- a/tools/phpstan/generate-wordpress-stubs.sh +++ b/tools/phpstan/generate-wordpress-stubs.sh @@ -8,12 +8,17 @@ # from trunk, they are generated here with its generator, and installed through the path repository # in composer.json in place of the released package. # +# The stubs are not committed. The package's composer.json records the source that they are generated +# from, and `composer phpstan` runs this script with no option first, which generates them when they +# are missing or were generated from another source. +# # This is temporary. Once php-stubs/wordpress-stubs publishes builds from trunk, require a pinned one # instead, and remove this script, the wordpress-stubs/ package, and the path repository. # # Usage, through `composer phpstan:generate-wordpress-stubs --