Skip to content

Commit 8099762

Browse files
committed
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 WordPress#10717 Props soean, mukesh27, westonruter. Fixes #64497. git-svn-id: https://develop.svn.wordpress.org/trunk@61474 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fd2693d commit 8099762

5 files changed

Lines changed: 11 additions & 29 deletions

File tree

src/wp-admin/includes/class-wp-ms-themes-list-table.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,9 @@ public function _order_callback( $theme_a, $theme_b ) {
302302
$a = $theme_a[ $orderby ];
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

316310
/**

src/wp-admin/includes/class-wp-plugin-install-list-table.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,9 @@ private function order_callback( $plugin_a, $plugin_b ) {
458458
$a = $plugin_a->$orderby;
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

472466
/**

src/wp-admin/includes/menu.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,9 @@ function sort_menu( $a, $b ) {
327327
} elseif ( ! isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
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
}
338335

src/wp-includes/interactivity-api/class-wp-interactivity-api.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -904,14 +904,11 @@ function ( $a, $b ) {
904904
$a_suffix = $a['suffix'] ?? '';
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
);
917914
return $entries;

src/wp-includes/script-loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2999,7 +2999,7 @@ function wp_maybe_inline_styles() {
29992999
usort(
30003000
$styles,
30013001
static function ( $a, $b ) {
3002-
return ( $a['size'] <= $b['size'] ) ? -1 : 1;
3002+
return $a['size'] <=> $b['size'];
30033003
}
30043004
);
30053005

0 commit comments

Comments
 (0)