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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

False only boolean type gets lost through a method with a template #6653

Closed
mitelg opened this issue Feb 15, 2022 · 7 comments
Closed

False only boolean type gets lost through a method with a template #6653

mitelg opened this issue Feb 15, 2022 · 7 comments

Comments

@mitelg
Copy link

mitelg commented Feb 15, 2022

Bug report

Hello there 馃憢

We have a method that gets all kind of values, and in it an event is dispatched. The return value is the same as the given parameter. It works totally fine, but with one exception, if the passed value is type of array|false. The return type is then array|bool which is not correct. See the prepared example

Code snippet that reproduces the problem

https://phpstan.org/r/a057351b-dec7-449b-b8fb-86e921e2cac6

Expected output

No issue should appear

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

We are using PHPStan on a heavy legacy code base and with its help, the code gets more stable every day, thanks for that!

@ondrejmirtes
Copy link
Member

This is a hard problem. PHPStan chooses to generalize scalar values so when you pass 1, you get int etc. If you specify scalar bounds, the literal type is preserved: @template TValue of bool

@ondrejmirtes ondrejmirtes added this to the Generics milestone Feb 23, 2022
@ondrejmirtes
Copy link
Member

Leaving this open as it should also work if you use of array|bool, but it currently doesn't. https://phpstan.org/r/ec06b040-0a8f-4ff3-a3e9-07998024a0ab

@Seldaek
Copy link
Contributor

Seldaek commented Jul 20, 2023

Just another example as I found this issue when trying to report the same :)

https://phpstan.org/r/6dec0406-f803-4e29-8d21-fd5f631efb94

Found via composer/composer#10429 (comment) (for my future reference if this gets fixed..)

@ondrejmirtes
Copy link
Member

Last night I came up with the idea that we mostly shouldn't generalize the generic type variables, except when they're in object generics, like Foo<int>.

Here's the resulting PR: phpstan/phpstan-src#2818

We can't do this for objects, because I want new Collection([1, 2, 3]) to become Collection<int>. This could be improved in the future thanks to this suggestion: #6732 (comment) (but it's pretty complex to implement so for now it has to wait).

The new behaviour now only applies to bleeding edge (https://phpstan.org/blog/what-is-bleeding-edge) so definitely enable it to get the taste of the future 馃憤

@mitelg
Copy link
Author

mitelg commented Dec 13, 2023

tried it out today and can confirm this is working! 馃帀

thank you very much

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 Jan 14, 2024
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

4 participants