diff --git a/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/with_parentheses.php.inc b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/with_parentheses.php.inc new file mode 100644 index 00000000000..1a2fc7268c4 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/with_parentheses.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php b/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php index 0cf1c2f05a1..de8f0d0f620 100644 --- a/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php +++ b/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php @@ -18,8 +18,10 @@ use PhpParser\Node\Expr\BinaryOp\Plus; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\UnaryMinus; +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; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -172,6 +174,14 @@ private function processBinaryPlusAndMinus(Plus | Minus $binaryOp): ?Expr return $binaryOp->left; } + private function isMulParenthesized(File $file, Mul $mul): bool + { + $oldTokens = $file->getOldTokens(); + $endTokenPost = $mul->getEndTokenPos(); + + return isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === ')'; + } + private function processBinaryMulAndDiv(Mul | Div $binaryOp): ?Expr { if ($binaryOp->left instanceof ClassConstFetch || $binaryOp->right instanceof ClassConstFetch) { @@ -186,6 +196,10 @@ private function processBinaryMulAndDiv(Mul | Div $binaryOp): ?Expr $binaryOp->left, 1 ) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) { + if ($this->isMulParenthesized($this->file, $binaryOp)) { + $binaryOp->right->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true); + } + return $binaryOp->right; }