Menu

← Back to nullCoalesce.*

Error Identifier: nullCoalesce.initializedProperty

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 User
{
	private string $name;

	public function __construct()
	{
		$this->name = 'John';
	}

	public function doFoo(): void
	{
		echo $this->name ?? 'default'; // ERROR: Property User::$name on left side of ?? is not nullable and initialized.
	}
}

Why is it reported? #

The ?? (null coalescing) operator is designed to provide a fallback value when the left side is null or undefined. In this case, PHPStan has determined that the property being checked with ?? has a non-nullable native type and is guaranteed to be initialized at the point of access. Since the property can never be null and is always initialized, the right side of ?? will never be used, making the null coalescing operator redundant.

How to fix it #

Remove the null coalescing operator since the property is always initialized and non-nullable:

 <?php declare(strict_types = 1);
 
 class User
 {
 	private string $name;

 	public function __construct()
 	{
 		$this->name = 'John';
 	}

 	public function doFoo(): void
 	{
-		echo $this->name ?? 'default';
+		echo $this->name;
 	}
 }

Or if the property should legitimately be nullable, change its type:

 <?php declare(strict_types = 1);
 
 class User
 {
-	private string $name;
+	private ?string $name;

 	public function __construct()
 	{
 		$this->name = 'John';
 	}

 	public function doFoo(): void
 	{
 		echo $this->name ?? 'default';
 	}
 }

How to ignore this error #

You can use the identifier nullCoalesce.initializedProperty to ignore this error using a comment:

// @phpstan-ignore nullCoalesce.initializedProperty
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: nullCoalesce.initializedProperty

Rules that report this error #

  • PHPStan\Rules\Variables\EmptyRule [1]
  • PHPStan\Rules\Variables\IssetRule [1]
  • PHPStan\Rules\Variables\NullCoalesceRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.