empty.expr

Menu

Error Identifier: empty.expr

← Back to empty.*

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);

/** @return positive-int */
function getCount(): int
{
	return 1;
}

if (empty(getCount())) {
	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, getCount() always returns a positive-int, which is always truthy, so empty(getCount()) 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 since the value can never be empty:

 <?php declare(strict_types = 1);
 
-if (empty(getCount())) {
-	echo 'empty';
-}
+echo getCount();

Or if the type is incorrect, fix the return type to allow falsy values:

 <?php declare(strict_types = 1);
 
-/** @return positive-int */
+/** @return non-negative-int */
 function getCount(): int
 {
 	return 1;
 }

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

Rules that report this error #

  • PHPStan\Rules\Variables\EmptyRule [1]
  • PHPStan\Rules\Variables\IssetRule [1]
  • PHPStan\Rules\Variables\NullCoalesceRule [1]