Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
55dcf69
PHPStan: Resolve always-true type checks
huzaifaalmesbah Nov 1, 2026
dd63098
Merge remote-tracking branch 'origin/trunk' into fix/phpstan-compat-c…
huzaifaalmesbah Nov 1, 2026
a87323b
Merge branch 'trunk' into fix/phpstan-compat-checks
westonruter Nov 6, 2026
cb1a8e2
Navigation: Narrow layout param instead of widening it to mixed
westonruter Nov 6, 2026
84fac7a
Navigation: Add a return shape for layout custom property declarations
westonruter Nov 6, 2026
9c9615d
Code Quality: Type the script module registry entries
westonruter Nov 6, 2026
9113a61
Navigation: Combine the layout comment with its `@var` docblock
westonruter Nov 6, 2026
2780b69
Code Quality: Add a return shape for viewport media queries
westonruter Nov 6, 2026
aed1887
Navigation: Skip viewport media queries that are not strings
westonruter Nov 6, 2026
9c357e2
Revert "Navigation: Skip viewport media queries that are not strings"
westonruter Nov 6, 2026
07fd94e
PHPStan: Read pull request stubs from the local checkout when it has …
westonruter Nov 6, 2026
86ed716
PHPStan: Generate the WordPress stubs from WordPress/wordpress-develo…
westonruter Nov 6, 2026
b949d7b
Revert "PHPStan: Read pull request stubs from the local checkout when…
westonruter Nov 6, 2026
93a968c
PHPStan: Document generating pull request stubs offline from the chec…
westonruter Nov 6, 2026
e91e01c
Docs: Break up the PHPStan paragraph in the coding guidelines
westonruter Nov 6, 2026
796b7e4
PHPStan: Move the viewport media queries ignore under a level 4 heading
westonruter Nov 6, 2026
7a1ab72
Docs: List the PHPStan commands as alternatives
westonruter Nov 6, 2026
1228084
Docs: Link the files and directories in the PHPStan guidance
westonruter Nov 6, 2026
1cd7ba2
Navigation: Accept any array of layout values instead of asserting st…
westonruter Nov 6, 2026
23d7758
Merge branch 'trunk' into fix/phpstan-compat-checks
westonruter Nov 6, 2026
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
35 changes: 34 additions & 1 deletion docs/contributors/code/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,40 @@ 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, 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:
### Static analysis with PHPStan

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/`](https://github.com/WordPress/gutenberg/tree/HEAD/lib), and all the PHP that WordPress core ships from this repository: the PHP in packages such as [`block-library`](/packages/block-library/README.md), and the PHP templates in [`packages/wp-build/templates`](https://github.com/WordPress/gutenberg/tree/HEAD/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.

#### Running the analysis

First run `composer install` and `npm run build`, which generates PHP files that the analysis needs. Then run the analysis in either of these ways:

- `composer phpstan`, with PHP and Composer installed on your computer.
- `npm run typecheck:php`, in the [local environment](/docs/contributors/code/getting-started-with-code-contribution.md#local-wordpress-environment).

#### The baseline

Errors that predate the analysis are recorded in [`tools/phpstan/baseline.neon`](https://github.com/WordPress/gutenberg/blob/HEAD/tools/phpstan/baseline.neon). When a change fixes one of them, PHPStan reports the baseline entry as unmatched. Regenerate the baseline in either of these ways, and commit the result:

- `composer phpstan:baselines`, with PHP and Composer installed on your computer.
- `npm run typecheck:php:baselines`, in the local environment.

#### WordPress stubs

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`](https://github.com/WordPress/gutenberg/blob/HEAD/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.

The stubs are generated by [`tools/phpstan/generate-wordpress-stubs.php`](https://github.com/WordPress/gutenberg/blob/HEAD/tools/phpstan/generate-wordpress-stubs.php), run with `composer phpstan:generate-wordpress-stubs -- <option>`, where the option is one of:

- `--commit=trunk`: Updates the stubs to the latest core trunk.
- `--pr=<number>`: Generates the stubs from a wordpress-develop pull request that a change depends on, so that the analysis knows what the pull request adds.
- `--checkout`: Generates the stubs from the wordpress-develop checkout that Gutenberg is in, including its uncommitted changes, while working on both. Those stubs cannot be generated in CI, so use `--pr` before pushing.
- `--checkout --pr=<number>`: Once the pull request's branch is checked out with no uncommitted changes and pushed, records the same source as `--pr` without fetching anything.

After generating them, run `composer update` and `composer phpstan:baselines`, and commit the updated [`composer.json`](https://github.com/WordPress/gutenberg/blob/HEAD/tools/phpstan/wordpress-stubs/composer.json) with the change. When the stubs come from a pull request, regenerate them with `--commit=trunk` once the core change is committed, before merging.

#### Local configuration

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`](https://github.com/WordPress/gutenberg/blob/HEAD/phpstan.neon.dist). The tests in [`phpunit/`](https://github.com/WordPress/gutenberg/tree/HEAD/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
8 changes: 7 additions & 1 deletion lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,13 @@ class WP_Theme_JSON_Gutenberg {
*
* @type bool $include_desktop Whether to include the desktop media query. Default false.
* }
* @return array Responsive media queries.
* @return array<string, string> Responsive media queries, keyed by style-state name.
*
* @phpstan-return array{
* '@mobile'?: non-falsy-string,
* '@tablet'?: non-falsy-string,
* '@desktop'?: non-falsy-string,
* }
*/
public static function get_viewport_media_queries( $viewport_settings = null, $options = array() ) {
$breakpoints = self::sanitize_viewport_settings( $viewport_settings );
Expand Down
3 changes: 2 additions & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,12 @@ function gutenberg_define_interactivity_modules_support() {
return;
}

/** @var list<array{ id: string, path: string, asset: string, min_only?: true }> $modules */
$modules = require $modules_registry_file;

// Add client navigation support to block library modules.
foreach ( $modules as $module ) {
if ( str_starts_with( $module['id'], '@wordpress/block-library' ) && method_exists( 'WP_Interactivity_API', 'add_client_navigation_support_to_script_module' ) ) {
if ( str_starts_with( $module['id'], '@wordpress/block-library' ) ) {

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.

This makes sense because Gutenberg requires WP 7.0, and WP_Interactivity_API::add_client_navigation_support_to_script_module() was introduced in WP 6.9.

wp_interactivity()->add_client_navigation_support_to_script_module( $module['id'] );
}
}
Expand Down
25 changes: 19 additions & 6 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ function block_core_navigation_get_submenu_visibility( $attributes ) {
*
* @since 7.1.0
*
* @param array $layout Layout configuration.
* @return array Navigation layout custom property declarations.
* @param array<mixed> $layout Layout configuration.
* @return array<string, string> Navigation layout custom property declarations.
*
* @phpstan-return array{
* '--navigation-layout-justification-setting': 'flex-start'|'center'|'flex-end'|'space-between',
* '--navigation-layout-direction': 'column'|'row',
* '--navigation-layout-wrap': 'nowrap'|'wrap',
* '--navigation-layout-justify': 'initial'|'flex-start'|'center'|'flex-end'|'space-between',
* '--navigation-layout-align': 'flex-start'|'center'|'flex-end',
* }
*/
function block_core_navigation_get_layout_custom_property_declarations( $layout ) {
$justification_values = array(
Expand All @@ -55,25 +63,30 @@ function block_core_navigation_get_layout_custom_property_declarations( $layout
'right' => 'flex-end',
'space-between' => 'space-between',
);
$justify_content = is_array( $layout ) ? ( $layout['justifyContent'] ?? 'left' ) : 'left';
$justify_content = $layout['justifyContent'] ?? 'left';
if ( ! is_string( $justify_content ) || ! isset( $justification_values[ $justify_content ] ) ) {
$justify_content = 'left';
}

$justification = $justification_values[ $justify_content ];
$is_vertical = is_array( $layout ) && 'vertical' === ( $layout['orientation'] ?? null );
$is_vertical = 'vertical' === ( $layout['orientation'] ?? null );
$align = 'center';
$justify = $justification;

if ( $is_vertical ) {
$align = in_array( $justify_content, array( 'center', 'right' ), true ) ? $justification : 'flex-start';
$vertical_align_values = array(
'center' => 'center',
'right' => 'flex-end',
);

$align = $vertical_align_values[ $justify_content ] ?? 'flex-start';
$justify = 'left' === $justify_content ? 'initial' : $justification;
}

return array(
'--navigation-layout-justification-setting' => $justification,
'--navigation-layout-direction' => $is_vertical ? 'column' : 'row',
'--navigation-layout-wrap' => is_array( $layout ) && 'nowrap' === ( $layout['flexWrap'] ?? null ) ? 'nowrap' : 'wrap',
'--navigation-layout-wrap' => 'nowrap' === ( $layout['flexWrap'] ?? null ) ? 'nowrap' : 'wrap',
'--navigation-layout-justify' => $justify,
'--navigation-layout-align' => $align,
);
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ parameters:
message: '#^WP_REST_Request(?:<.+>)? does not accept .+\.$#'
identifier: offsetAssign.valueType
reportUnmatched: false

# Level 4:
# The Navigation block checks for WP_Theme_JSON_Gutenberg::get_viewport_media_queries() before falling back to
# WP_Theme_JSON. PHPStan sees the Gutenberg class as always defined, but the block library also runs in
# WordPress Core, where it is not, so the check is needed there.
-
message: '#^Call to function method_exists\(\) with ''WP_Theme_JSON…'' and ''get_viewport_media…'' will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
path: packages/block-library/src/navigation/index.php
18 changes: 0 additions & 18 deletions tools/phpstan/baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ parameters:
count: 1
path: ../../lib/class-wp-rest-global-styles-controller-gutenberg.php

-
message: '#^Call to function method_exists\(\) with ''WP_Interactivity_API'' and ''add_client…'' will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 1
path: ../../lib/client-assets.php

-
message: '#^Access to an undefined property WP_Post\:\:\$content\.$#'
identifier: property.notFound
Expand Down Expand Up @@ -48,18 +42,6 @@ parameters:
count: 1
path: ../../lib/experimental/navigation-theme-opt-in.php

-
message: '#^Call to function is_array\(\) with array\<mixed\> will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 2
path: ../../packages/block-library/src/navigation/index.php

-
message: '#^Call to function method_exists\(\) with ''WP_Theme_JSON…'' and ''get_viewport_media…'' will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 1
path: ../../packages/block-library/src/navigation/index.php

-
message: '#^Function block_core_navigation_get_classic_menu_fallback\(\) should return object but return statement is missing\.$#'
identifier: return.missing
Expand Down
6 changes: 5 additions & 1 deletion tools/phpstan/generate-wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
* checkout that Gutenberg is in, such as at src/wp-content/plugins/gutenberg.
* Add `--pr` to name the pull request that the checkout is working on. Stubs
* generated from uncommitted changes cannot be generated again elsewhere, such
* as in CI, so generate them with `--pr` or `--commit` before pushing.
* as in CI, so generate them with `--pr` or `--commit` before pushing. Without
* uncommitted changes, the checkout's commit is recorded along with the pull
* request, as `--pr` records it, so when the pull request's branch is checked
* out and pushed, `--checkout --pr` generates the same stubs as `--pr` without
* fetching anything.
*
* A commit is read from the wordpress-develop checkout that Gutenberg is in when it has the commit,
* and is fetched from GitHub otherwise. The options record the new source in composer.json, which is
Expand Down
8 changes: 4 additions & 4 deletions tools/phpstan/wordpress-stubs/composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "php-stubs/wordpress-stubs",
"version": "7.2.0-beta4.65318",
"description": "WordPress stubs generated from https://github.com/WordPress/wordpress-develop/commit/28d2f500094cd41b3186748946c912cb78f8e021 (r65318), in place of the released package.",
"version": "7.2.0-beta4.65320",
"description": "WordPress stubs generated from https://github.com/WordPress/wordpress-develop/commit/27307a20f9ba784dadea277487f5a99d449ead99 (r65320), in place of the released package.",
"license": "GPL-2.0-or-later",
"extra": {
"source": {
"commit": "28d2f500094cd41b3186748946c912cb78f8e021",
"revision": 65318
"commit": "27307a20f9ba784dadea277487f5a99d449ead99",
"revision": 65320
}
}
}
Loading