Fixed Bad uksort callback in XF\Repository\Route

Affected version
2.2.1

PaulB

Active member
Around line 55:
PHP:
uksort($sub, function($a, $b)
{
	return strlen($a) < strlen($b);
});

The callback function should return an integer: -1, 0, or 1 for less than, equal to, or greater than, respectively. As far as I'm aware, returning a boolean has never been officially supported and will likely result in incorrect or indeterminate sorting.

PHP 8.0 explicitly disallows this:
Code:
ErrorException: [E_DEPRECATED] uksort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in src/XF/Repository/Route.php at line 60

Repro by installing XFES on PHP 8.0.0 RC2.

PHP 7.0 introduced the spaceship operator (<=>), which should be used here instead. Assuming you want descending order by string length:
PHP:
uksort($sub, function($a, $b)
{
	return strlen($b) <=> strlen($a);
});

Alternatively, for ascending order, swap the operands.
 

Xon

Well-known member
This may applies to at least one other sort like function call;

In XF\Repository\Emoji::getMatchingEmojiByString;
PHP:
        $sorter = function($a, $b) { return (strlen($a['shortname']) > strlen($b['shortname'])); };
        uasort($resultsP1, $sorter);
        uasort($resultsP2, $sorter);
        uasort($resultsP3, $sorter);
        uasort($resultsP4, $sorter);
 

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.2).

Change log:
Fix some sort callbacks on PHP 8+
There may be a delay before changes are rolled out to the XenForo Community.
 
Top