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,21 @@
<?php

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

function yAxisScale($results)
{
$stats = (object) [
'maxRange' => 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;
}
36 changes: 19 additions & 17 deletions rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,11 +30,6 @@
*/
final class RemoveDeadZeroAndOneOperationRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -157,17 +152,15 @@ 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);
}

return $binaryOp->right;
}

if (! $this->valueResolver->isValue($binaryOp->right, 0)) {
if (! $this->isLiteralZero($binaryOp->right)) {
return null;
}

Expand Down Expand Up @@ -218,21 +211,30 @@ 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);
}

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;
}
}