Make WordPress Core

Opened 9 months ago

Closed 9 months ago

#64497 closed enhancement (fixed)

Code Modernization: Replace manual comparison logic with the spaceship operator <=>

Reported by: Soean Owned by: westonruter
Priority: normal Milestone: 7.0
Component: General Version:
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

Since WordPress has bumped its minimum supported PHP version to 7.4, we should take advantage of modern PHP features to clean up legacy comparison logic. This ticket proposes replacing manual "less than / greater than" ternary or if/else blocks with the ​spaceship operator <=>.

The spaceship operator was introduced in PHP 7.0. It performs three-way comparisons and returns -1, 0, or 1 accordingly. In many parts of the WordPress Core, we still use verbose logic that can be simplified.

<?php
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
<?php
return $a <=> $b;

Change History (3)

This ticket was mentioned in ​PR #10717 on ​WordPress/wordpress-develop by ​@Soean.


9 months ago
#1

  • Keywords has-patch added

#2 @westonruter
9 months ago

  • Milestone Awaiting Review → 7.0
  • Owner set to westonruter
  • Status new → reviewing

#3 @westonruter
9 months ago

  • Resolution → fixed
  • Status reviewing → closed

In 61474:

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.

Note: See TracTickets for help on using tickets.