diff options
author | Bartosz DziewoĆski <matma.rex@gmail.com> | 2017-10-06 22:39:13 +0200 |
---|---|---|
committer | James D. Forrester <jforrester@wikimedia.org> | 2018-05-30 18:05:20 -0700 |
commit | b191e5e860f24e1dd05e3d3d782364e4ea75b176 (patch) | |
tree | 378e01dfd42501aa7adfa01e31f7e2561dc721d2 /includes/changes | |
parent | 4fd27f006f3a233f2bed8585b1325d318763abb0 (diff) | |
download | mediawikicore-b191e5e860f24e1dd05e3d3d782364e4ea75b176.tar.gz mediawikicore-b191e5e860f24e1dd05e3d3d782364e4ea75b176.zip |
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
Diffstat (limited to 'includes/changes')
-rw-r--r-- | includes/changes/ChangesListFilterGroup.php | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/includes/changes/ChangesListFilterGroup.php b/includes/changes/ChangesListFilterGroup.php index 3e2c464aafa4..79e8e33e8af4 100644 --- a/includes/changes/ChangesListFilterGroup.php +++ b/includes/changes/ChangesListFilterGroup.php @@ -358,7 +358,7 @@ abstract class ChangesListFilterGroup { } usort( $this->filters, function ( $a, $b ) { - return $b->getPriority() - $a->getPriority(); + return $b->getPriority() <=> $a->getPriority(); } ); foreach ( $this->filters as $filterName => $filter ) { |