diff --git a/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_non_literal_zero_or_one.php.inc b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_non_literal_zero_or_one.php.inc new file mode 100644 index 00000000000..bf35c4cceb3 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_non_literal_zero_or_one.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; + } }