Skip to content

Commit

Permalink
[DeadCode] RemoveDeadZeroAndOneOperationRector should not remove Cons…
Browse files Browse the repository at this point in the history
…tants (#4910)
  • Loading branch information
TwanVermeulen committed Sep 19, 2023
1 parent 95e1147 commit 3ffd995
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
@@ -0,0 +1,37 @@
<?php

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

class SkipConstants
{
public const ONE = 1;

public function run()
{
$value = 5 * self::ONE;
$value = 5 / self::ONE;
$value = self::ONE * 5;
$value = self::ONE / 5;
}
}

?>
-----
<?php

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

class SkipConstants
{
public const ONE = 1;

public function run()
{
$value = 5 * self::ONE;
$value = 5 / self::ONE;
$value = self::ONE * 5;
$value = self::ONE / 5;
}
}

?>
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Expr\BinaryOp\Mul;
use PhpParser\Node\Expr\BinaryOp\Plus;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\UnaryMinus;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -169,6 +170,10 @@ private function processBinaryPlusAndMinus(Plus | Minus $binaryOp): ?Expr

private function processBinaryMulAndDiv(Mul | Div $binaryOp): ?Expr
{
if ($binaryOp->left instanceof ClassConstFetch || $binaryOp->right instanceof ClassConstFetch) {
return null;
}

if (! $this->areNumberType($binaryOp)) {
return null;
}
Expand Down

0 comments on commit 3ffd995

Please sign in to comment.