Skip to content

Commit

Permalink
skip variable if used as next
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 3, 2022
1 parent 6da5c41 commit e112ec6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector\Fixture;

final class SkipUsedLater
{
private int $temporaryValue;

public function run()
{
$result = 100;

$this->temporaryValue = $result;

if ($result === 100) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
Expand Down Expand Up @@ -79,33 +80,25 @@ public function refactor(Node $node): ?Node

foreach ($stmts as $key => $stmt) {
$nextStmt = $stmts[$key + 1] ?? null;

if (! $stmt instanceof Expression) {
if (! $nextStmt instanceof Stmt) {
continue;
}

if (! $stmt->expr instanceof Assign) {
$currentAssign = $this->matchExpressionAssign($stmt);
if (! $currentAssign instanceof Assign) {
continue;
}

$currentAssign = $stmt->expr;


if (! $nextStmt instanceof Expression) {
$nextAssign = $this->matchExpressionAssign($nextStmt);
if (! $nextAssign instanceof Assign) {
continue;
}

if (! $nextStmt->expr instanceof Assign) {
continue;
}

$nextAssign = $nextStmt->expr;

if ($this->areTooComplexAssignsToShorten($currentAssign, $nextAssign)) {
continue;
}

if (! $this->areTwoVariablesCrossAssign($currentAssign, $nextAssign)) {
if (! $this->areTwoVariablesCrossAssign($currentAssign, $nextAssign, $node)) {
continue;
}

Expand All @@ -128,9 +121,14 @@ public function refactor(Node $node): ?Node
*
* $<some> = 1000;
* $this->value = $<some>;
*
* + not used $<some> bellow, so removal will not break it
*/
private function areTwoVariablesCrossAssign(Assign $currentAssign, Assign $nextAssign): bool
{
private function areTwoVariablesCrossAssign(
Assign $currentAssign,
Assign $nextAssign,
StmtsAwareInterface $stmtsAware
): bool {
// is just re-assign to variable
if (! $currentAssign->var instanceof Variable) {
return false;
Expand All @@ -148,7 +146,31 @@ private function areTwoVariablesCrossAssign(Assign $currentAssign, Assign $nextA
return false;
}

return ! $this->variableAnalyzer->isUsedByReference($nextAssign->expr);
if ($this->variableAnalyzer->isUsedByReference($nextAssign->expr)) {
return false;
}

$currentVariable = $currentAssign->var;
$nextVariable = $nextAssign->expr;

// is variable used later?
$nextUsedVariable = $this->betterNodeFinder->findFirst($stmtsAware, function (\PhpParser\Node $node) use (
$currentVariable,
$nextVariable
) {
if (in_array($node, [$currentVariable, $nextVariable], true)) {
return false;
}

if (! $node instanceof Variable) {
return false;
}

// is variable name?
return $this->nodeNameResolver->areNamesEqual($node, $currentVariable);
});

return ! $nextUsedVariable instanceof Variable;
}

/**
Expand All @@ -166,4 +188,17 @@ private function areTooComplexAssignsToShorten(Assign $currentAssign, Assign $ne

return $nextAssign->var instanceof ArrayDimFetch;
}

private function matchExpressionAssign(Stmt $stmt): ?Assign
{
if (! $stmt instanceof Expression) {
return null;
}

if (! $stmt->expr instanceof Assign) {
return null;
}

return $stmt->expr;
}
}

0 comments on commit e112ec6

Please sign in to comment.