Skip to content

Sync gutenberg_get_block_style_variation_name_from_class() with core - #68396

Open
justlevine wants to merge 3 commits into
WordPress:trunkfrom
justlevine:fix/matches-gutenberg_get_block_style_variation_name_from_class
Open

justlevine wants to merge 3 commits into
WordPress:trunkfrom
justlevine:fix/matches-gutenberg_get_block_style_variation_name_from_class

Conversation

@justlevine

Copy link
Copy Markdown
Contributor

What?

This PR fixes an issue in gutenberg_get_block_style_variation_name_from_class() where an array would be returned even if the variation name was not matched in the CSS class string.

This is due to the fact that preg_match_all() will always set $matches[1], and instead uses an empty string if no match is found.

Why?

While this issue was surfaced by PHPStan (via #66693) it can be remediated independently as part of #66598

How?

Testing Instructions

Testing Instructions for Keyboard

Screenshots or screencast

Before After

@github-actions

Copy link
Copy Markdown

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.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Technical Prototype, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core, Gutenberg Plugin.
  • Labels found: .

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.

@justlevine
justlevine force-pushed the fix/matches-gutenberg_get_block_style_variation_name_from_class branch from 7520a43 to a922df0 Compare April 18, 2025 22:57
westonruter and others added 2 commits October 24, 2026 21:53
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rather than turning an empty result into `null`, drop the redundant `??`
and return `$matches[1]`, which `preg_match_all()` always populates with
an array. This syncs `gutenberg_get_block_style_variation_name_from_class()`
with `wp_get_block_style_variation_name_from_class()` in WordPress core as
of r63890, including its refined types: a `string|null` parameter, a native
`?array` return type, and a conditional return type.

Returning `null` for a string without a variation would contradict core,
whose unit tests expect an empty array in that case. The only caller, in
`gutenberg_render_block_style_variation_support_styles()`, checks
`! $variations`, which treats both the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@westonruter westonruter added No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Type] Code Quality Issues or PRs that relate to code quality labels Oct 25, 2026
@westonruter westonruter changed the title fix: check contents of $matches[1] in gutenberg_get_block_style_variation_name_from_class() Sync gutenberg_get_block_style_variation_name_from_class() with core Oct 25, 2026
@westonruter

Copy link
Copy Markdown
Member

🤖 Comment by Claude Opus 5.5

trunk was merged in, and 632f393 replaces the approach here with a sync of wp_get_block_style_variation_name_from_class() as it stands in core after r63890 (Core-66162). Core returns $matches[1] as-is, since preg_match_all() always populates it with an array, so the ?? is dropped rather than turning an empty result into null. Core's unit tests expect an empty array for a string without a variation, so returning null there would diverge from core. The only caller, gutenberg_render_block_style_variation_support_styles(), checks ! $variations, which treats both the same.

The docblock and signature now match core too: a string|null parameter, a native ?array return type, and a conditional @phpstan-return that resolves to list<non-empty-string> when a string is passed. This fixes the two PHPStan level 5 errors on this function and introduces none. Since the function is now identical to core's, the No Core Sync Required label was added.

@github-actions

Copy link
Copy Markdown

🤖 PR meta 🤖

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: justlevine <justlevine@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@westonruter

Copy link
Copy Markdown
Member

Only difference between core and Gutenberg now is the function name itself:

--- core.txt	2026-10-25 13:36:57
+++ gutenberg.txt	2026-10-25 13:36:35
@@ -7,7 +7,7 @@
  * @return string[]|null The block style variation names (empty if none found), or null if a string was not supplied.
  * @phpstan-return ( $class_string is string ? list<non-empty-string> : null )
  */
-function wp_get_block_style_variation_name_from_class( $class_string ): ?array {
+function gutenberg_get_block_style_variation_name_from_class( $class_string ): ?array {
 	if ( ! is_string( $class_string ) ) {
 		return null;
 	}

@westonruter westonruter added [Type] Code Quality Issues or PRs that relate to code quality and removed [Type] Code Quality Issues or PRs that relate to code quality labels Oct 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants