Skip to content

Solve in_array() once and for all #6705

@ondrejmirtes

Description

@ondrejmirtes

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 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions