Skip to content

Commit 92104b0

Browse files
authored
[CodeQuality] Hnadle with negation binary op previous if on CombineIfRector (#7945)
1 parent 68156b9 commit 92104b0

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\If_\CombineIfRector\Fixture;
4+
5+
class WithNegationBinaryOpPreviousIf
6+
{
7+
public function run($a, $b, $data, $field)
8+
{
9+
if (!($a && $b)) {
10+
if (isset($data[$field])) {
11+
}
12+
}
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\CodeQuality\Rector\If_\CombineIfRector\Fixture;
21+
22+
class WithNegationBinaryOpPreviousIf
23+
{
24+
public function run($a, $b, $data, $field)
25+
{
26+
if (!($a && $b) && isset($data[$field])) {
27+
}
28+
}
29+
}
30+
31+
?>

rules/CodeQuality/Rector/If_/CombineIfRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\BinaryOp;
99
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
10+
use PhpParser\Node\Expr\BooleanNot;
1011
use PhpParser\Node\Stmt\Else_;
1112
use PhpParser\Node\Stmt\If_;
1213
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
@@ -102,6 +103,10 @@ public function refactor(Node $node): ?Node
102103
$subIf->cond->left->setAttribute(AttributeKey::ORIGINAL_NODE, null);
103104
}
104105

106+
if ($node->cond instanceof BooleanNot && $node->cond->expr instanceof BinaryOp) {
107+
$node->cond->expr->setAttribute(AttributeKey::ORIGINAL_NODE, null);
108+
}
109+
105110
$node->cond = new BooleanAnd($node->cond, $subIf->cond);
106111

107112
$node->stmts = $subIf->stmts;

0 commit comments

Comments
 (0)