psalm icon indicating copy to clipboard operation
psalm copied to clipboard

If a null-safe method call does not return null, it implies that the object is definitely not null

Open bwoebi opened this issue 1 year ago • 1 comments

Trivially if ($foo?->check() === false) { /* $foo is not null! */ }.

In fact psalm seems to this $foo is null, any not just possibly null here.

https://psalm.dev/r/ccf3505ede

bwoebi avatar Jan 15 '24 00:01 bwoebi

I found these snippets:

https://psalm.dev/r/ccf3505ede
<?php

class Foo {
    public function check(): bool {
        return false;
    }
    public function do(): void {}
}

function getAFoo(bool $null = false): ?Foo {
	return $null ? null : new Foo;
}

$foo = getAFoo();

if ($foo?->check() === false) {
    /** @psalm-trace $foo */
    ;
    $foo->do();
}
Psalm output (using commit a75d26a):

INFO: Trace - 18:5 - $foo: null

ERROR: NullReference - 19:11 - Cannot call method do on null value

psalm-github-bot[bot] avatar Jan 15 '24 00:01 psalm-github-bot[bot]