Skip to content

Commit

Permalink
Move FileWithoutNamespaceNodeTraverser outside RectorNodeTraverser (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Apr 25, 2022
1 parent 69de62a commit 4981ebe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
8 changes: 6 additions & 2 deletions src/Application/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Core\Application;

use Rector\ChangesReporting\Collector\AffectedFilesCollector;
use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
Expand All @@ -17,7 +18,8 @@ public function __construct(
private readonly AffectedFilesCollector $affectedFilesCollector,
private readonly NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
private readonly RectorParser $rectorParser,
private readonly RectorNodeTraverser $rectorNodeTraverser
private readonly RectorNodeTraverser $rectorNodeTraverser,
private readonly FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser,
) {
}

Expand All @@ -36,7 +38,9 @@ public function parseFileInfoToLocalCache(File $file): void

public function refactor(File $file, Configuration $configuration): void
{
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts());
$newStmts = $this->fileWithoutNamespaceNodeTraverser->traverse($file->getNewStmts());
$newStmts = $this->rectorNodeTraverser->traverse($newStmts);

$file->changeNewStmts($newStmts);

$this->affectedFilesCollector->removeFromList($file);
Expand Down
20 changes: 1 addition & 19 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
namespace Rector\Core\PhpParser\NodeTraverser;

use PhpParser\Node;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\VersionBonding\PhpVersionedFilter;

final class RectorNodeTraverser extends NodeTraverser
Expand All @@ -22,7 +18,6 @@ final class RectorNodeTraverser extends NodeTraverser
*/
public function __construct(
private readonly array $phpRectors,
private readonly NodeFinder $nodeFinder,
private readonly PhpVersionedFilter $phpVersionedFilter
) {
}
Expand All @@ -35,20 +30,7 @@ public function __construct(
public function traverse(array $nodes): array
{
$this->prepareNodeVisitors();

$hasNamespace = (bool) $this->nodeFinder->findFirstInstanceOf($nodes, Namespace_::class);
if (! $hasNamespace && $nodes !== []) {
$fileWithoutNamespace = new FileWithoutNamespace($nodes);
foreach ($nodes as $node) {
$node->setAttribute(AttributeKey::PARENT_NODE, $fileWithoutNamespace);
}

$nodesToTraverse = [$fileWithoutNamespace];
} else {
$nodesToTraverse = $nodes;
}

return parent::traverse($nodesToTraverse);
return parent::traverse($nodes);
}

/**
Expand Down

0 comments on commit 4981ebe

Please sign in to comment.