mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 07:22:43 +02:00
add NodeTraverserFactory
This commit is contained in:
parent
70bb18f099
commit
102298d49a
@ -2,8 +2,8 @@
|
||||
|
||||
namespace Rector\DependencyInjection\CompilerPass;
|
||||
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitor;
|
||||
use Rector\NodeTraverser\NodeTraverserFactory;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
@ -32,9 +32,9 @@ final class CollectorCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
DefinitionCollector::loadCollectorWithType(
|
||||
$containerBuilder,
|
||||
NodeTraverser::class,
|
||||
NodeTraverserFactory::class,
|
||||
NodeVisitor::class,
|
||||
'addVisitor'
|
||||
'addNodeVisitor'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
34
src/NodeTraverser/NodeTraverserFactory.php
Normal file
34
src/NodeTraverser/NodeTraverserFactory.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeTraverser;
|
||||
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitor;
|
||||
use PhpParser\NodeVisitor\CloningVisitor;
|
||||
|
||||
final class NodeTraverserFactory
|
||||
{
|
||||
/**
|
||||
* @var NodeVisitor[]
|
||||
*/
|
||||
private $nodeVisitors = [];
|
||||
|
||||
public function addNodeVisitor(NodeVisitor $nodeVisitor): void
|
||||
{
|
||||
$this->nodeVisitors[] = $nodeVisitor;
|
||||
}
|
||||
|
||||
public function create(): NodeTraverser
|
||||
{
|
||||
$nodeTraverser = new NodeTraverser;
|
||||
|
||||
// this one has priority
|
||||
$nodeTraverser->addVisitor(new CloningVisitor);
|
||||
|
||||
foreach ($this->nodeVisitors as $nodeVisitor) {
|
||||
$nodeTraverser->addVisitor($nodeVisitor);
|
||||
}
|
||||
|
||||
return $nodeTraverser;
|
||||
}
|
||||
}
|
@ -14,6 +14,11 @@ final class TokenSwitcher
|
||||
$this->isEnabled = true;
|
||||
}
|
||||
|
||||
public function disable(): void
|
||||
{
|
||||
$this->isEnabled = false;
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->isEnabled;
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Builder\Class_\ClassPropertyCollector;
|
||||
use Rector\Builder\ConstructorMethodBuilder;
|
||||
use Rector\Builder\PropertyBuilder;
|
||||
use Rector\NodeTraverser\TokenSwitcher;
|
||||
|
||||
/**
|
||||
* Add new propertis to class and to contructor.
|
||||
@ -29,14 +30,21 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||
*/
|
||||
private $newClassPropertyCollector;
|
||||
|
||||
/**
|
||||
* @var TokenSwitcher
|
||||
*/
|
||||
private $tokenSwitcher;
|
||||
|
||||
public function __construct(
|
||||
ConstructorMethodBuilder $constructorMethodBuilder,
|
||||
PropertyBuilder $propertyBuilder,
|
||||
ClassPropertyCollector $newClassPropertyCollector
|
||||
ClassPropertyCollector $newClassPropertyCollector,
|
||||
TokenSwitcher $tokenSwitcher
|
||||
) {
|
||||
$this->constructorMethodBuilder = $constructorMethodBuilder;
|
||||
$this->propertyBuilder = $propertyBuilder;
|
||||
$this->newClassPropertyCollector = $newClassPropertyCollector;
|
||||
$this->tokenSwitcher = $tokenSwitcher;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,9 +56,13 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||
foreach ($nodes as $key => $node) {
|
||||
if ($node instanceof Class_) {
|
||||
$nodes[$key] = $this->reconstruct($node, (string) $node->name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// this does!
|
||||
$this->tokenSwitcher->disable();
|
||||
|
||||
return $nodes;
|
||||
}
|
||||
|
||||
@ -58,6 +70,8 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className);
|
||||
|
||||
dump($propertiesForClass);
|
||||
|
||||
foreach ($propertiesForClass as $propertyType => $propertyName) {
|
||||
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
|
||||
$this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName);
|
||||
|
@ -45,7 +45,6 @@ final class PropertyRector extends NodeVisitorAbstract
|
||||
*/
|
||||
public function beforeTraverse(array $nodes): ?array
|
||||
{
|
||||
dump('1');
|
||||
foreach ($nodes as $node) {
|
||||
if ($node instanceof Class_) {
|
||||
$this->className = (string) $node->name;
|
||||
@ -61,8 +60,6 @@ final class PropertyRector extends NodeVisitorAbstract
|
||||
return null;
|
||||
}
|
||||
|
||||
dump('2');
|
||||
|
||||
return $this->reconstructProperty($node);
|
||||
}
|
||||
|
||||
|
@ -20,9 +20,10 @@ services:
|
||||
factory: ['@Rector\Parser\LexerFactory', 'create']
|
||||
PhpParser\BuilderFactory: ~
|
||||
|
||||
PhpParser\NodeTraverser:
|
||||
factory: ['@Rector\NodeTraverser\NodeTraverserFactory', 'create']
|
||||
|
||||
# Traverser
|
||||
PhpParser\NodeTraverser: ~
|
||||
PhpParser\ParserFactory: ~
|
||||
# Printer
|
||||
PhpParser\NodeVisitor\CloningVisitor: ~
|
||||
PhpParser\PrettyPrinter\Standard: ~
|
||||
|
@ -6,6 +6,7 @@ class ClassWithInjects
|
||||
* @var stdClass
|
||||
*/
|
||||
private $property;
|
||||
|
||||
/**
|
||||
* @var DateTimeInterface
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user