Error Identifier: logicalAnd.resultUnused
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, bool $b): void
{
$result = $a and $b;
}
Why is it reported? #
The result of the and operator is unused. This is almost always a bug caused by the unexpected precedence of the and operator. The and operator has lower precedence than =, so $result = $a and $b is parsed as ($result = $a) and $b. The variable $result receives the value of $a, and the and $b part is evaluated but its result is discarded.
How to fix it #
Use && instead of and, which has higher precedence than =:
function doFoo(bool $a, bool $b): void
{
- $result = $a and $b;
+ $result = $a && $b;
}
Or use parentheses to disambiguate:
function doFoo(bool $a, bool $b): void
{
- $result = $a and $b;
+ $result = ($a and $b);
}
How to ignore this error #
You can use the identifier logicalAnd.resultUnused to ignore this error using a comment:
// @phpstan-ignore logicalAnd.resultUnused
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.resultUnused
Rules that report this error #
- PHPStan\Rules\DeadCode\NoopRule [1]