From 3b011f6524254dad0f16840fdcfdbe7421548617 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 7 Dec 2023 23:21:25 +0100 Subject: [PATCH] LogicalXor is not shortcircuited --- src/Rules/Comparison/LogicalXorConstantConditionRule.php | 2 +- .../Comparison/LogicalXorConstantConditionRuleTest.php | 8 ++++++++ tests/PHPStan/Rules/Comparison/data/logical-xor.php | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Rules/Comparison/LogicalXorConstantConditionRule.php b/src/Rules/Comparison/LogicalXorConstantConditionRule.php index fec511438a..23116934e2 100644 --- a/src/Rules/Comparison/LogicalXorConstantConditionRule.php +++ b/src/Rules/Comparison/LogicalXorConstantConditionRule.php @@ -63,7 +63,7 @@ public function processNode(Node $node, Scope $scope): array } $rightType = $this->helper->getBooleanType($scope, $node->right); - if ($rightType instanceof ConstantBooleanType && !$scope->isInFirstLevelStatement()) { + if ($rightType instanceof ConstantBooleanType) { $addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $tipText): RuleErrorBuilder { if (!$this->treatPhpDocTypesAsCertain) { return $ruleErrorBuilder; diff --git a/tests/PHPStan/Rules/Comparison/LogicalXorConstantConditionRuleTest.php b/tests/PHPStan/Rules/Comparison/LogicalXorConstantConditionRuleTest.php index 8041b0f4ce..24080f2e00 100644 --- a/tests/PHPStan/Rules/Comparison/LogicalXorConstantConditionRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/LogicalXorConstantConditionRuleTest.php @@ -65,6 +65,14 @@ public function testRule(): void 20, $tipText, ], + [ + 'Left side of xor is always true.', + 24, + ], + [ + 'Right side of xor is always false.', + 24, + ], ]); } diff --git a/tests/PHPStan/Rules/Comparison/data/logical-xor.php b/tests/PHPStan/Rules/Comparison/data/logical-xor.php index a88068e6fd..fe63eb640b 100644 --- a/tests/PHPStan/Rules/Comparison/data/logical-xor.php +++ b/tests/PHPStan/Rules/Comparison/data/logical-xor.php @@ -20,6 +20,8 @@ public function doFoo($a, $b) if ($a xor $b) { } + + 1 xor 0; } }