Make WordPress Core

Opened 2 months ago

Closed 2 months ago

#64497 closed enhancement (fixed)

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

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

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.


2 months ago
#1

  • Keywords has-patch added

#2 @westonruter
2 months ago

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

#3 @westonruter
2 months ago

  • Resolution set to fixed
  • Status changed from reviewing to 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.