Make WordPress Core

Changeset 61474


Ignore:
Timestamp:
01/12/2026 09:15:00 PM (2 weeks ago)
Author:
westonruter
Message:

Code Modernization: Utilize spaceship operator <=> in sort comparison logic.

Some replaced instances also fix a bug where the comparison function should have returned 0 as opposed to 1 or -1 as used in ternaries. This results in a performance improvement.

Developed in https://github.com/WordPress/wordpress-develop/pull/10717

Props soean, mukesh27, westonruter.
Fixes #64497.

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-ms-themes-list-table.php

    r61444 r61474  
    303303        $b = $theme_b[ $orderby ];
    304304
    305         if ( $a === $b ) {
    306             return 0;
    307         }
    308 
    309         if ( 'DESC' === $order ) {
    310             return ( $a < $b ) ? 1 : -1;
    311         } else {
    312             return ( $a < $b ) ? -1 : 1;
    313         }
     305        return 'DESC' === $order ?
     306            $b <=> $a :
     307            $a <=> $b;
    314308    }
    315309
  • trunk/src/wp-admin/includes/class-wp-plugin-install-list-table.php

    r61456 r61474  
    459459        $b = $plugin_b->$orderby;
    460460
    461         if ( $a === $b ) {
    462             return 0;
    463         }
    464 
    465         if ( 'DESC' === $this->order ) {
    466             return ( $a < $b ) ? 1 : -1;
    467         } else {
    468             return ( $a < $b ) ? -1 : 1;
    469         }
     461        return 'DESC' === $this->order ?
     462            $b <=> $a :
     463            $a <=> $b;
    470464    }
    471465
  • trunk/src/wp-admin/includes/menu.php

    r57065 r61474  
    328328            return 1;
    329329        } elseif ( isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
    330             if ( $menu_order[ $a ] === $menu_order[ $b ] ) {
    331                 return 0;
    332             }
    333             return ( $menu_order[ $a ] < $menu_order[ $b ] ) ? -1 : 1;
     330            return $menu_order[ $a ] <=> $menu_order[ $b ];
    334331        } else {
    335             return ( $default_menu_order[ $a ] <= $default_menu_order[ $b ] ) ? -1 : 1;
     332            return $default_menu_order[ $a ] <=> $default_menu_order[ $b ];
    336333        }
    337334    }
  • trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php

    r61197 r61474  
    905905                $b_suffix = $b['suffix'] ?? '';
    906906                if ( $a_suffix !== $b_suffix ) {
    907                     return $a_suffix < $b_suffix ? -1 : 1;
     907                    return $a_suffix <=> $b_suffix;
    908908                }
    909909                $a_id = $a['unique_id'] ?? '';
    910910                $b_id = $b['unique_id'] ?? '';
    911                 if ( $a_id === $b_id ) {
    912                     return 0;
    913                 }
    914                 return $a_id > $b_id ? 1 : -1;
     911                return $a_id <=> $b_id;
    915912            }
    916913        );
  • trunk/src/wp-includes/script-loader.php

    r61473 r61474  
    30003000            $styles,
    30013001            static function ( $a, $b ) {
    3002                 return ( $a['size'] <= $b['size'] ) ? -1 : 1;
     3002                return $a['size'] <=> $b['size'];
    30033003            }
    30043004        );
Note: See TracChangeset for help on using the changeset viewer.