Skip to content

Commit

Permalink
Cleanup usage of NodesToAddCollector, return nodes directly instead (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 28, 2023
1 parent 3fb912c commit 25d6a97
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 57 deletions.
5 changes: 0 additions & 5 deletions rules/Php72/Rector/Assign/ListEachRector.php
Expand Up @@ -92,8 +92,6 @@ public function refactor(Node $node)
// only value: list(, $value) = each($values);
if ($listNode->items[1] instanceof ArrayItem && ! $listNode->items[0] instanceof ArrayItem) {
$nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args);
// $this->nodesToAddCollector->addNodeAfterNode($nextFuncCall, $assign);

$currentFuncCall = $this->nodeFactory->createFuncCall('current', $eachFuncCall->args);

$secondArrayItem = $listNode->items[1];
Expand All @@ -111,11 +109,8 @@ public function refactor(Node $node)
}

$currentAssign = new Assign($secondArrayItem->value, $currentFuncCall);
// $this->nodesToAddCollector->addNodeAfterNode($assign, $assign);

$nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args);
// $this->nodesToAddCollector->addNodeAfterNode($nextFuncCall, $node);

$keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args);

$firstArrayItem = $listNode->items[0];
Expand Down
22 changes: 4 additions & 18 deletions tests/Issues/AddNodeAfterNodeStmt/Source/AddNextNopRector.php
Expand Up @@ -14,14 +14,8 @@
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class AddNextNopRector extends AbstractRector
final class AddNextNopRector extends AbstractRector
{
private array $justAdded = [];

public function __construct(private readonly NodesToAddCollector $nodesToAddCollector)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('uff', []);
Expand All @@ -36,10 +30,6 @@ public function getNodeTypes(): array

public function refactor(Node $node)
{
if (isset($this->justAdded[$this->file->getFilePath()])) {
return null;
}

$echo = new Echo_([new String_("this is new stmt after Nop")]);

$phpDocInfo = $this->phpDocInfoFactory->createEmpty($echo);
Expand All @@ -51,13 +41,9 @@ public function refactor(Node $node)
)
);

$this->nodesToAddCollector->addNodeAfterNode(
return [
$node,
$echo,
$node
);

$this->justAdded[$this->file->getFilePath()] = true;

return $node;
];
}
}
22 changes: 4 additions & 18 deletions tests/Issues/AddNodeAfterNodeStmt/Source/AddNextStmtRector.php
Expand Up @@ -12,14 +12,8 @@
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class AddNextStmtRector extends AbstractRector
final class AddNextStmtRector extends AbstractRector
{
private array $justAdded = [];

public function __construct(private readonly NodesToAddCollector $nodesToAddCollector)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('uff', []);
Expand All @@ -34,17 +28,9 @@ public function getNodeTypes(): array

public function refactor(Node $node)
{
if (isset($this->justAdded[$this->file->getFilePath()])) {
return null;
}

$this->nodesToAddCollector->addNodeAfterNode(
return [
$node,
new Echo_([new String_("this is new stmt after if")]),
$node
);

$this->justAdded[$this->file->getFilePath()] = true;

return $node;
];
}
}
18 changes: 2 additions & 16 deletions tests/Issues/AddNodeBeforeNodeStmt/Source/AddBeforeStmtRector.php
Expand Up @@ -14,12 +14,6 @@

class AddBeforeStmtRector extends AbstractRector
{
private array $justAdded = [];

public function __construct(private readonly NodesToAddCollector $nodesToAddCollector)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('uff', []);
Expand All @@ -34,17 +28,9 @@ public function getNodeTypes(): array

public function refactor(Node $node)
{
if (isset($this->justAdded[$this->file->getFilePath()])) {
return null;
}

$this->nodesToAddCollector->addNodeBeforeNode(
return [
new Echo_([new String_("this is new stmt before if")]),
$node
);

$this->justAdded[$this->file->getFilePath()] = true;

return $node;
];
}
}

0 comments on commit 25d6a97

Please sign in to comment.