Eliminating the possibility for a null reference and returning early in that case, PHPStan still warns about a possible null reference:
<?php declare(strict_types=1);
class Greeter {
public function sayHello() : string {
return 'hello';
}
public function isEqualTo(Greeter $otherGreeter) : bool {
return $this->sayHello() === $otherGreeter->sayHello();
}
}
function isGreeterDifferent(?Greeter $greeterA, ?Greeter $greeterB) : bool {
if ($greeterA === null && $greeterB !== null) {
return true;
}
if ($greeterA !== null && $greeterB === null) {
return true;
}
if ($greeterA === null && $greeterB === null) {
return false;
}
return $greeterA->isEqualTo($greeterB);
}
Bug report
Eliminating the possibility for a null reference and returning early in that case, PHPStan still warns about a possible null reference:
Cannot call method isEqualTo() on Greeter\|nullIntroducing another (redundant) null check for
$greeterAmakes PHPStan think that$greeterBcan benull:Parameter #1 $otherGreeter of method Greeter::isEqualTo() expects Greeter, Greeter|null given.The
xorin the code can also be refactored asORand the error still shows, so it's the conceptual xor that's causing problems, not the literal `xor:Code snippet that reproduces the problem