Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.php') }}

- 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,
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ yarn.lock
/perf-envs
/composer.lock

# The WordPress stubs generated for PHPStan by tools/phpstan/generate-wordpress-stubs.php.
/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
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
},
{
"type": "path",
"url": "./tools/phpstan/wordpress-stubs"
"url": "./tools/phpstan/wordpress-stubs",
"options": {
"symlink": true
}
}
],
"require": {
Expand All @@ -64,9 +67,9 @@
"scripts": {
"format": "phpcbf --standard=phpcs.xml.dist --report-summary --report-source",
"lint": "phpcs --standard=phpcs.xml.dist",
"phpstan": "phpstan analyse --memory-limit=2G",
"phpstan": "@php tools/phpstan/generate-wordpress-stubs.php && 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",
"phpstan:generate-wordpress-stubs": "@php tools/phpstan/generate-wordpress-stubs.php",
"test": "phpunit",
"test:watch": "phpunit-watcher watch < /dev/tty"
}
Expand Down
2 changes: 1 addition & 1 deletion docs/contributors/code/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<number>`, 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=<number>`, 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:
Expand Down
3 changes: 2 additions & 1 deletion tools/phpstan/base.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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.php, 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.
Expand Down
8 changes: 7 additions & 1 deletion tools/phpstan/baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ parameters:
-
message: '#^Parameter \#1 \$query of method WP_HTML_Tag_Processor\:\:next_tag\(\) expects array\{tag_name\?\: string\|null, match_offset\?\: int\|null, class_name\?\: string\|null, tag_closers\?\: string\|null\}\|null, ''img'' given\.$#'
identifier: argument.type
count: 3
count: 4
path: ../../packages/block-library/src/image/index.php

-
Expand Down Expand Up @@ -600,6 +600,12 @@ parameters:
count: 1
path: ../../packages/block-library/src/navigation/index.php

-
message: '#^Property WP_Block\:\:\$inner_blocks \(WP_Block_List\) in empty\(\) is not falsy\.$#'
identifier: empty.property
count: 1
path: ../../packages/block-library/src/navigation/index.php

-
message: '#^Right side of && is always true\.$#'
identifier: booleanAnd.rightAlwaysTrue
Expand Down
Loading
Loading