Make WordPress Core


Ignore:
Timestamp:
09/09/2021 12:38:36 PM (3 years 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-content/themes/twentytwenty/classes/class-twentytwenty-walker-page.php

    r51739 r51779  
    2121         *
    2222         * @since Twenty Twenty 1.0
    23          * @since Twenty Twenty 1.9 Renamed `$page` to `$data_object` to match parent class for PHP 8 named parameter support.
     23         * @since Twenty Twenty 1.9 Renamed `$page` to `$data_object` and `$current_page` to `$current_object_id`
     24         *                          to match parent class for PHP 8 named parameter support.
    2425         *
    2526         * @see Walker::start_el()
    2627         *
    27          * @param string  $output       Used to append additional content. Passed by reference.
    28          * @param WP_Post $data_object  Page data object.
    29          * @param int     $depth        Optional. Depth of page. Used for padding. Default 0.
    30          * @param array   $args         Optional. Array of arguments. Default empty array.
    31          * @param int     $current_page Optional. Page ID. Default 0.
     28         * @param string  $output            Used to append additional content. Passed by reference.
     29         * @param WP_Post $data_object       Page data object.
     30         * @param int     $depth             Optional. Depth of page. Used for padding. Default 0.
     31         * @param array   $args              Optional. Array of arguments. Default empty array.
     32         * @param int     $current_object_id Optional. ID of the current page. Default 0.
    3233         */
    33         public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_page = 0 ) {
     34        public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
    3435            // Restores the more descriptive, specific name for use within this method.
    35             $page = $data_object;
     36            $page            = $data_object;
     37            $current_page_id = $current_object_id;
    3638
    3739            if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
     
    5456            }
    5557
    56             if ( ! empty( $current_page ) ) {
    57                 $_current_page = get_post( $current_page );
     58            if ( ! empty( $current_page_id ) ) {
     59                $_current_page = get_post( $current_page_id );
    5860                if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
    5961                    $css_class[] = 'current_page_ancestor';
    6062                }
    61                 if ( $page->ID === $current_page ) {
     63                if ( $page->ID === $current_page_id ) {
    6264                    $css_class[] = 'current_page_item';
    6365                } elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
     
    6971
    7072            /** This filter is documented in wp-includes/class-walker-page.php */
    71             $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
     73            $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page_id ) );
    7274            $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
    7375
     
    8284            $atts                 = array();
    8385            $atts['href']         = get_permalink( $page->ID );
    84             $atts['aria-current'] = ( $page->ID === $current_page ) ? 'page' : '';
     86            $atts['aria-current'] = ( $page->ID === $current_page_id ) ? 'page' : '';
    8587
    8688            /** This filter is documented in wp-includes/class-walker-page.php */
    87             $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
     89            $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page_id );
    8890
    8991            $attributes = '';
Note: See TracChangeset for help on using the changeset viewer.