Skip to content

Generic type is not inferred at all when instanceof is used. #13204

@LastDragon-ru

Description

@LastDragon-ru

Bug report

Phpstan infer type as mixed, but it is wrong, because TChild is always object. Thus, the type should be object|null.

/**
 * @template TChild of object
 *
 * @extends ArrayAccess<int, ?TChild>
 */
interface ParentNode extends ArrayAccess {
    // empty
}

class HelloWorld
{
	public function sayHelloBug(object $node): void
	{
		if ($node instanceof ParentNode) {
		     assertType('object|null', $node[0]);
		}
	}
}

Code snippet that reproduces the problem

https://phpstan.org/r/b49ce8e4-dd48-440a-9584-f22ce8a987b9

Expected output

No error.

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

Yep

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions