Menu

← Back to nullsafe.*

Error Identifier: nullsafe.neverNull

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?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;
}

Why is it reported? #

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.

How to fix it #

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;
 }

How to ignore this error #

You can use the identifier nullsafe.neverNull to ignore this error using a comment:

// @phpstan-ignore nullsafe.neverNull
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: nullsafe.neverNull

Rules that report this error #

  • PHPStan\Rules\Methods\NullsafeMethodCallRule [1]
  • PHPStan\Rules\Properties\NullsafePropertyFetchRule [1]
  • PHPStan\Rules\Variables\EmptyRule [1] [2]
  • PHPStan\Rules\Variables\IssetRule [1] [2]
  • PHPStan\Rules\Variables\NullCoalesceRule [1] [2]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.