-
Notifications
You must be signed in to change notification settings - Fork 550
Infer non-empty-array after array_key_first/last() #4536
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
Conversation
|
This pull request has been marked as ready for review. |
50d0626 to
885233d
Compare
bd15a28 to
d909a10
Compare
| } | ||
| } | ||
|
|
||
| // array_key_first($a) !== null |
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.
I thought you forgot null !== array_key_first($a) but seems like it works looking at the test.
I'm not familiar with this code to know why at first sight.
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 are in resolveNormalizedIdentical, which is invoked in both directions here:
phpstan-src/src/Analyser/TypeSpecifier.php
Lines 2258 to 2269 in eb4e247
| // Normalize to: fn() === expr | |
| if ($rightExpr instanceof FuncCall && !$leftExpr instanceof FuncCall) { | |
| $specifiedTypes = $this->resolveNormalizedIdentical(new Expr\BinaryOp\Identical( | |
| $rightExpr, | |
| $leftExpr, | |
| ), $scope, $context); | |
| } else { | |
| $specifiedTypes = $this->resolveNormalizedIdentical(new Expr\BinaryOp\Identical( | |
| $leftExpr, | |
| $rightExpr, | |
| ), $scope, $context); | |
| } |
| && $unwrappedLeftExpr->name instanceof Name | ||
| && in_array($unwrappedLeftExpr->name->toLowerString(), ['array_key_first', 'array_key_last'], true) | ||
| && isset($unwrappedLeftExpr->getArgs()[0]) | ||
| && $rightType->isNull()->yes() |
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.
You're getting escaped mutant here, but if I understand correctly, it should be supported by
if (array_key_first($array) !== $nullOrInt) {
assertType('list<string>', $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.
agree. I tested it locally and the test starts failling when I manually mutate the code.
closes phpstan/phpstan#13546