Error Identifier: doWhile.condNotBoolean
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.
This error is reported by phpstan/phpstan-strict-rules.
Code example #
<?php declare(strict_types = 1);
$count = 5;
do {
$count--;
} while ($count);
Why is it reported? #
The phpstan-strict-rules package enforces that conditions in do-while loops must be strictly boolean. Using a non-boolean value such as an integer or string relies on PHP’s implicit truthiness rules, which can lead to subtle bugs.
How to fix it #
Use an explicit boolean comparison in the condition:
<?php declare(strict_types = 1);
$count = 5;
do {
$count--;
-} while ($count);
+} while ($count > 0);
How to ignore this error #
You can use the identifier doWhile.condNotBoolean to ignore this error using a comment:
// @phpstan-ignore doWhile.condNotBoolean
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: doWhile.condNotBoolean
Rules that report this error #
- PHPStan\Rules\BooleansInConditions\BooleanInDoWhileConditionRule [1] phpstan/phpstan-strict-rules