Skip to content

ReflectionClass:isSubclassOf is either always true or always false #12473

@fmarchalemisys

Description

@fmarchalemisys

Bug report

Following #12467, it is not possible to avoid an issue with ReflectionClass::isSubclassOf in the following code:

function getPictureFqn(string $pictureType): ?string
{
    $fqn = 'App\\Entity\\' . $pictureType;
    if ($fqn === Picture::class) {
        return Picture::class;
    }
    $refl = new \ReflectionClass($fqn);
    if (!$refl->isSubclassOf(Picture::class)) {
        return null;
    }

    return $fqn;
}

Phpstan says:

Call to method ReflectionClass<object>::isSubclassOf() with 'App\\Entity\\Picture' will always evaluate to false.

If I force the variable type:

    /** @var class-string<Picture> $fqn */
    $fqn = 'App\\Entity\\' . $pictureType;

The error is:

Call to method ReflectionClass<Picture>::isSubclassOf() with 'App\\Entity\\Picture' will always evaluate to true.

Notice the output says "will evaluate to true" instead of false.

When I type:

    /** @var class-string<Picture|object> $fqn */
    $fqn = 'App\\Entity\\' . $pictureType;

The error reverts to:

Call to method ReflectionClass<object>::isSubclassOf() with 'App\\Entity\\Picture' will always evaluate to false.

This issue is related to php 8.4 (see #12459).

Code snippet that reproduces the problem

https://phpstan.org/r/cd85b227-3d67-4f14-8eb5-ad501398686f

Expected output

The result is not always false when applied on ReflectionClass<object>. Some objects are descendants of the class and other aren't.

I expect no issue with that function.

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions