website/errors/empty.expr.md
<?php declare(strict_types = 1);
/** @return positive-int */
function getCount(): int
{
return 1;
}
if (empty(getCount())) {
echo 'empty';
}
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".
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;
}