Skip to content

False positive when class method overrides abstract trait method and both have identical PHPDoc types narrower than the native types #10240

@ZebulanStanphill

Description

@ZebulanStanphill

Bug report

A new regression has been introduced in PHPStan 1.10.47: a false positive regarding contravariance is reported when a class method with PHPDoc types more specific than the native types overrides an abstract trait method with an identical native + PHPDoc signature.

The issue also occurs if the class method lacks PHPDocs of its own, but implements an interface method that has identical types to the trait method.

Example false positive from the linked snippet:

Parameter #1 $truthyStrParam (non-falsy-string) of method MyClass::doStuff() should be contravariant with parameter $truthyStrParam (string) of method MyTrait::doStuff()

It seems like the issue is that the increased specificity of the PHPDoc types used in the PHPDocs of the abstract trait method is lost.

Notably, this issue only seems to happen when the trait method is abstract rather than concrete.

Happens on level 3 or higher when strict rules and bleeding edge are enabled.

I'm guessing this was an unintentional side-effect of phpstan/phpstan-src@4c4f22f.

Code snippet that reproduces the problem

https://phpstan.org/r/c2bd3be8-840e-4b75-9617-4d46d3ef77e3

Expected output

No errors.

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

Seeing all the recent type-narrowing improvements in the recent changelogs has definitely made me happy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions