-
Notifications
You must be signed in to change notification settings - Fork 506
Fix type inference of array_sum()
#2591
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
Fix type inference of array_sum()
#2591
Conversation
80340fa
to
e8e6a0d
Compare
@ondrejmirtes Could you review this PR? |
$types = $constantArray->getValueTypes(); | ||
|
||
foreach ($types as $type) { | ||
$scalar = $type->getConstantScalarValues(); |
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.
Hi, I don't really love that this PR is working with specific scalar values and calls array_sum
internally. It misses out on various properties of the type system, like working with IntegerRangeType.
I'd much rather be if for ConstantArrayType objects, like [$one, $two, $three]
, it tried to do $scope->getType(new Plus($one, new Plus($two, $three)))
. Because Plus expr handling in MutatingScope already knows how to work with al possible Types, we can just call it here like that.
Because ConstantArrayType contains Type and not Expr, you can take advantage of TypeExpr virtual node to build an Expr out of Type instances.
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.
Yes, that's the idea :) And we don't have to limit ourselves to a single constant array, we can union results from all constant arrays. For example when something like array{1}|array{2, 3}
is passed in.
Thank you very much for your review! I will continue working it in tomorrow. |
This pull request has been marked as ready for review. |
@ondrejmirtes |
@zer0-star No, that's totally fine and actually a nice side-effect of the changes :) |
@ondrejmirtes |
10d0388
to
6524665
Compare
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.
Hi, I really appreciate this, especially the thorough test suite :) I wanted to write a simplification and it helped me a lot to get it right.
I also pushed a failing test case - ConstantArrayType might have an optional key. This is how to check for that:
foreach ($constantArray->getValueTypes() as $i => $type) {
if ($constantArray->isOptionalKey($i)) { ... }
}
Thanks so much for your help! I really appreciate it. Is there something else to revise? |
No, once the optional keys are supported, we are good to merge this :) |
I fixed it! I hope you like this change 🤗 |
63ec5dc
to
291bb57
Compare
I don't really like any There should be nothing special about I'll merge the PR if the build gets green :) Thank you very much for your cooperation! |
Thanks! I was happy to contribute to PHPStan. I learned a lot from your review! |
fix phpstan/phpstan#9808