Error Identifier: isset.expr
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);
function doFoo(int $value): void
{
if (isset($value)) {
// ...
}
}
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. The variable $value is typed as int, which cannot be null, so isset($value) serves no purpose.
The same applies to empty() checks and ?? (null coalescing) expressions when the left side is never null.
How to fix it #
Remove the unnecessary isset() check if the value is always defined and non-null:
- if (isset($value)) {
+ if (true) { // or simply remove the condition
// ...
}
If the value should be nullable, update the type declaration:
-function doFoo(int $value): void
+function doFoo(?int $value): void
{
if (isset($value)) {
// ...
}
}
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