phpstan-phpunit
phpstan-phpunit copied to clipboard
Wrong type detection when variable is changed by reference
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.
Yes, understanding of the could could be improved on PHPStan's part. Meanwhile, you can annotate the variable:
/** @var \stdClass|null $foo */
$foo = null;
Yes, I already use @var annotation as workaround, thanks