Error Identifier: empty.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
{
return 5;
}
if (empty(doFoo())) {
echo 'empty';
}
Why is it reported? #
The expression inside empty() has a type that makes the result of empty() always predictable. In the example above, doFoo() always returns int 5, which is truthy, so empty(doFoo()) is always false. This makes the check redundant.
Depending on the expression’s type, the message may say the expression “is always falsy”, “is not falsy”, “is always null”, or “is not nullable”.
How to fix it #
Remove the redundant empty() check:
<?php declare(strict_types = 1);
-if (empty(doFoo())) {
- echo 'empty';
-}
+$result = doFoo();
+if ($result === 0) {
+ echo 'empty';
+}
Or use a more explicit comparison that correctly reflects the intended logic.
How to ignore this error #
You can use the identifier empty.expr to ignore this error using a comment:
// @phpstan-ignore empty.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: empty.expr