Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP8 usort with version_compare as callback #6492

Open
pschombara opened this issue Jan 31, 2022 · 6 comments
Open

PHP8 usort with version_compare as callback #6492

pschombara opened this issue Jan 31, 2022 · 6 comments

Comments

@pschombara
Copy link

Bug report

Using \usort with an PHP core function(version_compare) reports an error in PHP8.
Parameter #2 $callback of function usort expects callable(string, string): int, 'version_compare' given.

Code snippet that reproduces the problem

https://phpstan.org/r/e27b69c8-d3df-4347-b251-de8aa66a89a2

Expected output

No error

@ondrejmirtes
Copy link
Member

version_compare returns int|bool: https://www.php.net/manual/en/function.version-compare.php

@pschombara
Copy link
Author

Okay it returns bool if the third parameter operator is defined, which is not the case using the function the way mentioned.

usort($versions, static fn (string $a, string $b): int => version_compare($a, $b));
does not complain any error.

https://phpstan.org/r/501d12f7-a2b8-4418-bfac-68a595f22f06

@ondrejmirtes
Copy link
Member

Until this is supported you can use the arrow function version, thanks.

@staabm
Copy link
Contributor

staabm commented Jan 31, 2022

@herndlm this is another issue which would benefit from a AST rewrite into a arrow function like phpstan/phpstan-src#938

wdyt? :)

@ondrejmirtes
Copy link
Member

That’s not needed, it’s just that dynamic return type extension needs to be invoked for cases like this.

@Sweetchuck
Copy link

Not ideal, but works with PHP 7.4 and 8.1 at the same time:

/** @var callable(string $a, string $b): int $comparer */
$comparer = 'version_compare';
usort($versions, $comparer);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants