Skip to content

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

Closed
@chris54721

Description

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!

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions