Skip to content

InArrayFunctionTypeSpecifyingExtension suggestions for improvements #7275

@gnutix

Description

@gnutix

Feature request

Hello there. While adding some types and types' tests for a wrapper function around in_array() in our codebase, I noticed some behavior could be improved.

The most useful improvement I see is for an array array<T|null>, we could infer that after calling !in_array(null, $array), $array is array<T>. So that this code would not trigger an inspection (currently does) : https://phpstan.org/r/a8dc5151-6acb-4a9f-b569-c6e658273608. Could be applied to bool / false too I guess, maybe others (I haven't thought a lot about other cases).

Another one would be for an array<T>, we could infer that after calling in_array($anyValue, $array), $array is non-empty-array<T> (I guess this one is self-explanatory 😄 ).

What do you think ? :) Here's the recap' demo with assertType calls : https://phpstan.org/r/1981fe30-11e7-4b73-97a5-254cfc5dd9fa

(I don't currently have the time to work on an implementation, but maybe someone will.)

Did PHPStan help you today? Did it make you happy in any way?

(I have a bunch of small issues to create, sorry if I don't come up with something new for each of them 🙏 )

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions