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

InArrayFunctionTypeSpecifyingExtension suggestions for improvements #7275

Closed
gnutix opened this issue May 21, 2022 · 3 comments
Closed

InArrayFunctionTypeSpecifyingExtension suggestions for improvements #7275

gnutix opened this issue May 21, 2022 · 3 comments

Comments

@gnutix
Copy link

gnutix commented May 21, 2022

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

@gnutix gnutix changed the title InArrayFunctionTypeSpecifyingExtension : suggestions for improvements InArrayFunctionTypeSpecifyingExtension suggestions for improvements May 21, 2022
@rvanvelzen
Copy link
Contributor

Both suggestions now work as long as you pass true as the 3rd parameter.

@ondrejmirtes
Copy link
Member

Which is as it should be, because loose comparisons are really messy :) Thanks!

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants