Make WordPress Core


Ignore:
Timestamp:
12/25/2022 01:10:42 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Rename parameters that use reserved keywords in phpunit/includes/abstract-testcase.php.

While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.

This commit:

  • Renames the $function parameter to $function_name in:
    • WP_UnitTestCase_Base::deprecated_function_run()
    • WP_UnitTestCase_Base::doing_it_wrong_run()
  • Renames the $object parameter to $actual in:
    • WP_UnitTestCase_Base::assertEqualFields()
    • WP_UnitTestCase_Base::assertNonEmptyMultidimensionalArray()

Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952], [54956], [54959], [54960], [54961], [54962], [54964], [54965], [54969], [54970], [54971], [54972], [54996], [55000], [55011], [55013], [55014], [55015].

Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r54865 r55016  
    683683     * @since 6.1.0 Added the `$replacement`, `$version`, and `$message` parameters.
    684684     *
    685      * @param string $function    The deprecated function.
    686      * @param string $replacement The function that should have been called.
    687      * @param string $version     The version of WordPress that deprecated the function.
    688      * @param string $message     Optional. A message regarding the change.
    689      */
    690     public function deprecated_function_run( $function, $replacement, $version, $message = '' ) {
    691         if ( ! isset( $this->caught_deprecated[ $function ] ) ) {
     685     * @param string $function_name The deprecated function.
     686     * @param string $replacement   The function that should have been called.
     687     * @param string $version       The version of WordPress that deprecated the function.
     688     * @param string $message       Optional. A message regarding the change.
     689     */
     690    public function deprecated_function_run( $function_name, $replacement, $version, $message = '' ) {
     691        if ( ! isset( $this->caught_deprecated[ $function_name ] ) ) {
    692692            switch ( current_action() ) {
    693693                case 'deprecated_function_run':
     
    695695                        $message = sprintf(
    696696                            'Function %1$s is deprecated since version %2$s! Use %3$s instead.',
    697                             $function,
     697                            $function_name,
    698698                            $version,
    699699                            $replacement
     
    702702                        $message = sprintf(
    703703                            'Function %1$s is deprecated since version %2$s with no alternative available.',
    704                             $function,
     704                            $function_name,
    705705                            $version
    706706                        );
     
    712712                        $message = sprintf(
    713713                            'Function %1$s was called with an argument that is deprecated since version %2$s! %3$s',
    714                             $function,
     714                            $function_name,
    715715                            $version,
    716716                            $replacement
     
    719719                        $message = sprintf(
    720720                            'Function %1$s was called with an argument that is deprecated since version %2$s with no alternative available.',
    721                             $function,
     721                            $function_name,
    722722                            $version
    723723                        );
     
    729729                        $message = sprintf(
    730730                            'File %1$s is deprecated since version %2$s! Use %3$s instead.',
    731                             $function,
     731                            $function_name,
    732732                            $version,
    733733                            $replacement
     
    736736                        $message = sprintf(
    737737                            'File %1$s is deprecated since version %2$s with no alternative available.',
    738                             $function,
     738                            $function_name,
    739739                            $version
    740740                        ) . ' ' . $message;
     
    746746                        $message = sprintf(
    747747                            'Hook %1$s is deprecated since version %2$s! Use %3$s instead.',
    748                             $function,
     748                            $function_name,
    749749                            $version,
    750750                            $replacement
     
    753753                        $message = sprintf(
    754754                            'Hook %1$s is deprecated since version %2$s with no alternative available.',
    755                             $function,
     755                            $function_name,
    756756                            $version
    757757                        ) . ' ' . $message;
     
    760760            }
    761761
    762             $this->caught_deprecated[ $function ] = $message;
     762            $this->caught_deprecated[ $function_name ] = $message;
    763763        }
    764764    }
     
    770770     * @since 6.1.0 Added the `$message` and `$version` parameters.
    771771     *
    772      * @param string $function The function to add.
    773      * @param string $message  A message explaining what has been done incorrectly.
    774      * @param string $version  The version of WordPress where the message was added.
    775      */
    776     public function doing_it_wrong_run( $function, $message, $version ) {
    777         if ( ! isset( $this->caught_doing_it_wrong[ $function ] ) ) {
     772     * @param string $function_name The function to add.
     773     * @param string $message       A message explaining what has been done incorrectly.
     774     * @param string $version       The version of WordPress where the message was added.
     775     */
     776    public function doing_it_wrong_run( $function_name, $message, $version ) {
     777        if ( ! isset( $this->caught_doing_it_wrong[ $function_name ] ) ) {
    778778            if ( $version ) {
    779779                $message .= ' ' . sprintf( '(This message was added in version %s.)', $version );
    780780            }
    781781
    782             $this->caught_doing_it_wrong[ $function ] = $message;
     782            $this->caught_doing_it_wrong[ $function_name ] = $message;
    783783        }
    784784    }
     
    838838     * @since 5.9.0 Added the `$message` parameter.
    839839     *
    840      * @param object $object  The object to check.
     840     * @param object $actual  The object to check.
    841841     * @param array  $fields  The fields to check.
    842842     * @param string $message Optional. Message to display when the assertion fails.
    843843     */
    844     public function assertEqualFields( $object, $fields, $message = '' ) {
    845         $this->assertIsObject( $object, $message . ' Passed $object is not an object.' );
     844    public function assertEqualFields( $actual, $fields, $message = '' ) {
     845        $this->assertIsObject( $actual, $message . ' Passed $actual is not an object.' );
    846846        $this->assertIsArray( $fields, $message . ' Passed $fields is not an array.' );
    847847        $this->assertNotEmpty( $fields, $message . ' Fields array is empty.' );
    848848
    849849        foreach ( $fields as $field_name => $field_value ) {
    850             $this->assertObjectHasAttribute( $field_name, $object, $message . " Property $field_name does not exist on the object." );
    851             $this->assertSame( $field_value, $object->$field_name, $message . " Value of property $field_name is not $field_value." );
     850            $this->assertObjectHasAttribute( $field_name, $actual, $message . " Property $field_name does not exist on the object." );
     851            $this->assertSame( $field_value, $actual->$field_name, $message . " Value of property $field_name is not $field_value." );
    852852        }
    853853    }
     
    10131013     * @since 5.9.0 Added the `$message` parameter.
    10141014     *
    1015      * @param array  $array   Array to check.
     1015     * @param array  $actual  Array to check.
    10161016     * @param string $message Optional. Message to display when the assertion fails.
    10171017     */
    1018     public function assertNonEmptyMultidimensionalArray( $array, $message = '' ) {
    1019         $this->assertIsArray( $array, $message . ' Value under test is not an array.' );
    1020         $this->assertNotEmpty( $array, $message . ' Array is empty.' );
    1021 
    1022         foreach ( $array as $sub_array ) {
     1018    public function assertNonEmptyMultidimensionalArray( $actual, $message = '' ) {
     1019        $this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
     1020        $this->assertNotEmpty( $actual, $message . ' Array is empty.' );
     1021
     1022        foreach ( $actual as $sub_array ) {
    10231023            $this->assertIsArray( $sub_array, $message . ' Subitem of the array is not an array.' );
    10241024            $this->assertNotEmpty( $sub_array, $message . ' Subitem of the array is empty.' );
Note: See TracChangeset for help on using the changeset viewer.