Add NodeScopeAndMetadataDecorator

This commit is contained in:
Tomas Votruba 2018-08-13 13:04:18 +02:00
parent 2136624f53
commit 547378054a
3 changed files with 55 additions and 26 deletions

View File

@ -3,11 +3,9 @@
namespace Rector\NodeTraverserQueue;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use Rector\NodeTraverser\RectorNodeTraverser;
use Rector\NodeTraverser\StandaloneTraverseNodeTraverser;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeScopeResolver;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\Parser\Parser;
use Symfony\Component\Finder\SplFileInfo;
@ -34,22 +32,22 @@ final class NodeTraverserQueue
private $standaloneTraverseNodeTraverser;
/**
* @var NodeScopeResolver
* @var NodeScopeAndMetadataDecorator
*/
private $nodeScopeResolver;
private $nodeScopeAndMetadataDecorator;
public function __construct(
Parser $parser,
Lexer $lexer,
RectorNodeTraverser $rectorNodeTraverser,
StandaloneTraverseNodeTraverser $standaloneTraverseNodeTraverser,
NodeScopeResolver $nodeScopeResolver
NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator
) {
$this->parser = $parser;
$this->lexer = $lexer;
$this->rectorNodeTraverser = $rectorNodeTraverser;
$this->standaloneTraverseNodeTraverser = $standaloneTraverseNodeTraverser;
$this->nodeScopeResolver = $nodeScopeResolver;
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
}
/**
@ -60,13 +58,7 @@ final class NodeTraverserQueue
$oldStmts = $this->parser->parseFile($fileInfo->getRealPath());
$oldTokens = $this->lexer->getTokens();
$newStmts = $oldStmts;
// @todo use NodeTypeResolverNodeTraverses
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NameResolver());
$newStmts = $nodeTraverser->traverse($newStmts);
$this->nodeScopeResolver->processNodes($newStmts, $fileInfo);
$newStmts = $this->nodeScopeAndMetadataDecorator->processNodesAndSplFileInfo($oldStmts, $fileInfo);
$newStmts = $this->standaloneTraverseNodeTraverser->traverse($newStmts);
$newStmts = $this->rectorNodeTraverser->traverse($newStmts);

View File

@ -0,0 +1,49 @@
<?php declare(strict_types=1);
namespace Rector\NodeTypeResolver;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use Rector\NodeTypeResolver\NodeVisitor\MetadataNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeScopeResolver;
use Symfony\Component\Finder\SplFileInfo;
final class NodeScopeAndMetadataDecorator
{
/**
* @var NodeScopeResolver
*/
private $nodeScopeResolver;
/**
* @var MetadataNodeVisitor
*/
private $metadataNodeVisitor;
public function __construct(NodeScopeResolver $nodeScopeResolver, MetadataNodeVisitor $metadataNodeVisitor)
{
$this->nodeScopeResolver = $nodeScopeResolver;
$this->metadataNodeVisitor = $metadataNodeVisitor;
}
/**
* @param Node[] $nodes
* @return Node[]
*/
public function processNodesAndSplFileInfo(array $nodes, SplFileInfo $splFileInfo): array
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NameResolver());
$nodeTraverser->traverse($nodes);
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($this->metadataNodeVisitor);
$nodeTraverser->traverse($nodes);
$nodes = $nodeTraverser->traverse($nodes);
$this->nodeScopeResolver->processNodes($nodes, $splFileInfo);
return $nodes;
}
}

View File

@ -6,8 +6,6 @@ use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeTraverserInterface;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitor\NameResolver;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeScopeResolver;
/**
* Oppose to NodeTraverser, that traverse ONE node by ALL NodeVisitors,
@ -20,16 +18,6 @@ final class StandaloneTraverseNodeTraverser
*/
private $nodeTraversers = [];
/**
* @var NodeScopeResolver
*/
private $nodeScopeResolver;
public function __construct(NodeScopeResolver $nodeScopeResolver)
{
$this->nodeScopeResolver = $nodeScopeResolver;
}
public function addNodeVisitor(NodeVisitor $nodeVisitor): void
{
$nodeTraverser = new NodeTraverser();