Error Identifier: isset.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 Foo
{
public int $value = 0;
public function doFoo(): void
{
if (isset($this->value)) {
echo $this->value;
}
}
}
Why is it reported? #
The isset() check (or ?? null-coalescing operator, or empty()) is used on a property that has a native type and is known to be initialized. A typed property that is initialized can never be null (unless null is part of its declared type) and can never be in an uninitialized state at the point of the check. The isset() call is therefore redundant – it always evaluates to true.
How to fix it #
Remove the unnecessary isset() check:
public function doFoo(): void
{
- if (isset($this->value)) {
- echo $this->value;
- }
+ echo $this->value;
}
If the property might legitimately be uninitialized in some code paths, declare it without a default value and consider making it nullable:
-public int $value = 0;
+public ?int $value = null;
How to ignore this error #
You can use the identifier isset.initializedProperty to ignore this error using a comment:
// @phpstan-ignore isset.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: isset.initializedProperty