Error Identifier: logicalAnd.rightNotBoolean
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(bool $a, string $s): void
{
$result = $a and $s;
}
Why is it reported? #
This error comes from phpstan-strict-rules. It reports that the right side of an and operator is not a boolean value. Strict boolean comparisons require both sides to be of type bool to avoid subtle bugs caused by PHP’s type juggling. In the example, $s is a string, which will be implicitly coerced to bool.
How to fix it #
Explicitly convert the value to a boolean:
function doFoo(bool $a, string $s): void
{
- $result = $a and $s;
+ $result = $a and ($s !== '');
}
Or use a boolean variable:
-function doFoo(bool $a, string $s): void
+function doFoo(bool $a, bool $b): void
{
- $result = $a and $s;
+ $result = $a and $b;
}
How to ignore this error #
You can use the identifier logicalAnd.rightNotBoolean to ignore this error using a comment:
// @phpstan-ignore logicalAnd.rightNotBoolean
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: logicalAnd.rightNotBoolean
Rules that report this error #
- PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule [1] phpstan/phpstan-strict-rules