Skip to content

Commit

Permalink
[DowngradePhp70] Handle increment variable assign on existing variabl…
Browse files Browse the repository at this point in the history
…e on DowngradeSpaceshipRector (#565)

* [DowngradePhp70] Handle increment variable assign on existing variable on DowngradeSpaceshipRector

* var

* update to use VariableNaming service
  • Loading branch information
samsonasik committed Aug 1, 2021
1 parent c9f07a9 commit 00015a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class ExistingVariableAssign
if ($a <=> $b) {
}
}

public function run2($a, $b)
{
$battleShipcompare = 'test';
$battleShipcompare2 = 'test2';
if ($a <=> $b) {
}
}
}

?>
Expand All @@ -23,13 +31,27 @@ class ExistingVariableAssign
public function run($a, $b)
{
$battleShipcompare = 'test';
$battleShipcompare1 = function ($left, $right) {
$battleShipcompare2 = function ($left, $right) {
if ($left === $right) {
return 0;
}
return $left < $right ? -1 : 1;
};
if ($battleShipcompare2($a, $b)) {
}
}

public function run2($a, $b)
{
$battleShipcompare = 'test';
$battleShipcompare2 = 'test2';
$battleShipcompare3 = function ($left, $right) {
if ($left === $right) {
return 0;
}
return $left < $right ? -1 : 1;
};
if ($battleShipcompare1($a, $b)) {
if ($battleShipcompare3($a, $b)) {
}
}
}
Expand Down
29 changes: 7 additions & 22 deletions rules/DowngradePhp70/Rector/Spaceship/DowngradeSpaceshipRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -31,7 +31,8 @@
final class DowngradeSpaceshipRector extends AbstractRector
{
public function __construct(
private IfManipulator $ifManipulator
private IfManipulator $ifManipulator,
private VariableNaming $variableNaming
) {
}

Expand Down Expand Up @@ -93,7 +94,10 @@ public function refactor(Node $node): FuncCall
$anonymousFunction->stmts[1] = new Return_($ternary);

$currentStatement = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
$variableAssign = $this->getVariableAssign($currentStatement);
$scope = $currentStatement->getAttribute(AttributeKey::SCOPE);

$variableAssignName = $this->variableNaming->createCountedValueName('battleShipcompare', $scope);
$variableAssign = new Variable($variableAssignName);
$assignExpression = $this->getAssignExpression($anonymousFunction, $variableAssign);
$this->addNodeBeforeNode($assignExpression, $currentStatement);

Expand All @@ -104,23 +108,4 @@ private function getAssignExpression(Closure $closure, Variable $variable): Expr
{
return new Expression(new Assign($variable, $closure));
}

private function getVariableAssign(Stmt $stmt, string $variableName = 'battleShipcompare'): Variable
{
$variable = new Variable($variableName);

$isFoundPrevious = (bool) $this->betterNodeFinder->findFirstPreviousOfNode(
$stmt,
fn (Node $node): bool => $node instanceof Variable && $this->nodeComparator->areNodesEqual($node, $variable)
);

$count = 0;
if ($isFoundPrevious) {
++$count;
$variableName .= (string) $count;
return $this->getVariableAssign($stmt, $variableName);
}

return $variable;
}
}

0 comments on commit 00015a4

Please sign in to comment.