Skip to content

Wrong type detection when variable is changed by reference #4

Open
@ossinkine

Description

@ossinkine

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.

Activity

ondrejmirtes

ondrejmirtes commented on Dec 12, 2017

@ondrejmirtes
Member

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

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

ossinkine commented on Dec 12, 2017

@ossinkine
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @ondrejmirtes@ossinkine

        Issue actions

          Wrong type detection when variable is changed by reference · Issue #4 · phpstan/phpstan-phpunit