phpstan-phpunit icon indicating copy to clipboard operation
phpstan-phpunit copied to clipboard

Unable to resolve the template type error when calling statement twice

Open m-vo opened this issue 3 years ago • 0 comments

I noticed a strange error "Unable to resolve the template type ExpectedType" when executing the same statement twice. Please see the following example code:

class Test extends \PHPUnit\Framework\TestCase
{
    public const A = 0;
    public const B = 1;

    public function testDemo(): void
    {
        $foo = 0;

        self::assertSame(self::A, $foo);
        self::assertSame(self::B, $foo);
        self::assertSame(self::B, $foo); // triggers error below
    }
}
Unable to resolve the template type ExpectedType in call to method static method PHPUnit\Framework\Assert::assertSame()      

I'm unsure where to start debugging this. It seems self::A appears as ConstantIntegerType while self::B as NeverType but I'm yet lacking the understanding of the internals to go further. :smile:

Minimal needed config to produce the error:

services:
    -
        class: PHPStan\Type\PHPUnit\Assert\AssertStaticMethodTypeSpecifyingExtension
        tags:
            - phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension

I've created a small reproducer in case you want to check it out https://github.com/m-vo/phpstan-debug-ttype (composer update && composer run phpstan).

(Please ignore the fact that this code example will also produce the message "Call to static method PHPUnit\Framework\Assert::assertSame() with 1 and 0 will always evaluate to false. " - which is of course correct. I didn't want to create a more complex example only to get rid of this.)

m-vo avatar Jul 11 '20 18:07 m-vo