From 0c1ec7c0afb933ec17b9ea9596ef18121f01e6d9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 30 Mar 2026 06:55:25 +0700 Subject: [PATCH] [CodeQuality] Hnadle with negation binary op previous if on CombineIfRector --- ...with_negation_binaryop_previous_if.php.inc | 31 +++++++++++++++++++ .../Rector/If_/CombineIfRector.php | 5 +++ 2 files changed, 36 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/If_/CombineIfRector/Fixture/with_negation_binaryop_previous_if.php.inc diff --git a/rules-tests/CodeQuality/Rector/If_/CombineIfRector/Fixture/with_negation_binaryop_previous_if.php.inc b/rules-tests/CodeQuality/Rector/If_/CombineIfRector/Fixture/with_negation_binaryop_previous_if.php.inc new file mode 100644 index 00000000000..920b5b7795a --- /dev/null +++ b/rules-tests/CodeQuality/Rector/If_/CombineIfRector/Fixture/with_negation_binaryop_previous_if.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules/CodeQuality/Rector/If_/CombineIfRector.php b/rules/CodeQuality/Rector/If_/CombineIfRector.php index 17c81ccb391..b3e13d6f28c 100644 --- a/rules/CodeQuality/Rector/If_/CombineIfRector.php +++ b/rules/CodeQuality/Rector/If_/CombineIfRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\BinaryOp; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; @@ -102,6 +103,10 @@ public function refactor(Node $node): ?Node $subIf->cond->left->setAttribute(AttributeKey::ORIGINAL_NODE, null); } + if ($node->cond instanceof BooleanNot && $node->cond->expr instanceof BinaryOp) { + $node->cond->expr->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + $node->cond = new BooleanAnd($node->cond, $subIf->cond); $node->stmts = $subIf->stmts;