Make WordPress Core

Changeset 54362


Ignore:
Timestamp:
09/30/2022 12:38:58 AM (19 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Fix null to non-nullable deprecation in WP_Theme_JSON::get_property_value().

This commit aims to fix errors caused by incorrect usage of the strncmp() function inside the WP_Theme_JSON::get_property_value() method on PHP 8.1 and above.

Some history of the affected code:

  • [50973] introduced the WP_Theme_JSON::get_property_value() method.
  • [54162] removed the $default parameter from the _wp_array_get() call in the method.

With the latter change, the default value that is returned if the path does not exist within the array, or if $array or $path are not arrays, became null instead of an empty string. Since null would then be unintentionally passed to the strncmp() function further in the code, this caused ~35 errors in the test suite along the lines of:

1) Tests_Blocks_Editor::test_get_block_editor_settings_theme_json_settings
strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated

/var/www/src/wp-includes/class-wp-theme-json.php:1754
/var/www/src/wp-includes/class-wp-theme-json.php:1641
/var/www/src/wp-includes/class-wp-theme-json.php:2066
/var/www/src/wp-includes/class-wp-theme-json.php:1002
/var/www/src/wp-includes/class-wp-theme-json.php:898
/var/www/src/wp-includes/global-styles-and-settings.php:140
/var/www/src/wp-includes/block-editor.php:421
/var/www/tests/phpunit/tests/blocks/editor.php:388
/var/www/vendor/bin/phpunit:123

This commit includes:

  • Restoring the $default value for _wp_array_get() call.
  • Adding an early return if the value is an empty string or null.
  • Adding a dedicated unit test to ensure that the method returns a string for invalid paths or null values.

Follow-up to [50973], [54162].

Props antonvlasenko, jrf, imadarshakshat, SergeyBiryukov.
Fixes #56620.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r54360 r54362  
    17101710     */
    17111711    protected static function get_property_value( $styles, $path, $theme_json = null ) {
    1712         $value = _wp_array_get( $styles, $path );
     1712        $value = _wp_array_get( $styles, $path, '' );
     1713
     1714        if ( '' === $value || null === $value ) {
     1715            // No need to process the value further.
     1716            return '';
     1717        }
    17131718
    17141719        /*
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r54272 r54362  
    29982998
    29992999    /**
     3000     * Tests that get_property_value() static method returns an empty string
     3001     * if the path is invalid or the value is null.
     3002     *
     3003     * Also, tests that PHP 8.1 "passing null to non-nullable" deprecation notice
     3004     * is not thrown when passing the value to strncmp() in the method.
     3005     *
     3006     * The notice that we should not see:
     3007     * `Deprecated: strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated`.
     3008     *
     3009     * @dataProvider data_get_property_value_should_return_string_for_invalid_paths_or_null_values
     3010     *
     3011     * @ticket 56620
     3012     *
     3013     * @covers WP_Theme_JSON::get_property_value
     3014     *
     3015     * @param array $styles An array with style definitions.
     3016     * @param array $path   Path to the desired properties.
     3017     *
     3018     */
     3019    public function test_get_property_value_should_return_string_for_invalid_paths_or_null_values( $styles, $path ) {
     3020        $reflection_class = new ReflectionClass( WP_Theme_JSON::class );
     3021
     3022        $get_property_value_method = $reflection_class->getMethod( 'get_property_value' );
     3023        $get_property_value_method->setAccessible( true );
     3024        $result = $get_property_value_method->invoke( null, $styles, $path );
     3025
     3026        $this->assertSame( '', $result );
     3027    }
     3028
     3029    /**
     3030     * Data provider for test_get_property_value_should_return_string_for_invalid_paths_or_null_values().
     3031     *
     3032     * @return array
     3033     */
     3034    public function data_get_property_value_should_return_string_for_invalid_paths_or_null_values() {
     3035        return array(
     3036            'empty string' => array(
     3037                'styles' => array(),
     3038                'path'   => array( 'non_existent_path' ),
     3039            ),
     3040            'null'         => array(
     3041                'styles' => array( 'some_null_value' => null ),
     3042                'path'   => array( 'some_null_value' ),
     3043            ),
     3044        );
     3045    }
     3046
     3047    /**
    30003048     * Testing that dynamic properties in theme.json that refer to other dynamic properties in a loop
    30013049     * should be left untouched.
     
    30843132        $this->assertSame( $expected, $theme_json->get_stylesheet() );
    30853133    }
    3086 
    30873134
    30883135    /**
Note: See TracChangeset for help on using the changeset viewer.