This repository was archived by the owner on Aug 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
php7.4 - usort() needs int, not bool #99
Copy link
Copy link
Open
Description
https://github.com/andreskrey/readability.php/blob/master/src/Readability.php#L198
// No luck after removing flags, just return the longest text we found during the different loops
usort($this->attempts, function ($a, $b) {
return $a['textLength'] < $b['textLength']; // L198
});php 7.4 is complaining:
usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zeroSuggested fix:
- return $a['textLength'] < $b['textLength'];
+ return $b['textLength'] - $a['textLength'];(NB: replacing < with -, and swapped operands)
Testing:
<?php
function f1(&$x){ // original
usort($x, function ($a, $b) {
return $a['textLength'] < $b['textLength'];
});
}
function f2(&$x){ // proposed change
usort($x, function ($a, $b) {
return $b['textLength'] - $a['textLength'];
});
}
$a = [
['textLength'=> 5, 'b'=>'1'],
['textLength'=> 7, 'b'=>'2'],
['textLength'=> 3, 'b'=>'3'],
['textLength'=> 1, 'b'=>'4'],
];
$b = $a;
f1($b);
print_r($b);
$b = $a;
f2($b);
print_r($b);Output:
$ php -e x.php
Array
(
[0] => Array
(
[textLength] => 7
[b] => 2
)
[1] => Array
(
[textLength] => 5
[b] => 1
)
[2] => Array
(
[textLength] => 3
[b] => 3
)
[3] => Array
(
[textLength] => 1
[b] => 4
)
)
Array
(
[0] => Array
(
[textLength] => 7
[b] => 2
)
[1] => Array
(
[textLength] => 5
[b] => 1
)
[2] => Array
(
[textLength] => 3
[b] => 3
)
[3] => Array
(
[textLength] => 1
[b] => 4
)
)Metadata
Metadata
Assignees
Labels
No labels