Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;

class WithParentheses
{
public function run()
{
$a = 1 * (2 + 3) * 4;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;

class WithParentheses
{
public function run()
{
$a = (2 + 3) * 4;
}
}

?>
14 changes: 14 additions & 0 deletions rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down