Repository navigation
Style Engine: Add PHPStan types for style definitions and avoid fatal errors when storing rules - #86468
Conversation
…ADATA
Wrap the docblock mentions of the constant in
`{@see self::BLOCK_STYLE_DEFINITIONS_METADATA}` so the developer docs
link to it. `self::` keeps the reference valid after the build renames
the class with the `_Gutenberg` suffix.
Also qualify the mention in the inline comment in `get_classnames()`
and align its asterisks.
Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: 1064defd-b2b0-40d4-bfca-0c5b60899c05
Add a `Style_Definition` PHPStan type alias to `WP_Style_Engine`, describing one entry of `BLOCK_STYLE_DEFINITIONS_METADATA`, and use it for the constant and for every method that receives a definition. This gives PHPStan the shapes of `property_keys`, `path`, `css_vars`, `classnames`, and `value_func`. Document the remaining raw style value parameters as `mixed`, since `parse_block_styles()` passes whatever is found at the definition's `path`, and the parsers already check its type. Also correct the `classnames` entry of the `parse_block_styles()` return value, which is an array rather than a space-separated string, matching core. With the types in place, a few small code changes keep the analysis sound without changing behavior for valid definitions: * Read the sibling definition in `get_individual_property_css_declarations()` from the constant directly, instead of through `_wp_array_get()`, which loses its type. * Return early from `get_css_declarations()` when a definition has neither a callable `value_func` nor `property_keys`, rather than reading an undefined index. * Only build a `url()` value in `get_url_or_value_css_declaration()` when the value is an array with a string `url`. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 1064defd-b2b0-40d4-bfca-0c5b60899c05
`WP_Style_Engine::store_css_rule()` rejects an empty selector, but `WP_Style_Engine_CSS_Rules_Store::add_rule()` trims the selector and returns nothing when the result is empty. A selector made only of whitespace therefore passed the first check and then called `add_declarations()` on null, which is a fatal error. It is reachable from `wp_style_engine_get_stylesheet_from_css_rules()` when a `context` is passed. Skip storing the rule when no store or rule is returned, and declare the method's `void` return type. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 1064defd-b2b0-40d4-bfca-0c5b60899c05
`get_css_declarations()` and `get_individual_property_css_declarations()` are documented to return `string[]`, but they passed raw block style values through unchanged, so a numeric value such as `textColumns: 2` or a padding side of `5` ended up in the declarations as an integer. `WP_Style_Engine_CSS_Declarations::add_declaration()` already drops non-string values, so they never reached the compiled CSS; they only leaked into the `declarations` array returned by `wp_style_engine_get_styles()`. Skip non-string values in both methods, so the return type holds. The compiled CSS is unchanged. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 1064defd-b2b0-40d4-bfca-0c5b60899c05
Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 1064defd-b2b0-40d4-bfca-0c5b60899c05
🤖 PR meta 🤖🎉 PropsIf 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:
Updated as activity occurs, without notifying anyone named here. Add the ⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Navigates the items list via UP/DOWN arrow keys in
|
|
Tested on WordPress 7.1.4 / PHP 8.3, running this branch's
|
| * @covers ::gutenberg_style_engine_get_styles | ||
| */ | ||
| public function test_should_omit_non_string_values_from_declarations() { | ||
| $block_styles = array( |
There was a problem hiding this comment.
Same as WordPress/wordpress-develop#15823 (comment)
Maybe add border.top.width: 5 to cover the individual properties for that style?
optional
There was a problem hiding this comment.
Could you amend the PR with what you had in mind?
There was a problem hiding this comment.
No probs. It's a tiny change to the test. Feel free to revert.
There was a problem hiding this comment.
Sorry, I realized my original comment wasn't clear at all. Probably should have got Claude to write it ![]()
border sides run through get_individual_property_css_declarations(), which this PR changes so just a cheap way to test the string omission.
… errors when storing rules (WordPress#86468) * Style Engine: Link docblock references to BLOCK_STYLE_DEFINITIONS_METADATA Wrap the docblock mentions of the constant in `{@see self::BLOCK_STYLE_DEFINITIONS_METADATA}` so the developer docs link to it. `self::` keeps the reference valid after the build renames the class with the `_Gutenberg` suffix. Also qualify the mention in the inline comment in `get_classnames()` and align its asterisks. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Style Engine: Add a PHPStan type for style definitions Add a `Style_Definition` PHPStan type alias to `WP_Style_Engine`, describing one entry of `BLOCK_STYLE_DEFINITIONS_METADATA`, and use it for the constant and for every method that receives a definition. This gives PHPStan the shapes of `property_keys`, `path`, `css_vars`, `classnames`, and `value_func`. Document the remaining raw style value parameters as `mixed`, since `parse_block_styles()` passes whatever is found at the definition's `path`, and the parsers already check its type. Also correct the `classnames` entry of the `parse_block_styles()` return value, which is an array rather than a space-separated string, matching core. With the types in place, a few small code changes keep the analysis sound without changing behavior for valid definitions: * Read the sibling definition in `get_individual_property_css_declarations()` from the constant directly, instead of through `_wp_array_get()`, which loses its type. * Return early from `get_css_declarations()` when a definition has neither a callable `value_func` nor `property_keys`, rather than reading an undefined index. * Only build a `url()` value in `get_url_or_value_css_declaration()` when the value is an array with a string `url`. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Style Engine: Avoid a fatal error for a whitespace-only selector `WP_Style_Engine::store_css_rule()` rejects an empty selector, but `WP_Style_Engine_CSS_Rules_Store::add_rule()` trims the selector and returns nothing when the result is empty. A selector made only of whitespace therefore passed the first check and then called `add_declarations()` on null, which is a fatal error. It is reachable from `wp_style_engine_get_stylesheet_from_css_rules()` when a `context` is passed. Skip storing the rule when no store or rule is returned, and declare the method's `void` return type. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Style Engine: Only return string values as CSS declarations `get_css_declarations()` and `get_individual_property_css_declarations()` are documented to return `string[]`, but they passed raw block style values through unchanged, so a numeric value such as `textColumns: 2` or a padding side of `5` ended up in the declarations as an integer. `WP_Style_Engine_CSS_Declarations::add_declaration()` already drops non-string values, so they never reached the compiled CSS; they only leaked into the `declarations` array returned by `wp_style_engine_get_styles()`. Skip non-string values in both methods, so the return type holds. The compiled CSS is unchanged. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Add this PR to the Style Engine backport changelog entry Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Style Engine: Cover non-string individual border declarations --------- Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: dknauss <dpknauss@git.wordpress.org>
What?
Follow up to #86105. See #86088.
Improves the PHPStan types in
WP_Style_Enginebeyond level 5, and fixes two problems the stricter types surfaced: a fatal error when storing a rule with a whitespace-only selector, and non-string values leaking into the returned CSS declarations.Core backport: WordPress/wordpress-develop#15823 (Core-65817)
Why?
At level 10,
packages/style-engine/src/class-wp-style-engine.phphas 38 PHPStan errors on trunk, almost all from the untyped style definition arrays. This PR brings it to 5, all of themmissingType.iterableValueon$optionsparameters. Core does not report those, because its PHPStan setup derives array shapes from the existing@typehashes.How?
Types
Style_DefinitionPHPStan type alias for one entry ofBLOCK_STYLE_DEFINITIONS_METADATA, and use it for the constant and for every method that receives a definition. This replaces the inline shape onget_classnames().$style_valueparameters asmixed, since the parsers check the type themselves, and$block_stylesasarray<string, mixed>.classnamesentry of theparse_block_styles()return value, which is an array, not a space-separated string, matching Core.{@see self::BLOCK_STYLE_DEFINITIONS_METADATA}. Usingself::keeps the reference valid after the build renames the class toWP_Style_Engine_Gutenberg.Fixes
store_css_rule()rejects an empty selector, butWP_Style_Engine_CSS_Rules_Store::add_rule()trims it and returns nothing when the result is empty. A selector of only whitespace therefore calledadd_declarations()on null, a fatal error reachable fromwp_style_engine_get_stylesheet_from_css_rules()when acontextis passed. A non-string store name fails the same way, since the store lookup returns nothing. Both are now skipped, and the method declaresvoid.get_css_declarations()andget_individual_property_css_declarations()are documented to returnstring[], but passed raw values through, sotextColumns: 2or a padding side of5came back as integers. They now skip non-string values.WP_Style_Engine_CSS_Declarations::add_declaration()already dropped them, so the compiled CSS is unchanged; only thedeclarationsarray returned bywp_style_engine_get_styles()loses them._wp_array_get(), a definition withoutproperty_keysreturns no declarations instead of reading an undefined index, andurl()is only built from a stringurl.Testing Instructions
composer phpstanand confirm it passes.npm run test:unit:php -- -- --filter WP_Style_Engine_Test. Two new tests cover the whitespace selector and the non-string values.packages/style-engine/src/class-wp-style-engine.phpand compare with trunk.Testing Instructions for Keyboard
Not applicable. There are no UI changes.
Screenshots or screencast
Not applicable.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (medium effort)
Used for: Investigating the PHPStan errors, drafting the type changes, fixes, and tests, and syncing them to the Core backport; reviewed and directed by me.