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

Fix array_filter with callback that always evaluates to false #939

Merged
merged 1 commit into from Jan 24, 2022
Merged

Fix array_filter with callback that always evaluates to false #939

merged 1 commit into from Jan 24, 2022

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Jan 23, 2022

Fixes the return type narrowing if the callback evaluates to false. E.g. an int[] array is used together with a is_string callback. In that case the filtered type in ArrayFilterFunctionReturnTypeReturnTypeExtension would be array<*NEVER*> which does not make sense.
We found this in https://github.com/phpstan/phpstan-src/pull/938/files#r790184831.

@ondrejmirtes
Copy link
Member

Nope, it should be an empty ConstantArrayType 😊

@herndlm
Copy link
Contributor Author

herndlm commented Jan 23, 2022

ah forgot that the tests still need to run with older PHP versions too..

@herndlm herndlm marked this pull request as draft January 23, 2022 13:06
@herndlm
Copy link
Contributor Author

herndlm commented Jan 23, 2022

Nope, it should be an empty ConstantArrayType blush

thought about that too hmm, I was afraid it might mess things up if e.g. later on elements are added again. Update: tested and seems to be behaving just fine.

Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it

@herndlm
Copy link
Contributor Author

herndlm commented Jan 23, 2022

The remaining failing checks (5x phpstan-doctrine OOM, 1x composer) are the same I saw elsewhere too IIRC, should be fine.

@herndlm herndlm marked this pull request as ready for review January 23, 2022 14:32
@herndlm
Copy link
Contributor Author

herndlm commented Jan 23, 2022

I added more tests for already existing, unchanged behaviour. Hope that's fine. My plan is to refactor and add features in 1 or 2 follow-up PRs and already existing tests would be very useful then :)

@ondrejmirtes ondrejmirtes merged commit d8dc6d0 into phpstan:master Jan 24, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@herndlm herndlm deleted the fix-array-filter-return-type-narrowing-with-callback-that-always-evaluates-to-false branch January 24, 2022 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants