From 5120451ee558d1ae8d9518392075c4d41928825d Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 20 Jul 2017 19:10:06 +0200 Subject: [PATCH] cleanup --- src/Application/FileProcessor.php | 1 - ...jectAnnotationToConstructorNodeVisitor.php | 57 ++++++++++--------- .../AddPropertiesToClassNodeVisitor.php | 14 ++--- src/Testing/Application/FileReconstructor.php | 15 ++--- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/Application/FileProcessor.php b/src/Application/FileProcessor.php index 0b5236bf86d..45e31f70d56 100644 --- a/src/Application/FileProcessor.php +++ b/src/Application/FileProcessor.php @@ -62,7 +62,6 @@ final class FileProcessor $newStmts = $this->nodeTraverser->traverse($oldStmts); - $this->codeStyledPrinter->printToFile($file, $newStmts, $oldStmts, $oldTokens); } diff --git a/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorNodeVisitor.php b/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorNodeVisitor.php index 845ee5d2f85..710419938b3 100644 --- a/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorNodeVisitor.php +++ b/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorNodeVisitor.php @@ -12,7 +12,11 @@ use Rector\Builder\ConstructorMethodBuilder; final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract { - const ANNOTATION_INJECT = 'inject'; + /** + * @var string + */ + private const ANNOTATION_INJECT = 'inject'; + /** * @var ConstructorMethodBuilder */ @@ -29,9 +33,31 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract } /** - * @param Class_ $classNode + * Called when entering a node. + * + * 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 Replacement node (or special return value) */ - public function reconstruct(Node $classNode): void + public function enterNode(Node $node) + { + if ($node instanceof Class_) { + $this->reconstruct($node); + return $node; + } + + return null; + } + + private function reconstruct(Class_ $classNode): void { foreach ($classNode->stmts as $classElementStatement) { if (! $classElementStatement instanceof Property) { @@ -76,29 +102,4 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract $propertyNode->setDocComment(new Doc($propertyDocBlock->getContent())); } - - /** - * Called when entering a node. - * - * 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 Replacement node (or special return value) - */ - public function enterNode(Node $node) - { - if ($node instanceof Class_) { - $this->reconstruct($node); - return $node; - } - - return null; - } } diff --git a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php index a0f2f2c329c..7283b167fd0 100644 --- a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php +++ b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php @@ -24,11 +24,6 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract */ private $propertyBuilder; - /** - * @var string - */ - private $className; - /** * @var ClassPropertyCollector */ @@ -52,17 +47,18 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract { foreach ($nodes as $node) { if ($node instanceof Class_) { - $this->className = (string) $node->name; - $this->reconstruct($node); + $this->reconstruct($node, (string) $node->name); + break; } } return $nodes; } - private function reconstruct(Class_ $classNode): void + private function reconstruct(Class_ $classNode, string $className): void { - $propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($this->className); + $propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className); + foreach ($propertiesForClass as $propertyType => $propertyName) { $this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName); $this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName); diff --git a/src/Testing/Application/FileReconstructor.php b/src/Testing/Application/FileReconstructor.php index b2924d8b517..f4ab6b6141b 100644 --- a/src/Testing/Application/FileReconstructor.php +++ b/src/Testing/Application/FileReconstructor.php @@ -20,18 +20,23 @@ final class FileReconstructor * @var CodeStyledPrinter */ private $codeStyledPrinter; + /** * @var Lexer */ - private $lexer; + /** * @var NodeTraverser */ private $nodeTraverser; - public function __construct(Parser $parser, CodeStyledPrinter $codeStyledPrinter, Lexer $lexer, NodeTraverser $nodeTraverser) - { + public function __construct( + Parser $parser, + CodeStyledPrinter $codeStyledPrinter, + Lexer $lexer, + NodeTraverser $nodeTraverser + ) { $this->parser = $parser; $this->codeStyledPrinter = $codeStyledPrinter; $this->lexer = $lexer; @@ -43,12 +48,8 @@ final class FileReconstructor { $fileContent = file_get_contents($file->getRealPath()); - /** @var Node[] $nodes */ $oldStmts = $this->parser->parse($fileContent); - - // keep format printer $oldTokens = $this->lexer->getTokens(); - $newStmts = $this->nodeTraverser->traverse($oldStmts); return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens);