Skip to content
This repository was archived by the owner on Aug 14, 2021. It is now read-only.

php7.4 - usort() needs int, not bool #99

@pauljherring

Description

@pauljherring

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 zero

Suggested 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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions