Skip to content

Commit

Permalink
Bleeding edge - report top-level xor because that's probably not what…
Browse files Browse the repository at this point in the history
… the user intended to do
  • Loading branch information
ondrejmirtes committed Dec 7, 2023
1 parent 3b011f6 commit a1fffb3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
8 changes: 7 additions & 1 deletion conf/config.level4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ includes:

rules:
- PHPStan\Rules\Arrays\DeadForeachRule
- PHPStan\Rules\DeadCode\NoopRule
- PHPStan\Rules\DeadCode\UnreachableStatementRule
- PHPStan\Rules\DeadCode\UnusedPrivateConstantRule
- PHPStan\Rules\DeadCode\UnusedPrivateMethodRule
Expand Down Expand Up @@ -68,6 +67,13 @@ services:
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\DeadCode\NoopRule
arguments:
logicalXor: %featureToggles.logicalXor%
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\DeadCode\UnusedPrivatePropertyRule
arguments:
Expand Down
11 changes: 10 additions & 1 deletion src/Rules/DeadCode/NoopRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class NoopRule implements Rule
{

public function __construct(private ExprPrinter $exprPrinter)
public function __construct(private ExprPrinter $exprPrinter, private bool $logicalXor)
{
}

Expand All @@ -36,6 +36,15 @@ public function processNode(Node $node, Scope $scope): array
) {
$expr = $expr->expr;
}
if ($this->logicalXor && $expr instanceof Node\Expr\BinaryOp\LogicalXor) {
return [
RuleErrorBuilder::message(
'Unused result of "xor" operator.',
)->line($expr->getLine())
->tip('This operator has unexpected precedence, try disambiguating the logic with parentheses ().')
->build(),
];
}
if (
!$expr instanceof Node\Expr\Variable
&& !$expr instanceof Node\Expr\PropertyFetch
Expand Down
7 changes: 6 additions & 1 deletion tests/PHPStan/Rules/DeadCode/NoopRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NoopRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new NoopRule(new ExprPrinter(new Printer()));
return new NoopRule(new ExprPrinter(new Printer()), true);
}

public function testRule(): void
Expand Down Expand Up @@ -77,6 +77,11 @@ public function testRule(): void
'Expression "(string) 1" on a separate line does not do anything.',
30,
],
[
'Unused result of "xor" operator.',
32,
'This operator has unexpected precedence, try disambiguating the logic with parentheses ().',
],
]);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/PHPStan/Rules/DeadCode/data/noop.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DeadCodeNoop;

function (stdClass $foo) {
function (stdClass $foo, bool $a, bool $b) {
$foo->foo();

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

(string) 1;

$r = $a xor $b;
};

0 comments on commit a1fffb3

Please sign in to comment.