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 function getValue(): int
{
return 5;
}
}
function doFoo(Foo $foo): void
{
if (isset($foo->getValue())) {
echo $foo->getValue();
}
}
Why is it reported? #
The expression inside isset() is never null based on the types PHPStan has inferred, so the isset() check is unnecessary – it will always evaluate to true. In the example above, getValue() returns int, which cannot be null, so isset($foo->getValue()) serves no purpose.
How to fix it #
Remove the unnecessary isset() check if the expression is always non-null:
function doFoo(Foo $foo): void
{
- if (isset($foo->getValue())) {
- echo $foo->getValue();
- }
+ echo $foo->getValue();
}
If the return value should be nullable, update the return type:
- public function getValue(): int
+ public function getValue(): ?int
{
return 5;
}
How to ignore this error #
You can use the identifier isset.expr to ignore this error using a comment:
// @phpstan-ignore isset.expr
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.expr