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
sort()
, rsort()
and usort()
converts an array to list
#2891
Conversation
Please have a look at phpstan/phpstan#3312 It seems also fixed by your PR. If so, please add a test |
Yes, this PR will fix it too. I added a test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise perfect 👍
src/Analyser/NodeScopeResolver.php
Outdated
private function getArraySortFunctionType(Type $type): Type | ||
{ | ||
if (!$type->isArray()->yes()) { | ||
return new ErrorType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just return the same $type
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/Analyser/NodeScopeResolver.php
Outdated
|
||
$isIterableAtLeastOnce = $type->isIterableAtLeastOnce(); | ||
if ($isIterableAtLeastOnce->no()) { | ||
return new ConstantArrayType([], []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just return the same $type
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/Analyser/NodeScopeResolver.php
Outdated
$functionReflection !== null | ||
&& ( | ||
(in_array($functionReflection->getName(), ['sort', 'rsort'], true) && count($expr->getArgs()) >= 1) | ||
|| ($functionReflection->getName() === 'usort' && count($expr->getArgs()) >= 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to complicate this by checking that the number of args is >= 2
. Just check that the first arg is always there and join the condition into a single in_array
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@ondrejmirtes |
https://github.com/phpstan/phpstan-src/actions/runs/7682388532/job/20936712149 |
These errors were reported before, but they were in the baseline. So I think we can ignore the CI error. |
Yes we can :) |
Thank you! |
Closes phpstan/phpstan#6467
Closes phpstan/phpstan#10423
Closes phpstan/phpstan#3312