mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 03:35:01 +01:00
PseudoNamespaceToNamespaceRector - use StatementGlue
This commit is contained in:
parent
7203e18c7f
commit
e323204acb
@ -17,7 +17,7 @@ final class StatementGlue
|
||||
{
|
||||
foreach ($classNode->stmts as $key => $classElementNode) {
|
||||
if ($classElementNode instanceof ClassMethod) {
|
||||
$this->insertBefore($classNode, $node, $key);
|
||||
$classNode->stmts = $this->insertBefore($classNode->stmts, $node, $key);
|
||||
|
||||
return;
|
||||
}
|
||||
@ -26,7 +26,7 @@ final class StatementGlue
|
||||
$previousElement = null;
|
||||
foreach ($classNode->stmts as $key => $classElementNode) {
|
||||
if ($previousElement instanceof Property && ! $classElementNode instanceof Property) {
|
||||
$this->insertBefore($classNode, $node, $key);
|
||||
$classNode->stmts = $this->insertBefore($classNode->stmts, $node, $key);
|
||||
|
||||
return;
|
||||
}
|
||||
@ -47,7 +47,7 @@ final class StatementGlue
|
||||
foreach ($types as $type) {
|
||||
foreach ($classNode->stmts as $key => $classElementNode) {
|
||||
if (is_a($classElementNode, $type, true)) {
|
||||
$this->insertBefore($classNode, $node, $key);
|
||||
$classNode->stmts = $this->insertBefore($classNode->stmts, $node, $key);
|
||||
|
||||
return;
|
||||
}
|
||||
@ -58,10 +58,13 @@ final class StatementGlue
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo decouple to statements added
|
||||
* @param Node[] $nodes
|
||||
* @return Node[] $nodes
|
||||
*/
|
||||
private function insertBefore(Class_ $classNode, Node $node, int $key): void
|
||||
public function insertBefore(array $nodes, Node $node, int $key): array
|
||||
{
|
||||
array_splice($classNode->stmts, $key, 0, [$node]);
|
||||
array_splice($nodes, $key, 0, [$node]);
|
||||
|
||||
return $nodes;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Nop;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use Rector\Builder\StatementGlue;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
@ -40,13 +41,19 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector
|
||||
*/
|
||||
private $nodeFactory;
|
||||
|
||||
/**
|
||||
* @var StatementGlue
|
||||
*/
|
||||
private $statementGlue;
|
||||
|
||||
/**
|
||||
* @param string[] $pseudoNamespacePrefixes
|
||||
*/
|
||||
public function __construct(array $pseudoNamespacePrefixes, NodeFactory $nodeFactory)
|
||||
public function __construct(array $pseudoNamespacePrefixes, NodeFactory $nodeFactory, StatementGlue $statementGlue)
|
||||
{
|
||||
$this->pseudoNamespacePrefixes = $pseudoNamespacePrefixes;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->statementGlue = $statementGlue;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,8 +130,8 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector
|
||||
|
||||
foreach ($nodes as $key => $node) {
|
||||
if ($node instanceof Class_) {
|
||||
$nodes = $this->insertBefore($nodes, $namespaceNode, $key);
|
||||
$nodes = $this->insertBefore($nodes, new Nop, $key);
|
||||
$nodes = $this->statementGlue->insertBefore($nodes, $namespaceNode, $key);
|
||||
$nodes = $this->statementGlue->insertBefore($nodes, new Nop, $key);
|
||||
|
||||
break;
|
||||
}
|
||||
@ -146,15 +153,4 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return Node[]
|
||||
*/
|
||||
private function insertBefore(array $nodes, Node $addedNode, int $key): array
|
||||
{
|
||||
array_splice($nodes, $key, 0, [$addedNode]);
|
||||
|
||||
return $nodes;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user