website/errors/logicalOr.leftNotBoolean.md
<?php declare(strict_types = 1);
$string = 'str';
$bool = true;
if ($string or $bool) {
// ...
}
The left side of the or expression is not a boolean value. PHP will implicitly cast the non-boolean value to bool before evaluating the expression. This implicit type coercion can lead to unexpected behaviour depending on PHP's type juggling rules.
This rule is part of phpstan-strict-rules and enforces that only boolean values are used with the or operator, making the code's intent explicit.
The or keyword is the low-precedence version of ||. This identifier specifically covers the or keyword; for ||, see booleanOr.leftNotBoolean.
In the example above, $string is of type string, not bool, so using it on the left side of or relies on PHP's loose type coercion.
Use an explicit comparison to produce a boolean value:
<?php declare(strict_types = 1);
$string = 'str';
$bool = true;
-if ($string or $bool) {
+if ($string !== '' or $bool) {
// ...
}
Or convert the value to boolean with a meaningful comparison:
<?php declare(strict_types = 1);
$string = 'str';
$bool = true;
-if ($string or $bool) {
+if (strlen($string) > 0 or $bool) {
// ...
}