mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 00:10:51 +00:00
[DowngradePhp70] Handle increment variable assign on existing variable on DowngradeSpaceshipRector (#565)
* [DowngradePhp70] Handle increment variable assign on existing variable on DowngradeSpaceshipRector * var * update to use VariableNaming service
This commit is contained in:
parent
c9f07a955a
commit
00015a4390
|
@ -10,6 +10,14 @@ class ExistingVariableAssign
|
|||
if ($a <=> $b) {
|
||||
}
|
||||
}
|
||||
|
||||
public function run2($a, $b)
|
||||
{
|
||||
$battleShipcompare = 'test';
|
||||
$battleShipcompare2 = 'test2';
|
||||
if ($a <=> $b) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -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 ($battleShipcompare1($a, $b)) {
|
||||
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 ($battleShipcompare3($a, $b)) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@ use PhpParser\Node\Expr\Ternary;
|
|||
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;
|
||||
|
@ -31,7 +31,8 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
final class DowngradeSpaceshipRector extends AbstractRector
|
||||
{
|
||||
public function __construct(
|
||||
private IfManipulator $ifManipulator
|
||||
private IfManipulator $ifManipulator,
|
||||
private VariableNaming $variableNaming
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -93,7 +94,10 @@ CODE_SAMPLE
|
|||
$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);
|
||||
|
||||
|
@ -104,23 +108,4 @@ CODE_SAMPLE
|
|||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user