Skip to content

Commit a1fffb3

Browse files
committed
Bleeding edge - report top-level xor because that's probably not what the user intended to do
1 parent 3b011f6 commit a1fffb3

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

conf/config.level4.neon

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ includes:
33

44
rules:
55
- PHPStan\Rules\Arrays\DeadForeachRule
6-
- PHPStan\Rules\DeadCode\NoopRule
76
- PHPStan\Rules\DeadCode\UnreachableStatementRule
87
- PHPStan\Rules\DeadCode\UnusedPrivateConstantRule
98
- PHPStan\Rules\DeadCode\UnusedPrivateMethodRule
@@ -68,6 +67,13 @@ services:
6867
tags:
6968
- phpstan.rules.rule
7069

70+
-
71+
class: PHPStan\Rules\DeadCode\NoopRule
72+
arguments:
73+
logicalXor: %featureToggles.logicalXor%
74+
tags:
75+
- phpstan.rules.rule
76+
7177
-
7278
class: PHPStan\Rules\DeadCode\UnusedPrivatePropertyRule
7379
arguments:

src/Rules/DeadCode/NoopRule.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class NoopRule implements Rule
1616
{
1717

18-
public function __construct(private ExprPrinter $exprPrinter)
18+
public function __construct(private ExprPrinter $exprPrinter, private bool $logicalXor)
1919
{
2020
}
2121

@@ -36,6 +36,15 @@ public function processNode(Node $node, Scope $scope): array
3636
) {
3737
$expr = $expr->expr;
3838
}
39+
if ($this->logicalXor && $expr instanceof Node\Expr\BinaryOp\LogicalXor) {
40+
return [
41+
RuleErrorBuilder::message(
42+
'Unused result of "xor" operator.',
43+
)->line($expr->getLine())
44+
->tip('This operator has unexpected precedence, try disambiguating the logic with parentheses ().')
45+
->build(),
46+
];
47+
}
3948
if (
4049
!$expr instanceof Node\Expr\Variable
4150
&& !$expr instanceof Node\Expr\PropertyFetch

tests/PHPStan/Rules/DeadCode/NoopRuleTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class NoopRuleTest extends RuleTestCase
1515

1616
protected function getRule(): Rule
1717
{
18-
return new NoopRule(new ExprPrinter(new Printer()));
18+
return new NoopRule(new ExprPrinter(new Printer()), true);
1919
}
2020

2121
public function testRule(): void
@@ -77,6 +77,11 @@ public function testRule(): void
7777
'Expression "(string) 1" on a separate line does not do anything.',
7878
30,
7979
],
80+
[
81+
'Unused result of "xor" operator.',
82+
32,
83+
'This operator has unexpected precedence, try disambiguating the logic with parentheses ().',
84+
],
8085
]);
8186
}
8287

tests/PHPStan/Rules/DeadCode/data/noop.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace DeadCodeNoop;
44

5-
function (stdClass $foo) {
5+
function (stdClass $foo, bool $a, bool $b) {
66
$foo->foo();
77

88
$arr = [];
@@ -28,4 +28,6 @@ function (stdClass $foo) {
2828
Foo::TEST;
2929

3030
(string) 1;
31+
32+
$r = $a xor $b;
3133
};

0 commit comments

Comments
 (0)