Make WordPress Core

Changeset 63368


Ignore:
Timestamp:
08/27/2026 08:02:54 PM (6 weeks ago)
Author:
westonruter
Message:

Code Quality: Call private static methods via self::.

The classes WP_Classic_To_Block_Menu_Converter, WP_Navigation_Fallback, WP_Theme_JSON_Resolver, and WP_Font_Face_Resolver reached their own private static helpers through static::. Late static binding is problematic here. Because the helpers are private they are not an extension point, yet static:: still resolves against the called class, so a subclass that declares a method of the same name either hijacks the call when that method is protected or public, or triggers a fatal error when it is private. With self:: the binding of the call goes to the declaring class, which is what the visibility already promises.

In practice the resolution is unchanged, since core always invokes these classes by their own name and none of them is subclassed in core. Only calls to private methods are converted; static:: is retained where it targets a protected or public member and late static binding is meaningful. This clears the 20 remaining staticClassAccess.privateMethod entries, covering 26 call sites, from the PHPStan baseline.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13072.
Follow-up to r63020.

Props dpantazis, dhruvang21.
See #65817.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-classic-to-block-menu-converter.php

    r61431 r63368  
    4343                _wp_menu_item_classes_by_context( $menu_items );
    4444
    45                 $menu_items_by_parent_id = static::group_by_parent_id( $menu_items );
     45                $menu_items_by_parent_id = self::group_by_parent_id( $menu_items );
    4646
    4747                $first_menu_item = $menu_items_by_parent_id[0] ?? array();
    4848
    49                 $inner_blocks = static::to_blocks(
     49                $inner_blocks = self::to_blocks(
    5050                        $first_menu_item,
    5151                        $menu_items_by_parent_id
    … …  
    118118
    119119                        $block['innerBlocks']  = isset( $menu_items_by_parent_id[ $menu_item->ID ] )
    120                         ? static::to_blocks( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id )
     120                        ? self::to_blocks( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id )
    121121                        : array();
    122122                        $block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] );
  • trunk/src/wp-includes/class-wp-navigation-fallback.php

    r60325 r63368  
    7878                $should_create_fallback = apply_filters( 'wp_navigation_should_create_fallback', true );
    7979
    80                 $fallback = static::get_most_recently_published_navigation();
     80                $fallback = self::get_most_recently_published_navigation();
    8181
    8282                if ( $fallback || ! $should_create_fallback ) {
    … …  
    8484                }
    8585
    86                 $fallback = static::create_classic_menu_fallback();
     86                $fallback = self::create_classic_menu_fallback();
    8787
    8888                if ( $fallback && ! is_wp_error( $fallback ) ) {
    8989                        // Return the newly created fallback post object which will now be the most recently created navigation menu.
    90                         return $fallback instanceof WP_Post ? $fallback : static::get_most_recently_published_navigation();
    91                 }
    92 
    93                 $fallback = static::create_default_fallback();
     90                        return $fallback instanceof WP_Post ? $fallback : self::get_most_recently_published_navigation();
     91                }
     92
     93                $fallback = self::create_default_fallback();
    9494
    9595                if ( $fallback && ! is_wp_error( $fallback ) ) {
    9696                        // Return the newly created fallback post object which will now be the most recently created navigation menu.
    97                         return $fallback instanceof WP_Post ? $fallback : static::get_most_recently_published_navigation();
     97                        return $fallback instanceof WP_Post ? $fallback : self::get_most_recently_published_navigation();
    9898                }
    9999
    … …  
    139139        private static function create_classic_menu_fallback() {
    140140                // See if we have a classic menu.
    141                 $classic_nav_menu = static::get_fallback_classic_menu();
     141                $classic_nav_menu = self::get_fallback_classic_menu();
    142142
    143143                if ( ! $classic_nav_menu ) {
    … …  
    185185                }
    186186
    187                 $nav_menu = static::get_nav_menu_at_primary_location();
     187                $nav_menu = self::get_nav_menu_at_primary_location();
    188188
    189189                if ( $nav_menu ) {
    … …  
    191191                }
    192192
    193                 $nav_menu = static::get_nav_menu_with_primary_slug( $classic_nav_menus );
     193                $nav_menu = self::get_nav_menu_with_primary_slug( $classic_nav_menus );
    194194
    195195                if ( $nav_menu ) {
    … …  
    197197                }
    198198
    199                 return static::get_most_recently_created_nav_menu( $classic_nav_menus );
     199                return self::get_most_recently_created_nav_menu( $classic_nav_menus );
    200200        }
    201201
    … …  
    270270        private static function create_default_fallback() {
    271271
    272                 $default_blocks = static::get_default_fallback_blocks();
     272                $default_blocks = self::get_default_fallback_blocks();
    273273
    274274                // Create a new navigation menu from the fallback blocks.
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r61637 r63368  
    280280                         *
    281281                         */
    282                         $theme_json_data = static::inject_variations_from_block_style_variation_files( $theme_json_data, $variations );
    283                         $theme_json_data = static::inject_variations_from_block_styles_registry( $theme_json_data );
     282                        $theme_json_data = self::inject_variations_from_block_style_variation_files( $theme_json_data, $variations );
     283                        $theme_json_data = self::inject_variations_from_block_styles_registry( $theme_json_data );
    284284
    285285                        /**
    … …  
    401401                foreach ( $blocks as $block_name => $block_type ) {
    402402                        if ( isset( $block_type->supports['__experimentalStyle'] ) ) {
    403                                 $config['styles']['blocks'][ $block_name ] = static::remove_json_comments( $block_type->supports['__experimentalStyle'] );
     403                                $config['styles']['blocks'][ $block_name ] = self::remove_json_comments( $block_type->supports['__experimentalStyle'] );
    404404                        }
    405405
    … …  
    451451                foreach ( $input_array as $k => $v ) {
    452452                        if ( is_array( $v ) ) {
    453                                 $input_array[ $k ] = static::remove_json_comments( $v );
     453                                $input_array[ $k ] = self::remove_json_comments( $v );
    454454                        }
    455455                }
    … …  
    806806                $template_directory = get_template_directory() . '/styles';
    807807                if ( is_dir( $base_directory ) ) {
    808                         $variation_files = static::recursively_iterate_json( $base_directory );
     808                        $variation_files = self::recursively_iterate_json( $base_directory );
    809809                }
    810810                if ( is_dir( $template_directory ) && $template_directory !== $base_directory ) {
    811                         $variation_files_parent = static::recursively_iterate_json( $template_directory );
     811                        $variation_files_parent = self::recursively_iterate_json( $template_directory );
    812812                        // If the child and parent variation file basename are the same, only include the child theme's.
    813813                        foreach ( $variation_files_parent as $parent_path => $parent ) {
    … …  
    823823                foreach ( $variation_files as $path => $file ) {
    824824                        $decoded_file = self::read_json_file( $path );
    825                         if ( is_array( $decoded_file ) && static::style_variation_has_scope( $decoded_file, $scope ) ) {
     825                        if ( is_array( $decoded_file ) && self::style_variation_has_scope( $decoded_file, $scope ) ) {
    826826                                $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
    827827                                $variation  = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
  • trunk/src/wp-includes/fonts/class-wp-font-face-resolver.php

    r59260 r63368  
    3434                }
    3535
    36                 return static::parse_settings( $settings );
     36                return self::parse_settings( $settings );
    3737        }
    3838
    … …  
    6666                );
    6767
    68                 return static::parse_settings( $settings );
     68                return self::parse_settings( $settings );
    6969        }
    7070
    … …  
    9393                                }
    9494
    95                                 $font_family_name = static::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] );
     95                                $font_family_name = self::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] );
    9696
    9797                                // Skip if no font family is defined.
    … …  
    100100                                }
    101101
    102                                 $fonts[] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name );
     102                                $fonts[] = self::convert_font_face_properties( $definition['fontFace'], $font_family_name );
    103103                        }
    104104                }
    … …  
    144144                        // Converts the "file:./" src placeholder into a theme font file URI.
    145145                        if ( ! empty( $font_face['src'] ) ) {
    146                                 $font_face['src'] = static::to_theme_file_uri( (array) $font_face['src'] );
     146                                $font_face['src'] = self::to_theme_file_uri( (array) $font_face['src'] );
    147147                        }
    148148
    149149                        // Convert camelCase properties into kebab-case.
    150                         $font_face = static::to_kebab_case( $font_face );
     150                        $font_face = self::to_kebab_case( $font_face );
    151151
    152152                        $converted_font_faces[] = $font_face;
  • trunk/tests/phpstan/baselines/staticClassAccess.privateMethod.neon

    r63020 r63368  
    1919parameters:
    2020        ignoreErrors:
    21                 -
    22                         message: '#^Unsafe call to private method WP_Classic_To_Block_Menu_Converter\:\:group_by_parent_id\(\) through static\:\:\.$#'
    23                         identifier: staticClassAccess.privateMethod
    24                         count: 1
    25                         path: ../../../src/wp-includes/class-wp-classic-to-block-menu-converter.php
    26                 -
    27                         message: '#^Unsafe call to private method WP_Classic_To_Block_Menu_Converter\:\:to_blocks\(\) through static\:\:\.$#'
    28                         identifier: staticClassAccess.privateMethod
    29                         count: 2
    30                         path: ../../../src/wp-includes/class-wp-classic-to-block-menu-converter.php
    31                 -
    32                         message: '#^Unsafe call to private method WP_Navigation_Fallback\:\:create_classic_menu_fallback\(\) through static\:\:\.$#'
    33                         identifier: staticClassAccess.privateMethod
    34                         count: 1
    35                         path: ../../../src/wp-includes/class-wp-navigation-fallback.php
    36                 -
    37                         message: '#^Unsafe call to private method WP_Navigation_Fallback\:\:create_default_fallback\(\) through static\:\:\.$#'
    38                         identifier: staticClassAccess.privateMethod
    39                         count: 1
    40                         path: ../../../src/wp-includes/class-wp-navigation-fallback.php
    41                 -
    42                         message: '#^Unsafe call to private method WP_Navigation_Fallback\:\:get_default_fallback_blocks\(\) through static\:\:\.$#'
    43                         identifier: staticClassAccess.privateMethod
    44                         count: 1
    45                         path: ../../../src/wp-includes/class-wp-navigation-fallback.php
    46                 -
    47                         message: '#^Unsafe call to private method WP_Navigation_Fallback\:\:get_fallback_classic_menu\(\) through static\:\:\.$#'
    48                         identifier: staticClassAccess.privateMethod
    49                         count: 1
    50                         path: ../../../src/wp-includes/class-wp-navigation-fallback.php
    51                 -
    52                         message: '#^Unsafe call to private method WP_Navigation_Fallback\:\:get_most_recently_created_nav_menu\(\) through static\:\:\.$#'
    53                         identifier: staticClassAccess.privateMethod
    54                         count: 1
    55                         path: ../../../src/wp-includes/class-wp-navigation-fallback.php
    56                 -
    57                         message: '#^Unsafe call to private method WP_Navigation_Fallback\:\:get_most_recently_published_navigation\(\) through static\:\:\.$#'
    58                         identifier: staticClassAccess.privateMethod
    59                         count: 3
    60                         path: ../../../src/wp-includes/class-wp-navigation-fallback.php
    61                 -
    62                         message: '#^Unsafe call to private method WP_Navigation_Fallback\:\:get_nav_menu_at_primary_location\(\) through static\:\:\.$#'
    63                         identifier: staticClassAccess.privateMethod
    64                         count: 1
    65                         path: ../../../src/wp-includes/class-wp-navigation-fallback.php
    66                 -
    67                         message: '#^Unsafe call to private method WP_Navigation_Fallback\:\:get_nav_menu_with_primary_slug\(\) through static\:\:\.$#'
    68                         identifier: staticClassAccess.privateMethod
    69                         count: 1
    70                         path: ../../../src/wp-includes/class-wp-navigation-fallback.php
    71                 -
    72                         message: '#^Unsafe call to private method WP_Theme_JSON_Resolver\:\:inject_variations_from_block_style_variation_files\(\) through static\:\:\.$#'
    73                         identifier: staticClassAccess.privateMethod
    74                         count: 1
    75                         path: ../../../src/wp-includes/class-wp-theme-json-resolver.php
    76                 -
    77                         message: '#^Unsafe call to private method WP_Theme_JSON_Resolver\:\:inject_variations_from_block_styles_registry\(\) through static\:\:\.$#'
    78                         identifier: staticClassAccess.privateMethod
    79                         count: 1
    80                         path: ../../../src/wp-includes/class-wp-theme-json-resolver.php
    81                 -
    82                         message: '#^Unsafe call to private method WP_Theme_JSON_Resolver\:\:recursively_iterate_json\(\) through static\:\:\.$#'
    83                         identifier: staticClassAccess.privateMethod
    84                         count: 2
    85                         path: ../../../src/wp-includes/class-wp-theme-json-resolver.php
    86                 -
    87                         message: '#^Unsafe call to private method WP_Theme_JSON_Resolver\:\:remove_json_comments\(\) through static\:\:\.$#'
    88                         identifier: staticClassAccess.privateMethod
    89                         count: 2
    90                         path: ../../../src/wp-includes/class-wp-theme-json-resolver.php
    91                 -
    92                         message: '#^Unsafe call to private method WP_Theme_JSON_Resolver\:\:style_variation_has_scope\(\) through static\:\:\.$#'
    93                         identifier: staticClassAccess.privateMethod
    94                         count: 1
    95                         path: ../../../src/wp-includes/class-wp-theme-json-resolver.php
    9621                -
    9722                        message: '#^Unsafe call to private method WP_Theme_JSON\:\:compute_spacing_sizes\(\) through static\:\:\.$#'
    … …  
    16489                        count: 1
    16590                        path: ../../../src/wp-includes/class-wp-theme-json.php
    166                 -
    167                         message: '#^Unsafe call to private method WP_Font_Face_Resolver\:\:convert_font_face_properties\(\) through static\:\:\.$#'
    168                         identifier: staticClassAccess.privateMethod
    169                         count: 1
    170                         path: ../../../src/wp-includes/fonts/class-wp-font-face-resolver.php
    171                 -
    172                         message: '#^Unsafe call to private method WP_Font_Face_Resolver\:\:maybe_parse_name_from_comma_separated_list\(\) through static\:\:\.$#'
    173                         identifier: staticClassAccess.privateMethod
    174                         count: 1
    175                         path: ../../../src/wp-includes/fonts/class-wp-font-face-resolver.php
    176                 -
    177                         message: '#^Unsafe call to private method WP_Font_Face_Resolver\:\:parse_settings\(\) through static\:\:\.$#'
    178                         identifier: staticClassAccess.privateMethod
    179                         count: 2
    180                         path: ../../../src/wp-includes/fonts/class-wp-font-face-resolver.php
    181                 -
    182                         message: '#^Unsafe call to private method WP_Font_Face_Resolver\:\:to_kebab_case\(\) through static\:\:\.$#'
    183                         identifier: staticClassAccess.privateMethod
    184                         count: 1
    185                         path: ../../../src/wp-includes/fonts/class-wp-font-face-resolver.php
    186                 -
    187                         message: '#^Unsafe call to private method WP_Font_Face_Resolver\:\:to_theme_file_uri\(\) through static\:\:\.$#'
    188                         identifier: staticClassAccess.privateMethod
    189                         count: 1
    190                         path: ../../../src/wp-includes/fonts/class-wp-font-face-resolver.php
Note: See TracChangeset for help on using the changeset viewer.