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