WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/09/2021 12:38:36 PM (6 weeks ago)
Author:
hellofromTonya
Message:

Code Modernization: Fix last parameter name mismatches for parent/child classes in Walker::start_el().

The parent class uses $current_object_id while most of the child classes use $id. As the parent class' is more descriptive, renaming the last parameter in each of child class.

Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.

Changes for readability:

  • @since clearly specifies the original parameter name and its new name as well as why the change happened.
  • In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
  • In cases where the original parameter name was too generic or misleading, renamed (when reassigning) to a more descriptive name for use within the method.

Follow-up to [7737], [8900], [8970], [14248], [15077], [16100], [25642], [25644], [37051], [37054], [37056], [46271], [47189], [51739].

Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-walker-page.php

    r51739 r51779  
    9494     * @see Walker::start_el()
    9595     * @since 2.1.0
    96      * @since 5.9.0 Renamed `$page` to `$data_object` to match parent class for PHP 8 named parameter support.
    97      *
    98      * @param string  $output       Used to append additional content. Passed by reference.
    99      * @param WP_Post $data_object  Page data object.
    100      * @param int     $depth        Optional. Depth of page. Used for padding. Default 0.
    101      * @param array   $args         Optional. Array of arguments. Default empty array.
    102      * @param int     $current_page Optional. Page ID. Default 0.
    103      */
    104     public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_page = 0 ) {
     96     * @since 5.9.0 Renamed `$page` to `$data_object` and `$current_page` to `$current_object_id`
     97     *              to match parent class for PHP 8 named parameter support.
     98     *
     99     * @param string  $output            Used to append additional content. Passed by reference.
     100     * @param WP_Post $data_object       Page data object.
     101     * @param int     $depth             Optional. Depth of page. Used for padding. Default 0.
     102     * @param array   $args              Optional. Array of arguments. Default empty array.
     103     * @param int     $current_object_id Optional. ID of the current page. Default 0.
     104     */
     105    public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
    105106        // Restores the more descriptive, specific name for use within this method.
    106         $page = $data_object;
     107        $page            = $data_object;
     108        $current_page_id = $current_object_id;
    107109
    108110        if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
     
    125127        }
    126128
    127         if ( ! empty( $current_page ) ) {
    128             $_current_page = get_post( $current_page );
     129        if ( ! empty( $current_page_id ) ) {
     130            $_current_page = get_post( $current_page_id );
    129131
    130132            if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
     
    132134            }
    133135
    134             if ( $page->ID == $current_page ) {
     136            if ( $page->ID == $current_page_id ) {
    135137                $css_class[] = 'current_page_item';
    136138            } elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
     
    148150         * @see wp_list_pages()
    149151         *
    150          * @param string[] $css_class    An array of CSS classes to be applied to each list item.
    151          * @param WP_Post  $page         Page data object.
    152          * @param int      $depth        Depth of page, used for padding.
    153          * @param array    $args         An array of arguments.
    154          * @param int      $current_page ID of the current page.
     152         * @param string[] $css_class       An array of CSS classes to be applied to each list item.
     153         * @param WP_Post  $page            Page data object.
     154         * @param int      $depth           Depth of page, used for padding.
     155         * @param array    $args            An array of arguments.
     156         * @param int      $current_page_id ID of the current page.
    155157         */
    156         $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
     158        $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page_id ) );
    157159        $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
    158160
     
    167169        $atts                 = array();
    168170        $atts['href']         = get_permalink( $page->ID );
    169         $atts['aria-current'] = ( $page->ID == $current_page ) ? 'page' : '';
     171        $atts['aria-current'] = ( $page->ID == $current_page_id ) ? 'page' : '';
    170172
    171173        /**
     
    180182         *     @type string $aria-current The aria-current attribute.
    181183         * }
    182          * @param WP_Post $page         Page data object.
    183          * @param int     $depth        Depth of page, used for padding.
    184          * @param array   $args         An array of arguments.
    185          * @param int     $current_page ID of the current page.
     184         * @param WP_Post $page            Page data object.
     185         * @param int     $depth           Depth of page, used for padding.
     186         * @param array   $args            An array of arguments.
     187         * @param int     $current_page_id ID of the current page.
    186188         */
    187         $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
     189        $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page_id );
    188190
    189191        $attributes = '';
Note: See TracChangeset for help on using the changeset viewer.