diff --git a/src/Builder/Class_/ClassPropertyCollector.php b/src/Builder/Class_/ClassPropertyCollector.php index 13f4bb9ee2e..038ba46c067 100644 --- a/src/Builder/Class_/ClassPropertyCollector.php +++ b/src/Builder/Class_/ClassPropertyCollector.php @@ -11,9 +11,7 @@ final class ClassPropertyCollector public function addPropertyForClass(string $class, string $propertyType, string $propertyName): void { - $this->classProperties[$class] = [ - $propertyType => $propertyName, - ]; + $this->classProperties[$class][$propertyType] = $propertyName; } /** diff --git a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php b/src/NodeVisitor/DependencyInjection/AddPropertiesToClassNodeVisitor.php similarity index 76% rename from src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php rename to src/NodeVisitor/DependencyInjection/AddPropertiesToClassNodeVisitor.php index 857f8630dea..dbc9f534c88 100644 --- a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php +++ b/src/NodeVisitor/DependencyInjection/AddPropertiesToClassNodeVisitor.php @@ -1,6 +1,6 @@ constructorMethodBuilder = $constructorMethodBuilder; $this->propertyBuilder = $propertyBuilder; $this->newClassPropertyCollector = $newClassPropertyCollector; - $this->tokenSwitcher = $tokenSwitcher; } /** @@ -53,24 +45,24 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract */ public function afterTraverse(array $nodes): array { - foreach ($nodes as $node) { + foreach ($nodes as $key => $node) { if ($node instanceof Class_) { - $this->reconstruct($node, (string) $node->name); - break; + $nodes[$key] = $this->reconstruct($node, (string) $node->name); } } return $nodes; } - private function reconstruct(Class_ $classNode, string $className): void + private function reconstruct(Class_ $classNode, string $className): Class_ { $propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className); foreach ($propertiesForClass as $propertyType => $propertyName) { - $this->tokenSwitcher->enable(); $this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName); $this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName); } + + return $classNode; } } diff --git a/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorRector.php b/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructor/PropertyRector.php similarity index 55% rename from src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorRector.php rename to src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructor/PropertyRector.php index 567c0113f63..c06c4f3eb2a 100644 --- a/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorRector.php +++ b/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructor/PropertyRector.php @@ -1,15 +1,17 @@ constructorMethodBuilder = $constructorMethodBuilder; -// } + /** + * @var ClassPropertyCollector + */ + private $classPropertyCollector; - public function __construct(TokenSwitcher $tokenSwitcher) + /** + * @var string + */ + private $className; + + public function __construct(TokenSwitcher $tokenSwitcher, ClassPropertyCollector $classPropertyCollector) { $this->tokenSwitcher = $tokenSwitcher; + $this->classPropertyCollector = $classPropertyCollector; + } + + /** + * @param Node[] $nodes + * @return null|array + */ + public function beforeTraverse(array $nodes): ?array + { + dump('1'); + foreach ($nodes as $node) { + if ($node instanceof Class_) { + $this->className = (string) $node->name; + } + } + + return null; } public function enterNode(Node $node): ?Node @@ -42,6 +61,8 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract return null; } + dump('2'); + return $this->reconstructProperty($node); } @@ -59,43 +80,14 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract return true; } -// private function reconstruct(Class_ $classNode): Node -// { -// dump($classNode); -// die; -// -// foreach ($classNode->stmts as $classElementStatement) { -// if (! $classElementStatement instanceof Property) { -// continue; -// } -// -// $propertyNode = $classElementStatement; -// -// $propertyDocBlock = $this->createDocBlockFromNode($propertyNode); -// $injectAnnotations = $propertyDocBlock->getAnnotationsOfType(self::ANNOTATION_INJECT); -// if (! $injectAnnotations) { -// continue; -// } -// -// $this->removeInjectAnnotationFromProperty($propertyNode, $propertyDocBlock); -// $this->makePropertyPrivate($propertyNode); -// -// $propertyType = $propertyDocBlock->getAnnotationsOfType('var')[0] -// ->getTypes()[0]; -// $propertyName = (string) $propertyNode->props[0]->name; -// -// $this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName); -// } -// -// return $classNode; -// } - private function reconstructProperty($propertyNode): Property { $propertyDocBlock = $this->createDocBlockFromNode($propertyNode); $propertyNode = $this->removeInjectAnnotationFromProperty($propertyNode, $propertyDocBlock); -// $propertyNode->flags = Class_::MODIFIER_PRIVATE; + $propertyNode->flags = Class_::MODIFIER_PRIVATE; + + $this->addPropertyToCollector($propertyNode, $propertyDocBlock); return $propertyNode; } @@ -115,7 +107,6 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract private function removeInjectAnnotationFromProperty(Property $propertyNode, DocBlock $propertyDocBlock): Property { $injectAnnotations = $propertyDocBlock->getAnnotationsOfType(self::ANNOTATION_INJECT); - foreach ($injectAnnotations as $injectAnnotation) { $injectAnnotation->remove(); } @@ -124,4 +115,14 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract return $propertyNode; } + + private function addPropertyToCollector(Property $propertyNode, DocBlock $propertyDocBlock): void + { + $propertyType = $propertyDocBlock->getAnnotationsOfType('var')[0] + ->getTypes()[0]; + + $propertyName = (string)$propertyNode->props[0]->name; + + $this->classPropertyCollector->addPropertyForClass($this->className, $propertyType, $propertyName); + } } diff --git a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyNodeVisitor.php b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyRector.php similarity index 88% rename from src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyNodeVisitor.php rename to src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyRector.php index 18384bd4c7f..45e323be49e 100644 --- a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyNodeVisitor.php +++ b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyRector.php @@ -13,7 +13,7 @@ use PhpParser\NodeVisitorAbstract; use Rector\Builder\Class_\ClassPropertyCollector; use Rector\Builder\Kernel\ServiceFromKernelResolver; use Rector\Builder\Naming\NameResolver; -use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorReconstructor\Source\LocalKernel; +use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorRector\Source\LocalKernel; /** * Converts all: @@ -22,7 +22,7 @@ use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorRecon * into: * $this->someService # where "someService" is type of the service */ -final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract +final class GetterToPropertyRector extends NodeVisitorAbstract { /** * @var string @@ -69,25 +69,10 @@ final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract return null; } - /** - * Return value semantics: - * * null - * => $node stays as-is - * * NodeTraverser::DONT_TRAVERSE_CHILDREN - * => Children of $node are not traversed. $node stays as-is - * * NodeTraverser::STOP_TRAVERSAL - * => Traversal is aborted. $node stays as-is - * * otherwise - * => $node is set to the return value. - * - * @return null|int|Node - */ - public function enterNode(Node $node) + public function enterNode(Node $node): ?Node { if ($this->isCandidate($node)) { - $this->reconstruct($node); - - return $node; + return $this->reconstruct($node); } return null; diff --git a/tests/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorRector/correct/correct.php.inc b/tests/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorRector/correct/correct.php.inc index 57ec4a63833..5154e980d9f 100644 --- a/tests/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorRector/correct/correct.php.inc +++ b/tests/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorRector/correct/correct.php.inc @@ -6,7 +6,6 @@ class ClassWithInjects * @var stdClass */ private $property; - /** * @var DateTimeInterface */ diff --git a/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Source/LocalKernel.php b/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorRector/Source/LocalKernel.php similarity index 95% rename from tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Source/LocalKernel.php rename to tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorRector/Source/LocalKernel.php index 0f812a061c8..bc404e115f2 100644 --- a/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Source/LocalKernel.php +++ b/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorRector/Source/LocalKernel.php @@ -1,6 +1,6 @@