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
"Asserted type does not narrow down the type" when using assertions with generics #10573
Comments
As a second work-a-round. Personally I just prefer to be explicit with TAttribute, i.e. like this: https://phpstan.org/r/2842e234-4c51-4a80-b87b-e49ec8cad94a So being explicit in the supported attributes means that you get even better static analyses. @ondrejmirtes I guess this is due to phpstan/phpstan-src#2909? As then it will then read the same attribute on the inherited instance as if it was written by hand. So do you have any hint for a fix? As I would obviously like to resolve the bug I created with my fix. But not sure how the rule giving this "new" error could determine whether the assert tag is inherited or handwritten. |
Fixed: phpstan/phpstan-src@299df51 |
Hi, I have this issue with phpstan 1.10.58 and not with 1.10.57. Thanks a lot |
It's fixed on 1.10.x. It's going to be released in matter of days. |
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. |
Bug report
When using assertions such as
@phpstan-assert-if-true
with generics, if the extending class specifies the same type specified in the "upper bound" of the generic (in the example:@template TAttribute of string
and@extends Voter<string, Post>
), theassert.alreadyNarrowedType
error is returned.As a workaround, I am using
@extends Voter<non-empty-string, Post>
.The Voter class in the example is adapted from the
phpstan/phpstan-symfony
stub.Can reproduce only with PHPStan 1.10.58 (not <=1.10.57)
Code snippet that reproduces the problem
https://phpstan.org/r/d2eb8124-32b6-4d5f-8d75-536c7357a17f
Expected output
It should not report the error as not narrowing the type is a valid thing to do.
Did PHPStan help you today? Did it make you happy in any way?
It helps me every single day!
The text was updated successfully, but these errors were encountered: