Skip to content
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

Open
ondrejmirtes opened this issue Feb 27, 2022 · 2 comments
Open

Solve in_array() once and for all #6705

ondrejmirtes opened this issue Feb 27, 2022 · 2 comments
Labels

Comments

@ondrejmirtes
Copy link
Member

@ondrejmirtes ondrejmirtes commented Feb 27, 2022

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 #5355 when in_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

@ondrejmirtes
Copy link
Member Author

@ondrejmirtes ondrejmirtes commented Mar 2, 2022

I did something: phpstan/phpstan-src@4321374

But it's still a mess worthy of some deep research and cleanup.

@phpstan-bot
Copy link

@phpstan-bot phpstan-bot commented Mar 2, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants