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

"Asserted type does not narrow down the type" when using assertions with generics #10573

Closed
chris54721 opened this issue Feb 13, 2024 · 6 comments
Labels
Milestone

Comments

@chris54721
Copy link

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>), the assert.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!

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 13, 2024
@RobertMe
Copy link

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
Not solely because of the error it now gives (calling with aaa while it only accepts POST_READ). But if you have a voter which supports multiple attributes (which you can union, like 'POST_READ'|'POST_WRITE') then PHPStan will also automatically error when you have a match or switch which doesn't handle all cases, like this: https://phpstan.org/r/ea859beb-bfc8-4b3f-8f28-402732816720 / https://phpstan.org/r/884e8030-fd93-4a79-9f57-2b4c8e30ddda

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.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@299df51

@floviolleau
Copy link

floviolleau commented Feb 19, 2024

Hi,

I have this issue with phpstan 1.10.58 and not with 1.10.57.
I saw that you fixed it on 1.11 branch. Will it be fixed on 1.10 branch?

Thanks a lot

@ondrejmirtes
Copy link
Member

It's fixed on 1.10.x. It's going to be released in matter of days.

@ondrejmirtes
Copy link
Member

Released: https://github.com/phpstan/phpstan/releases/tag/1.10.59

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 Mar 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants