Error Identifier: while.alwaysFalse
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(): void
{
while (false) {
echo 'unreachable';
}
}
Why is it reported? #
The condition of the while loop always evaluates to false, which means the loop body will never execute. This is dead code and usually indicates a logic error, such as a condition that was incorrectly written or a variable whose type has been narrowed to a state that makes the condition impossible.
How to fix it #
Fix the condition so that the loop can actually execute:
<?php declare(strict_types = 1);
-function doFoo(): void
+function doFoo(bool $condition): void
{
- while (false) {
+ while ($condition) {
echo 'reachable';
+ $condition = false;
}
}
Or remove the dead code if the loop is no longer needed:
<?php declare(strict_types = 1);
function doFoo(): void
{
- while (false) {
- echo 'unreachable';
- }
}
How to ignore this error #
You can use the identifier while.alwaysFalse to ignore this error using a comment:
// @phpstan-ignore while.alwaysFalse
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: while.alwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\WhileLoopAlwaysFalseConditionRule [1]