add NodeTraverserFactory

This commit is contained in:
TomasVotruba 2017-08-08 17:55:38 +02:00
parent 70bb18f099
commit 102298d49a
7 changed files with 61 additions and 9 deletions

View File

@ -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'
);
}
}

View 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;
}
}

View File

@ -14,6 +14,11 @@ final class TokenSwitcher
$this->isEnabled = true;
}
public function disable(): void
{
$this->isEnabled = false;
}
public function isEnabled(): bool
{
return $this->isEnabled;

View File

@ -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);

View File

@ -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);
}

View File

@ -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: ~

View File

@ -6,6 +6,7 @@ class ClassWithInjects
* @var stdClass
*/
private $property;
/**
* @var DateTimeInterface
*/