website/errors/empty.notAllowed.md
<?php declare(strict_types = 1);
function isValid(string $name): bool
{
return !empty($name);
}
This rule is provided by the package phpstan/phpstan-strict-rules.
The empty() language construct is disallowed by strict rules because it combines two checks into one -- it checks if a variable is set and if its value is falsy. This makes its behaviour unpredictable with different types: empty('0') returns true, empty(0) returns true, and empty([]) returns true. These implicit coercions can hide bugs.
Using explicit comparisons makes the code's intent clearer and avoids surprising results from PHP's loose type juggling.
Replace empty() with an explicit comparison appropriate for the expected type:
<?php declare(strict_types = 1);
function isValid(string $name): bool
{
- return !empty($name);
+ return $name !== '';
}
For arrays, compare against an empty array or use count():
<?php declare(strict_types = 1);
/** @param list<string> $items */
function hasItems(array $items): bool
{
- return !empty($items);
+ return count($items) > 0;
}