Skip to content

Wrong inference that get_parent_class() always return false #4302

@noemi-salaun

Description

@noemi-salaun

Bug report

When using get_parent_class($object) on something that may be a subclass, PHPStan assume that it can only return false.

Code snippet that reproduces the problem

class HelloWorld
{	
    public function theMethod(MyInterface $object) {
        if (get_parent_class($object)) {    // <-- ERROR: if condition is always false
            return 1;
        }
    }
}

interface MyInterface {}

https://phpstan.org/r/a374c54d-1422-4c1e-9a34-5005f6668c8c

Expected output

When using an interface or a superclass, PHPStan should not be sure about the return being false, it should keep the type string|false, because the real object may be a subclass of the one typehinted

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions