Make WordPress Core


Ignore:
Timestamp:
09/08/2021 03:35:32 PM (3 years ago)
Author:
hellofromTonya
Message:

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

In the parent class, renames the parameter $object to $data_object.

Why? object is a PHP reserved keyword.

In each child class: renames the corresponding parameter to match the parent's method signature.

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, 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].

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

File:
1 edited

Legend:

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

    r50823 r51739  
    107107     * @since 3.0.0
    108108     * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
     109     * @since 5.9.0 Renamed `$item` to `$data_object` to match parent class for PHP 8 named parameter support.
    109110     *
    110111     * @see Walker::start_el()
    111112     *
    112      * @param string   $output Used to append additional content (passed by reference).
    113      * @param WP_Post  $item   Menu item data object.
    114      * @param int      $depth  Depth of menu item. Used for padding.
    115      * @param stdClass $args   An object of wp_nav_menu() arguments.
    116      * @param int      $id     Current item ID.
    117      */
    118     public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
     113     * @param string   $output      Used to append additional content (passed by reference).
     114     * @param WP_Post  $data_object Menu item data object.
     115     * @param int      $depth       Depth of menu item. Used for padding.
     116     * @param stdClass $args        An object of wp_nav_menu() arguments.
     117     * @param int      $id          Current item ID.
     118     */
     119    public function start_el( &$output, $data_object, $depth = 0, $args = null, $id = 0 ) {
     120        // Restores the more descriptive, specific name for use within this method.
     121        $menu_item = $data_object;
     122
    119123        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
    120124            $t = '';
     
    126130        $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
    127131
    128         $classes   = empty( $item->classes ) ? array() : (array) $item->classes;
    129         $classes[] = 'menu-item-' . $item->ID;
     132        $classes   = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes;
     133        $classes[] = 'menu-item-' . $menu_item->ID;
    130134
    131135        /**
     
    134138         * @since 4.4.0
    135139         *
    136          * @param stdClass $args  An object of wp_nav_menu() arguments.
    137          * @param WP_Post  $item Menu item data object.
    138          * @param int      $depth Depth of menu item. Used for padding.
    139          */
    140         $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
     140         * @param stdClass $args      An object of wp_nav_menu() arguments.
     141         * @param WP_Post  $menu_item Menu item data object.
     142         * @param int      $depth     Depth of menu item. Used for padding.
     143         */
     144        $args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth );
    141145
    142146        /**
     
    146150         * @since 4.1.0 The `$depth` parameter was added.
    147151         *
    148          * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
    149          * @param WP_Post  $item    The current menu item.
    150          * @param stdClass $args    An object of wp_nav_menu() arguments.
    151          * @param int      $depth   Depth of menu item. Used for padding.
    152          */
    153         $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
     152         * @param string[] $classes   Array of the CSS classes that are applied to the menu item's `<li>` element.
     153         * @param WP_Post  $menu_item The current menu item object.
     154         * @param stdClass $args      An object of wp_nav_menu() arguments.
     155         * @param int      $depth     Depth of menu item. Used for padding.
     156         */
     157        $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
    154158        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    155159
     
    160164         * @since 4.1.0 The `$depth` parameter was added.
    161165         *
    162          * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
    163          * @param WP_Post  $item    The current menu item.
    164          * @param stdClass $args    An object of wp_nav_menu() arguments.
    165          * @param int      $depth   Depth of menu item. Used for padding.
    166          */
    167         $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
     166         * @param string   $menu_id   The ID that is applied to the menu item's `<li>` element.
     167         * @param WP_Post  $menu_item The current menu item.
     168         * @param stdClass $args      An object of wp_nav_menu() arguments.
     169         * @param int      $depth     Depth of menu item. Used for padding.
     170         */
     171        $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
    168172        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    169173
     
    171175
    172176        $atts           = array();
    173         $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
    174         $atts['target'] = ! empty( $item->target ) ? $item->target : '';
    175         if ( '_blank' === $item->target && empty( $item->xfn ) ) {
     177        $atts['title']  = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
     178        $atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
     179        if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) {
    176180            $atts['rel'] = 'noopener';
    177181        } else {
    178             $atts['rel'] = $item->xfn;
    179         }
    180         $atts['href']         = ! empty( $item->url ) ? $item->url : '';
    181         $atts['aria-current'] = $item->current ? 'page' : '';
     182            $atts['rel'] = $menu_item->xfn;
     183        }
     184        $atts['href']         = ! empty( $menu_item->url ) ? $menu_item->url : '';
     185        $atts['aria-current'] = $menu_item->current ? 'page' : '';
    182186
    183187        /**
     
    196200         *     @type string $aria-current The aria-current attribute.
    197201         * }
    198          * @param WP_Post  $item  The current menu item.
    199          * @param stdClass $args  An object of wp_nav_menu() arguments.
    200          * @param int      $depth Depth of menu item. Used for padding.
    201          */
    202         $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
     202         * @param WP_Post  $menu_item The current menu item object.
     203         * @param stdClass $args      An object of wp_nav_menu() arguments.
     204         * @param int      $depth     Depth of menu item. Used for padding.
     205         */
     206        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
    203207
    204208        $attributes = '';
     
    211215
    212216        /** This filter is documented in wp-includes/post-template.php */
    213         $title = apply_filters( 'the_title', $item->title, $item->ID );
     217        $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
    214218
    215219        /**
     
    218222         * @since 4.4.0
    219223         *
    220          * @param string   $title The menu item's title.
    221          * @param WP_Post  $item  The current menu item.
    222          * @param stdClass $args  An object of wp_nav_menu() arguments.
    223          * @param int      $depth Depth of menu item. Used for padding.
    224          */
    225         $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
     224         * @param string   $title     The menu item's title.
     225         * @param WP_Post  $menu_item The current menu item object.
     226         * @param stdClass $args      An object of wp_nav_menu() arguments.
     227         * @param int      $depth     Depth of menu item. Used for padding.
     228         */
     229        $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth );
    226230
    227231        $item_output  = $args->before;
     
    241245         *
    242246         * @param string   $item_output The menu item's starting HTML output.
    243          * @param WP_Post  $item        Menu item data object.
     247         * @param WP_Post  $menu_item   Menu item data object.
    244248         * @param int      $depth       Depth of menu item. Used for padding.
    245249         * @param stdClass $args        An object of wp_nav_menu() arguments.
    246250         */
    247         $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
     251        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args );
    248252    }
    249253
Note: See TracChangeset for help on using the changeset viewer.