Skip to content

Recognize covariance when returning generic union types #5844

@spawnia

Description

@spawnia

Bug report

See #5841

Code snippet that reproduces the problem

https://phpstan.org/r/f64c26bf-9543-4f57-9024-a415d78b2cd2

Expected output

PHPStan recognizes the narrowing of the type:

Function value() should return T of int|string but returns T of string.

However, it does not seem to recognize that T of string is covariant to T of int|string.

Separating the generics and doing an ad-hoc union seems to work: https://phpstan.org/r/673e1385-a14a-47b5-9102-a7d3d3e3ce96

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

PHPStan level 4 found a lot of redundant type checks in https://github.com/webonyx/graphql-php. A series of pull requests driven by the analysis lead to a 10-20% performance boost.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions