Error Identifier: logicalXor.rightAlwaysTrue
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(object $a): void
{
if ($a xor true) {
// ...
}
}
Why is it reported? #
The right side of the xor operator is always true. Since one operand is always true, the xor expression effectively becomes the negation of the other operand (!$a in this case). This is misleading and likely indicates a logic error.
The xor operator returns true when exactly one of its operands is true. If one operand is always true, the result is simply the inverse of the other operand.
How to fix it #
If negation is intended, use the ! operator for clarity:
- if ($a xor true) {
+ if (!$a) {
// ...
}
If the condition should actually test two dynamic values, fix the right operand:
- if ($a xor true) {
+ if ($a xor $b) {
// ...
}
How to ignore this error #
You can use the identifier logicalXor.rightAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore logicalXor.rightAlwaysTrue
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.rightAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\LogicalXorConstantConditionRule [1]