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
Solve in_array() once and for all #6705
Comments
I did something: phpstan/phpstan-src@4321374 But it's still a mess worthy of some deep research and cleanup. |
@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-15: Call to function in_array() with arguments 0, array<int, 0|1> and true will always evaluate to true.
+No errors |
There's no way to describe that e.g. |
@herndlm HasOffsetType could be enriched to contain the value type as well. After that we could stop relying on the fact that the |
I had a similar idea that I was playing around with yesterday. If that works, it might solve many of the in_array problems. I'll keep looking a bit in that direction then, thx |
I'm getting closer. I found a thing that confuses me though: given the type |
I seem to be down to one weird extra conditional needed only. and it is a bit related to the previous comment I guess. maybe I'll open a PR later to get some feedback from you |
OMG, I just realized what we need to solve in_array once and for all and without any hacks. We have HasOffsetType (we know the key) and HasOffsetValueType (we know the key and value combination). For in_array() and maybe other uses we need HasConstantValueType accessory type that tells us about value existence in an unknown key. Paired with |
Yeah exactly, that would fix most of the problems, if not all, here. That's why I was so looking forward to HasOffsetValueType. Until I realised it needs a constant key :) or a key at all :D |
I was thinking about this one again, regarding your idea about a |
It breaks apart if you do |
ok, an initial implementation looks promising, I'll try to bring it in a reviewable state soon. maybe before that - looks like I have questions already now :)
|
Yes. Same problem we have with HasOffsetValueType. Remedied here: https://github.com/phpstan/phpstan-src/blob/506a4d9df43ca815b75e95a5807b44d983a1fdab/src/Type/TypeCombinator.php#L898-L902
That's already taken care of somehow by this piece of code: https://github.com/phpstan/phpstan-src/blob/506a4d9df43ca815b75e95a5807b44d983a1fdab/src/Type/TypeCombinator.php#L532-L560 |
Bug report
/cc @herndlm this is something up your alley, could you please spare a few days to occupy your mind with this to see how it could be solved? :)
in_array
is a very problematic function - the type-specifying extension (https://github.com/phpstan/phpstan-src/blob/master/src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php) currently serves to narrow down the type of the first argument.The problems come when the error "Call to function in_array() will always evaluate to true/false" is reported for cases where it's not correct.
Here are some open issues about it: https://github.com/phpstan/phpstan/issues?q=is%3Aissue+is%3Aopen+in_array
Please note that this should be solved entirely with conditions in the type-specifying extension and the code related to
in_array
should be removed from https://github.com/phpstan/phpstan-src/blob/bedcc2db8616d195f4cf698d3731cc69d634c2b7/src/Rules/Comparison/ImpossibleCheckTypeHelper.php#L81-L130 entirely. This is so that issues like this phpstan/phpstan-webmozart-assert#142 whenin_array
is handled indirectly are also solved.Thank you.
Code snippet that reproduces the problem
https://phpstan.org/r/179a9da2-e9e7-4d4b-b4af-b0e01c35a9db and many others
The text was updated successfully, but these errors were encountered: