Skip to content

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

Closed
phpstan/phpstan-src
#4472

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

Activity

  1. VincentLanglet commented on Oct 23, 2025

    @VincentLanglet
    Contributor

    stdClass doesn't means exactly a stdClass, it means stdClass and all the child classes.
    So it's correct to simplify stdClass&object to stdClass in your example.

    What is not correct is the result of is_subclass_of, which is shown by
    https://phpstan.org/r/0cf1fbc5-1934-4dd8-b30c-dcba30a8cb48

    The issue seems related to https://github.com/phpstan/phpstan-src/blob/dd2717b2046823a18d431b462acc0cdee2fe5ddb/src/Type/Php/IsAFunctionTypeSpecifyingHelper.php#L46-L48

    There is currently no differences between

    \is_subclass_of(new stdClass(), stdClass::class, false)
    

    and

    \is_subclass_of($stdClassOrChildren, stdClass::class, false)
    
  2. VincentLanglet commented on Oct 23, 2025

    @VincentLanglet
    Contributor

    I'll try fixing it with phpstan/phpstan-src#4472 (WIP)

  3. added this to the Easy fixes milestone on Feb 11, 2026
  4. phpstan-bot commented on Feb 26, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#4472

  5. github-actions commented on Mar 30, 2026

    @github-actions

    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.

  6. locked as resolved and limited conversation to collaborators on Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions