phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

Incorrect "comparison will always evaluate to false" for comparison between `static::class` and `<subclass>::class` in class with `@template`

Open lkrms opened this issue 1 year ago • 0 comments

Bug report

Works (see playground):

<?php

abstract class A
{
    public static function foo(): void
    {
        if (static::class === B::class) {
            printf("I'm a %s" . PHP_EOL, B::class);
        }
    }
}

class B extends A {}

Fails:

<?php

/**
 * @template T of object
 */
abstract class A
{
    public static function foo(): void
    {
        if (static::class === B::class) {
            printf("I'm a %s" . PHP_EOL, B::class);
        }
    }
}

/**
 * @extends A<C>
 */
class B extends A {}

class C {};

Code snippet that reproduces the problem

https://phpstan.org/r/79f71be1-69bd-40a6-8bf3-ed36806ba226

Expected output

PHPStan shouldn't report a comparison between static::class and the name of a subclass as "always false", whether templates are in play or not.

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

No response

lkrms avatar Jan 28 '24 01:01 lkrms