-
-
Notifications
You must be signed in to change notification settings - Fork 897
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
Using array_merge and array shapes #7857
Comments
@nicholasruunu After the latest push in 1.8.x, PHPStan now reports different result with your code snippet: @@ @@
-10: Method Paginator::toArray() should return array{page: int, perPage?: int} but returns non-empty-array<'page'|'perPage', int>.
+10: Method Paginator::toArray() should return array{page: int, perPage?: int} but returns array{}. Full report
|
@canvural Feel free to look at the extension, something about the logic is clearly wrong :) Thanks. |
I just reverted the changed, I found out the |
No, thanks 😄 It was just funny that now it inferred empty array. |
I'll take a look. I messed around with that extension in the past as well UPDATE: it's the constant array union it cannot deal with yet. if one arg is a non constant array it just generalizes it to a generic array right now UPDATE2: it is most likely not necessarily a precondition here, but I'm honestly thinking about creating a |
@herndlm More useful method is getConstantArrays() I think. |
what I currently don't fully understand with such methods as we have them in e.g. TypeUtils too is that I can't find out if all types of a union are e.g. constant array. wouldn't this lead to code that maybe misses things? |
maybe it's a use case for the TypeTraverser after all.. :) |
If we're interested only in unions that consist only of constant arrays, or we want to extract constant arrays from non-exclusive unions, I think those should be two different methods. I think the problem with TypeTraverser is that it's easy to misuse it and dive into nested arrays like |
yeah that was a bad example and good point. I have to think about this a bit more, array_merge works with many other seem-ably constant array unions, but turns out they are all normalized to a constant array. So in this case where the empty array is retained and we have |
The vision is that all The code that currently calls |
sorry, one more related to this issue: is there currently a way to force-collapse constant array unions? e.g. |
This is all about how we change/optimize reduceArrays(). |
Fixed phpstan/phpstan-src#3770 |
Bug report
There seems to be a small issue with specificity when using array_merge and array shapes.
It seems to force
non-empty-array<'page'|'perPage', int>
over the more specificarray{page: int, perPage?: int}
.It also passes when rewritten with if statements without array_merge.
Code snippet that reproduces the problem
phpstan snippet
Expected output
I'm expecting it to pass since the array shape is more specific in that page is always present but perPage not always.
Also passes in Psalm
Did PHPStan help you today? Did it make you happy in any way?
Great project, keep it up!
The text was updated successfully, but these errors were encountered: