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

Wrong type detection when variable is changed by reference

Open ossinkine opened this issue 8 years ago • 2 comments

Example

class Test extends \PHPUnit\Framework\TestCase
{
    public function testAssertVariable()
    {
        $foo = null;
        $value = new \stdClass();
        $mock = $this->getMockBuilder('Foo')->setMethods(['update'])->getMock();
        $mock->method('update')->willReturnCallback(function () use (&$foo, $value) {
            $foo = $value;
        });
        $this->assertSame($value, $foo);
    }
}

$foo variable equals $value after $mock->update() call but PHPStan throws error Call to assertSame() with different types stdClass and null will always result in test failure.

ossinkine avatar Dec 12 '17 10:12 ossinkine

Yes, understanding of the could could be improved on PHPStan's part. Meanwhile, you can annotate the variable:

/** @var \stdClass|null $foo */
$foo = null;

ondrejmirtes avatar Dec 12 '17 11:12 ondrejmirtes

Yes, I already use @var annotation as workaround, thanks

ossinkine avatar Dec 12 '17 14:12 ossinkine