From 5df51eeadc8800916e31b05081e54f15ee3c8312 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 22 Feb 2026 04:53:33 +0700 Subject: [PATCH 1/2] [DeadCode] Skip dynamic value on RemoveDeadZeroAndOneOperationRector --- .../Fixture/skip_dynamic_value.php.inc | 21 +++++++++++ .../RemoveDeadZeroAndOneOperationRector.php | 36 ++++++++++--------- 2 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_dynamic_value.php.inc diff --git a/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_dynamic_value.php.inc b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_dynamic_value.php.inc new file mode 100644 index 00000000000..bf35c4cceb3 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_dynamic_value.php.inc @@ -0,0 +1,21 @@ + 0, + 'data' => [], + ]; + + foreach ($results as $item) { + if ($item->value > $stats->maxRange) { + $stats->maxRange = $item->value; + } + } + + $stats->maxRange = (intval(($stats->maxRange + 1) / 500) + 1) * 500; + + return $stats->maxRange; +} diff --git a/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php b/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php index 9eb19cb0087..a08d084aacc 100644 --- a/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php +++ b/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php @@ -18,8 +18,8 @@ use PhpParser\Node\Expr\BinaryOp\Plus; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\UnaryMinus; +use PhpParser\Node\Scalar\Int_; use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Rector\ValueObject\Application\File; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -30,11 +30,6 @@ */ final class RemoveDeadZeroAndOneOperationRector extends AbstractRector { - public function __construct( - private readonly ValueResolver $valueResolver - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -101,7 +96,7 @@ private function processAssignOp(AssignOp $assignOp): ?Expr { // +=, -= if ($assignOp instanceof AssignPlus || $assignOp instanceof AssignMinus) { - if (! $this->valueResolver->isValue($assignOp->expr, 0)) { + if (! $this->isLiteralZero($assignOp->expr)) { return null; } @@ -112,7 +107,7 @@ private function processAssignOp(AssignOp $assignOp): ?Expr // *, / if ($assignOp instanceof AssignMul || $assignOp instanceof AssignDiv) { - if (! $this->valueResolver->isValue($assignOp->expr, 1)) { + if (! $this->isLiteralOne($assignOp->expr)) { return null; } @@ -157,9 +152,7 @@ private function processBinaryPlusAndMinus(Plus | Minus $binaryOp): ?Expr return null; } - if ($this->valueResolver->isValue($binaryOp->left, 0) && $this->nodeTypeResolver->isNumberType( - $binaryOp->right - )) { + if ($this->isLiteralZero($binaryOp->left) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) { if ($binaryOp instanceof Minus) { return new UnaryMinus($binaryOp->right); } @@ -167,7 +160,7 @@ private function processBinaryPlusAndMinus(Plus | Minus $binaryOp): ?Expr return $binaryOp->right; } - if (! $this->valueResolver->isValue($binaryOp->right, 0)) { + if (! $this->isLiteralZero($binaryOp->right)) { return null; } @@ -218,10 +211,9 @@ private function processBinaryMulAndDiv(Mul | Div $binaryOp): ?Expr return null; } - if ($binaryOp instanceof Mul && $this->valueResolver->isValue( - $binaryOp->left, - 1 - ) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) { + if ($binaryOp instanceof Mul && $this->isLiteralOne($binaryOp->left) && $this->nodeTypeResolver->isNumberType( + $binaryOp->right + )) { if ($this->isMulParenthesized($this->file, $binaryOp)) { $binaryOp->right->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true); } @@ -229,10 +221,20 @@ private function processBinaryMulAndDiv(Mul | Div $binaryOp): ?Expr return $binaryOp->right; } - if (! $this->valueResolver->isValue($binaryOp->right, 1)) { + if (! $this->isLiteralOne($binaryOp->right)) { return null; } return $binaryOp->left; } + + private function isLiteralOne(Expr $expr): bool + { + return $expr instanceof Int_ && $expr->value === 1; + } + + private function isLiteralZero(Expr $expr): bool + { + return $expr instanceof Int_ && $expr->value === 0; + } } From 2980e0a857d95eb54a5e14ba16376121c3e3d378 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 22 Feb 2026 04:58:57 +0700 Subject: [PATCH 2/2] rename fixture --- ...dynamic_value.php.inc => skip_non_literal_zero_or_one.php.inc} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/{skip_dynamic_value.php.inc => skip_non_literal_zero_or_one.php.inc} (100%) diff --git a/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_dynamic_value.php.inc b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_non_literal_zero_or_one.php.inc similarity index 100% rename from rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_dynamic_value.php.inc rename to rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_non_literal_zero_or_one.php.inc