Use PHP 7 '<=>' operator in 'sort()' callbacks
authorBartosz Dziewoński <matma.rex@gmail.com>
Fri, 6 Oct 2017 20:39:13 +0000 (22:39 +0200)
committerJames D. Forrester <jforrester@wikimedia.org>
Thu, 31 May 2018 01:05:20 +0000 (18:05 -0700)
commitb191e5e860f24e1dd05e3d3d782364e4ea75b176
tree378e01dfd42501aa7adfa01e31f7e2561dc721d2
parent4fd27f006f3a233f2bed8585b1325d318763abb0
Use PHP 7 '<=>' operator in 'sort()' callbacks

`$a <=> $b` returns `-1` if `$a` is lesser, `1` if `$b` is lesser,
and `0` if they are equal, which are exactly the values 'sort()'
callbacks are supposed to return.

It also enables the neat idiom `$a[x] <=> $b[x] ?: $a[y] <=> $b[y]`
to sort arrays of objects first by 'x', and by 'y' if they are equal.

* Replace a common pattern like `return $a < $b ? -1 : 1` with the
  new operator (and similar patterns with the variables, the numbers
  or the comparison inverted). Some of the uses were previously not
  correctly handling the variables being equal; this is now
  automatically fixed.
* Also replace `return $a - $b`, which is equivalent to `return
  $a <=> $b` if both variables are integers but less intuitive.
* (Do not replace `return strcmp( $a, $b )`. It is also equivalent
  when both variables are strings, but if any of the variables is not,
  'strcmp()' converts it to a string before comparison, which could
  give different results than '<=>', so changing this would require
  careful review and isn't worth it.)
* Also replace `return $a > $b`, which presumably sort of works most
  of the time (returns `1` if `$b` is lesser, and `0` if they are
  equal or `$a` is lesser) but is erroneous.

Change-Id: I19a3d2fc8fcdb208c10330bd7a42c4e05d7f5cf3
24 files changed:
includes/GlobalFunctions.php
includes/MagicWord.php
includes/OutputPage.php
includes/Title.php
includes/api/ApiQueryUserContribs.php
includes/auth/AuthManager.php
includes/changes/ChangesListFilterGroup.php
includes/collation/IcuCollation.php
includes/libs/Timing.php
includes/libs/XhprofData.php
includes/libs/virtualrest/VirtualRESTServiceClient.php
includes/page/ImagePage.php
includes/parser/Parser.php
includes/profiler/ProfilerSectionOnly.php
includes/profiler/ProfilerXhprof.php
includes/profiler/output/ProfilerOutputText.php
includes/session/SessionInfo.php
includes/specialpage/AuthManagerSpecialPage.php
includes/specialpage/ChangesListSpecialPage.php
includes/specials/SpecialVersion.php
maintenance/findDeprecated.php
maintenance/namespaceDupes.php
profileinfo.php
tests/phpunit/MediaWikiTestCase.php