psalm
psalm copied to clipboard
If a null-safe method call does not return null, it implies that the object is definitely not null
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
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