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-admin/includes/class-walker-nav-menu-checklist.php

    r49936 r51739  
    6262     *
    6363     * @since 3.0.0
     64     * @since 5.9.0 Renamed `$item` to `$data_object` to match parent class for PHP 8 named parameter support.
    6465     *
    6566     * @global int        $_nav_menu_placeholder
    6667     * @global int|string $nav_menu_selected_id
    6768     *
    68      * @param string   $output Used to append additional content (passed by reference).
    69      * @param WP_Post  $item  Menu item data object.
    70      * @param int      $depth  Depth of menu item. Used for padding.
    71      * @param stdClass $args   Not used.
    72      * @param int      $id     Not used.
     69     * @param string   $output      Used to append additional content (passed by reference).
     70     * @param WP_Post  $data_object Menu item data object.
     71     * @param int      $depth       Depth of menu item. Used for padding.
     72     * @param stdClass $args        Not used.
     73     * @param int      $id          Not used.
    7374     */
    74     public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
     75    public function start_el( &$output, $data_object, $depth = 0, $args = null, $id = 0 ) {
    7576        global $_nav_menu_placeholder, $nav_menu_selected_id;
    7677
     78        // Restores the more descriptive, specific name for use within this method.
     79        $menu_item = $data_object;
     80
    7781        $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? (int) $_nav_menu_placeholder - 1 : -1;
    78         $possible_object_id    = isset( $item->post_type ) && 'nav_menu_item' === $item->post_type ? $item->object_id : $_nav_menu_placeholder;
    79         $possible_db_id        = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0;
     82        $possible_object_id    = isset( $menu_item->post_type ) && 'nav_menu_item' === $menu_item->post_type ? $menu_item->object_id : $_nav_menu_placeholder;
     83        $possible_db_id        = ( ! empty( $menu_item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $menu_item->ID : 0;
    8084
    8185        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
     
    8589        $output .= '<input type="checkbox"' . wp_nav_menu_disabled_check( $nav_menu_selected_id, false ) . ' class="menu-item-checkbox';
    8690
    87         if ( ! empty( $item->front_or_home ) ) {
     91        if ( ! empty( $menu_item->front_or_home ) ) {
    8892            $output .= ' add-to-top';
    8993        }
    9094
    91         $output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="' . esc_attr( $item->object_id ) . '" /> ';
     95        $output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="' . esc_attr( $menu_item->object_id ) . '" /> ';
    9296
    93         if ( ! empty( $item->label ) ) {
    94             $title = $item->label;
    95         } elseif ( isset( $item->post_type ) ) {
     97        if ( ! empty( $menu_item->label ) ) {
     98            $title = $menu_item->label;
     99        } elseif ( isset( $menu_item->post_type ) ) {
    96100            /** This filter is documented in wp-includes/post-template.php */
    97             $title = apply_filters( 'the_title', $item->post_title, $item->ID );
     101            $title = apply_filters( 'the_title', $menu_item->post_title, $menu_item->ID );
    98102        }
    99103
    100         $output .= isset( $title ) ? esc_html( $title ) : esc_html( $item->title );
     104        $output .= isset( $title ) ? esc_html( $title ) : esc_html( $menu_item->title );
    101105
    102         if ( empty( $item->label ) && isset( $item->post_type ) && 'page' === $item->post_type ) {
     106        if ( empty( $menu_item->label ) && isset( $menu_item->post_type ) && 'page' === $menu_item->post_type ) {
    103107            // Append post states.
    104             $output .= _post_states( $item, false );
     108            $output .= _post_states( $menu_item, false );
    105109        }
    106110
     
    109113        // Menu item hidden fields.
    110114        $output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />';
    111         $output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="' . esc_attr( $item->object ) . '" />';
    112         $output .= '<input type="hidden" class="menu-item-parent-id" name="menu-item[' . $possible_object_id . '][menu-item-parent-id]" value="' . esc_attr( $item->menu_item_parent ) . '" />';
    113         $output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="' . esc_attr( $item->type ) . '" />';
    114         $output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="' . esc_attr( $item->title ) . '" />';
    115         $output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="' . esc_attr( $item->url ) . '" />';
    116         $output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="' . esc_attr( $item->target ) . '" />';
    117         $output .= '<input type="hidden" class="menu-item-attr-title" name="menu-item[' . $possible_object_id . '][menu-item-attr-title]" value="' . esc_attr( $item->attr_title ) . '" />';
    118         $output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="' . esc_attr( implode( ' ', $item->classes ) ) . '" />';
    119         $output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="' . esc_attr( $item->xfn ) . '" />';
     115        $output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="' . esc_attr( $menu_item->object ) . '" />';
     116        $output .= '<input type="hidden" class="menu-item-parent-id" name="menu-item[' . $possible_object_id . '][menu-item-parent-id]" value="' . esc_attr( $menu_item->menu_item_parent ) . '" />';
     117        $output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="' . esc_attr( $menu_item->type ) . '" />';
     118        $output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="' . esc_attr( $menu_item->title ) . '" />';
     119        $output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="' . esc_attr( $menu_item->url ) . '" />';
     120        $output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="' . esc_attr( $menu_item->target ) . '" />';
     121        $output .= '<input type="hidden" class="menu-item-attr-title" name="menu-item[' . $possible_object_id . '][menu-item-attr-title]" value="' . esc_attr( $menu_item->attr_title ) . '" />';
     122        $output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="' . esc_attr( implode( ' ', $menu_item->classes ) ) . '" />';
     123        $output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="' . esc_attr( $menu_item->xfn ) . '" />';
    120124    }
    121125
Note: See TracChangeset for help on using the changeset viewer.