Skip to content

PHPStan incorrectly narrows type of object after an instanceof check #13713

@clayfreeman

Description

@clayfreeman

Bug report

When an instanceof check is used on a variable typed as object, PHPStan incorrectly infers its type as the concrete class (T) instead of the correct intersection type (object&T). This leads to a false-positive with a subsequent is_subclass_of() check since PHPStan believes that the same type is being supplied to its first and second parameters.

Consult the linked test snippet for more detail about the erroneous pattern.

Code snippet that reproduces the problem

https://phpstan.org/r/25c06554-9cd6-42ea-bd30-d86dc12d94b6

Expected output

Line 	Error
6 	
Dumped type: object
Non-ignorable
8 	
Dumped type: object&stdClass
Non-ignorable

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions