website/errors/nullsafe.neverNull.md
<?php declare(strict_types = 1);
class Foo
{
public string $name = '';
public function getName(): string
{
return $this->name;
}
}
function doFoo(Foo $foo): void
{
$foo?->getName();
$foo?->name;
}
The nullsafe operator (?->) is used on a value that can never be null. The ?-> operator is meant to short-circuit to null when the left side is null, but since the type is non-nullable, the null check is unnecessary. This suggests either the type is wrong, or -> should be used instead.
Replace the nullsafe operator with the regular object operator:
function doFoo(Foo $foo): void
{
- $foo?->getName();
- $foo?->name;
+ $foo->getName();
+ $foo->name;
}
If the value can indeed be null, fix the type declaration:
-function doFoo(Foo $foo): void
+function doFoo(?Foo $foo): void
{
$foo?->getName();
$foo?->name;
}