Make WordPress Core

Changeset 63524


Ignore:
Timestamp:
09/08/2026 06:04:10 AM (6 weeks ago)
Author:
westonruter
Message:

Code Quality: Combine registered block styles with array_merge().

In WP_REST_Block_Types_Controller the block styles registered for a block are combined with array_merge(), where wp_parse_args() had been used erroneously on what are two lists. In support of this, a global Block_Style_Properties type alias is introduced for the shape of a registered block style.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13432.
Follow-up to r48173, r59760, r63522.

Props westonruter, swissspidy.
See #65817, #65860.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-styles-registry.php

    r63443 r63524  
    2020         * @since 5.3.0
    2121         *
    22          * @var array[]
     22         * @var array<string, array<string, array<string, mixed>>>
     23         * @phpstan-var array<string, array<string, Block_Style_Properties>>
    2324         */
    2425        private $registered_block_styles = array();
    … …  
    5152         *     Array containing the properties of the style.
    5253         *
    53          *     @type string $name         The identifier of the style used to compute a CSS class.
    54          *     @type string $label        A human-readable label for the style.
    55          *     @type string $inline_style Inline CSS code that registers the CSS class required
    56          *                                for the style.
    57          *     @type string $style_handle The handle to an already registered style that should be
    58          *                                enqueued in places where block styles are needed.
    59          *     @type bool   $is_default   Whether this is the default style for the block type.
    60          *     @type array  $style_data   Theme.json-like object to generate CSS from.
     54         *     @type string               $name         The identifier of the style used to compute a CSS class.
     55         *     @type string               $label        A human-readable label for the style.
     56         *     @type string               $inline_style Inline CSS code that registers the CSS class required
     57         *                                              for the style.
     58         *     @type string               $style_handle The handle to an already registered style that should be
     59         *                                              enqueued in places where block styles are needed.
     60         *     @type bool                 $is_default   Whether this is the default style for the block type.
     61         *     @type array<string, mixed> $style_data   Theme.json-like object to generate CSS from.
    6162         * }
    6263         * @return bool True if the block style was registered with success and false otherwise.
    … …  
    141142         * @param string $block_name       Block type name including namespace.
    142143         * @param string $block_style_name Block style name.
    143          * @return array|null Registered block style properties or `null` if the block style is not registered.
     144         * @return array<string, mixed>|null Registered block style properties or `null` if the block style is not registered.
     145         * @phpstan-return Block_Style_Properties|null
    144146         */
    145147        public function get_registered( $block_name, $block_style_name ) {
    … …  
    156158         * @since 5.3.0
    157159         *
    158          * @return array[] Array of arrays containing the registered block styles properties grouped by block type.
     160         * @return array<string, array<string, array<string, mixed>>> Array of arrays containing the registered block styles
     161         *                                                            properties grouped by block type.
     162         * @phpstan-return array<string, array<string, Block_Style_Properties>>
    159163         */
    160164        public function get_all_registered() {
    … …  
    168172         *
    169173         * @param string $block_name Block type name including namespace.
    170          * @return array[] Array whose keys are block style names and whose values are block style properties.
     174         * @return array<string, array<string, mixed>> Array whose keys are block style names and whose values are
     175         *                                             block style properties.
     176         * @phpstan-return array<string, Block_Style_Properties>
    171177         */
    172178        public function get_registered_styles_for_block( $block_name ) {
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r59970 r63524  
    331331
    332332                if ( rest_is_field_included( 'styles', $fields ) ) {
    333                         $styles         = $this->style_registry->get_registered_styles_for_block( $block_type->name );
    334                         $styles         = array_values( $styles );
    335                         $data['styles'] = wp_parse_args( $styles, $data['styles'] );
     333                        $styles = $this->style_registry->get_registered_styles_for_block( $block_type->name );
     334                        $styles = array_values( $styles );
     335
     336                        /*
     337                         * The loop above assigns this key from rest_sanitize_value_from_schema(), whose
     338                         * return is documented as `mixed|WP_Error`, so what the styles came back as has
     339                         * to be restated here. A WP_Error is why the value is checked rather than cast.
     340                         */
     341                        /**
     342                         * @var array<string, mixed>[] $block_styles
     343                         * @phpstan-var list<Block_Style_Properties> $block_styles
     344                         */
     345                        $block_styles = isset( $data['styles'] ) && is_array( $data['styles'] ) ? $data['styles'] : array();
     346
     347                        $data['styles'] = array_merge( $block_styles, $styles );
    336348                        $data['styles'] = array_filter( $data['styles'] );
    337349                }
  • trunk/tests/phpstan/base.neon

    r63462 r63524  
    277277                        }
    278278                '''
     279
     280                # The properties of a block style as WP_Block_Styles_Registry stores them, which is
     281                # more than its register() hash accepts: `name` is required for a style to be
     282                # registered at all, and `label` is filled in from the name when the caller omits
     283                # it, so both are always present on the way back out. The shape stays open because
     284                # the array is stored verbatim, so a caller's own keys survive — including into the
     285                # REST response, which does not set `additionalProperties` to false. Read from
     286                # procedural files as well as classes, which is why it is declared here.
     287                Block_Style_Properties: '''
     288                        array{
     289                                name: string,
     290                                label: string,
     291                                inline_style?: string,
     292                                style_handle?: string,
     293                                is_default?: bool,
     294                                style_data?: array<string, mixed>,
     295                                ...
     296                        }
     297                '''
  • trunk/tests/phpstan/baselines/argument.type.neon

    r63522 r63524  
    12501250                        path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php
    12511251                -
    1252                         message: '#^Parameter \#1 \$args of function wp_parse_args expects array\<string, mixed\>\|object\|string, list\<array\> given\.$#'
    1253                         identifier: argument.type
    1254                         count: 1
    1255                         path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php
    1256                 -
    12571252                        message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int given\.$#'
    12581253                        identifier: argument.type
Note: See TracChangeset for help on using the changeset viewer.