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 $bar = 0;
}
function test(): void
{
$foo = new Foo();
$foo->bar = 5;
if (isset($foo->bar)) {
echo $foo->bar;
}
}
Why is it reported? #
The isset() check on a property is unnecessary because PHPStan can determine the property always exists and is never null at the point of the check. The property $bar is declared with type int and has been assigned a value, so it is initialized and can never be null. Using isset() on it always returns true.
How to fix it #
Remove the unnecessary isset() check:
function test(): void
{
$foo = new Foo();
$foo->bar = 5;
- if (isset($foo->bar)) {
- echo $foo->bar;
- }
+ echo $foo->bar;
}
If the property can legitimately be nullable, declare it as such:
class Foo
{
- public int $bar = 0;
+ public ?int $bar = 0;
}
How to ignore this error #
You can use the identifier isset.property to ignore this error using a comment:
// @phpstan-ignore isset.property
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.property