Error Identifier: logicalXor.rightAlwaysFalse
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 test(bool $a): bool
{
return $a xor ($a && !$a);
}
Why is it reported? #
The right side of the xor operator always evaluates to false. This makes the xor expression equivalent to just the left side, because $left xor false always equals $left. The right operand is redundant and likely indicates a logic error.
How to fix it #
Review the logic and either remove the xor expression entirely if the right side is not needed, or correct the right-side condition so that it can actually evaluate to true.
<?php declare(strict_types = 1);
function test(bool $a): bool
{
- return $a xor ($a && !$a);
+ return $a xor someOtherCondition();
}
How to ignore this error #
You can use the identifier logicalXor.rightAlwaysFalse to ignore this error using a comment:
// @phpstan-ignore logicalXor.rightAlwaysFalse
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: logicalXor.rightAlwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\LogicalXorConstantConditionRule [1]