From 09c13a11fa5fe8c214ce473c87612e86d0eb97a2 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 20 Jul 2017 18:58:34 +0200 Subject: [PATCH] [NamedServicesToContrutor] make tests pass --- src/Application/FileProcessor.php | 24 ++++++++++++------- src/Builder/Class_/ClassPropertyCollector.php | 4 ++-- .../AddPropertiesToClassNodeVisitor.php | 8 +++++-- .../GetterToPropertyNodeVisitor.php | 3 ++- src/Parser/LexerFactory.php | 4 ++++ src/Printer/CodeStyledPrinter.php | 10 ++++---- src/Testing/Application/FileReconstructor.php | 2 +- .../Test.php | 17 ++++++++++--- .../correct/correct3.php.inc | 1 + 9 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/Application/FileProcessor.php b/src/Application/FileProcessor.php index 11c0d497cc0..0b5236bf86d 100644 --- a/src/Application/FileProcessor.php +++ b/src/Application/FileProcessor.php @@ -2,6 +2,7 @@ namespace Rector\Application; +use PhpParser\Lexer; use PhpParser\NodeTraverser; use PhpParser\Parser; use Rector\Printer\CodeStyledPrinter; @@ -24,11 +25,17 @@ final class FileProcessor */ private $nodeTraverser; - public function __construct(Parser $parser, CodeStyledPrinter $codeStyledPrinter, NodeTraverser $nodeTraverser) + /** + * @var Lexer + */ + private $lexer; + + public function __construct(Parser $parser, CodeStyledPrinter $codeStyledPrinter, Lexer $lexer, NodeTraverser $nodeTraverser) { $this->parser = $parser; $this->codeStyledPrinter = $codeStyledPrinter; $this->nodeTraverser = $nodeTraverser; + $this->lexer = $lexer; } /** @@ -44,18 +51,19 @@ final class FileProcessor public function processFile(SplFileInfo $file): void { $fileContent = file_get_contents($file->getRealPath()); - $nodes = $this->parser->parse($fileContent); - if ($nodes === null) { + $oldStmts = $this->parser->parse($fileContent); + if ($oldStmts === null) { return; } - $originalNodes = $this->cloneArrayOfObjects($nodes); + $oldStmts = $this->cloneArrayOfObjects($oldStmts); + + $oldTokens = $this->lexer->getTokens(); + + $newStmts = $this->nodeTraverser->traverse($oldStmts); - $this->nodeTraverser->traverse($nodes); - - - $this->codeStyledPrinter->printToFile($file, $originalNodes, $nodes); + $this->codeStyledPrinter->printToFile($file, $newStmts, $oldStmts, $oldTokens); } /** diff --git a/src/Builder/Class_/ClassPropertyCollector.php b/src/Builder/Class_/ClassPropertyCollector.php index 4b0255ac111..374a9d03ef9 100644 --- a/src/Builder/Class_/ClassPropertyCollector.php +++ b/src/Builder/Class_/ClassPropertyCollector.php @@ -1,6 +1,6 @@ classProperties[$class] ?: []; + return $this->classProperties[$class] ?? []; } } diff --git a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php index db33ea6ab06..a0f2f2c329c 100644 --- a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php +++ b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/AddPropertiesToClassNodeVisitor.php @@ -2,11 +2,12 @@ namespace Rector\NodeVisitor\DependencyInjection\NamedServicesToConstructor; +use PhpParser\Node; use PhpParser\Node\Stmt\Class_; use PhpParser\NodeVisitorAbstract; +use Rector\Builder\Class_\ClassPropertyCollector; use Rector\Builder\ConstructorMethodBuilder; use Rector\Builder\PropertyBuilder; -use Rector\Buillder\Class_\ClassPropertyCollector; /** * Add new propertis to class and to contructor. @@ -43,6 +44,10 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract $this->newClassPropertyCollector = $newClassPropertyCollector; } + /** + * @param Node[] $nodes + * @return Node[] + */ public function afterTraverse(array $nodes): array { foreach ($nodes as $node) { @@ -58,7 +63,6 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract private function reconstruct(Class_ $classNode): void { $propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($this->className); - foreach ($propertiesForClass as $propertyType => $propertyName) { $this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName); $this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName); diff --git a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyNodeVisitor.php b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyNodeVisitor.php index bd0dfeb30ff..d967af595fd 100644 --- a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyNodeVisitor.php +++ b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructor/GetterToPropertyNodeVisitor.php @@ -11,7 +11,7 @@ use PhpParser\Node\Scalar\String_; use PhpParser\NodeVisitorAbstract; use Rector\Builder\Kernel\ServiceFromKernelResolver; use Rector\Builder\Naming\NameResolver; -use Rector\Buillder\Class_\ClassPropertyCollector; +use Rector\Builder\Class_\ClassPropertyCollector; use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorReconstructor\Source\LocalKernel; /** @@ -85,6 +85,7 @@ final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract { if ($this->isCandidate($node)) { $this->reconstruct($node); + return $node; } return null; diff --git a/src/Parser/LexerFactory.php b/src/Parser/LexerFactory.php index e9e7f169262..f6906f3c51e 100644 --- a/src/Parser/LexerFactory.php +++ b/src/Parser/LexerFactory.php @@ -5,6 +5,10 @@ namespace Rector\Parser; use PhpParser\Lexer; use PhpParser\Lexer\Emulative; +/** + * This Lexer allows Format-perserving AST Transformations + * @see https://github.com/nikic/PHP-Parser/issues/344#issuecomment-298162516 + */ final class LexerFactory { public function create(): Lexer diff --git a/src/Printer/CodeStyledPrinter.php b/src/Printer/CodeStyledPrinter.php index 8d3f85c3f45..08ea5addb20 100644 --- a/src/Printer/CodeStyledPrinter.php +++ b/src/Printer/CodeStyledPrinter.php @@ -17,18 +17,18 @@ final class CodeStyledPrinter $this->prettyPrinter = $prettyPrinter; } - public function printToFile(SplFileInfo $file, array $originalNodes, array $newNodes): void + public function printToFile(SplFileInfo $file, array $newStmts, array $oldStmts, array $oldTokens): void { - if ($originalNodes === $newNodes) { + if ($oldStmts === $newStmts) { return; } - file_put_contents($file->getRealPath(), $this->printToString($newNodes)); + file_put_contents($file->getRealPath(), $this->printToString($newStmts, $oldStmts, $oldTokens)); // @todo: run ecs with minimal set to code look nice } - public function printToString(array $oldStmts, array $newStmts, array $oldTokens): string + public function printToString(array $newStmts, array $oldStmts, array $oldTokens): string { - return $this->prettyPrinter->printFormatPreserving($oldStmts, $newStmts, $oldTokens); + return $this->prettyPrinter->printFormatPreserving($newStmts, $oldStmts, $oldTokens); } } diff --git a/src/Testing/Application/FileReconstructor.php b/src/Testing/Application/FileReconstructor.php index 9f5cd1a9e7a..b2924d8b517 100644 --- a/src/Testing/Application/FileReconstructor.php +++ b/src/Testing/Application/FileReconstructor.php @@ -51,6 +51,6 @@ final class FileReconstructor $newStmts = $this->nodeTraverser->traverse($oldStmts); - return $this->codeStyledPrinter->printToString($oldStmts, $newStmts, $oldTokens); + return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens); } } diff --git a/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Test.php b/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Test.php index 361c063516e..612898c60ac 100644 --- a/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Test.php +++ b/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Test.php @@ -8,8 +8,19 @@ final class Test extends AbstractReconstructorTestCase { public function test(): void { - $this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong.php.inc', __DIR__ . '/correct/correct.php.inc'); - $this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong2.php.inc', __DIR__ . '/correct/correct2.php.inc'); -// $this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong3.php.inc', __DIR__ . '/correct/correct3.php.inc'); + $this->doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong.php.inc', + __DIR__ . '/correct/correct.php.inc' + ); + + $this->doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong2.php.inc', + __DIR__ . '/correct/correct2.php.inc' + ); + + $this->doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong3.php.inc', + __DIR__ . '/correct/correct3.php.inc' + ); } } diff --git a/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/correct/correct3.php.inc b/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/correct/correct3.php.inc index bc8f70e63c6..6f59335fa51 100644 --- a/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/correct/correct3.php.inc +++ b/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/correct/correct3.php.inc @@ -1,4 +1,5 @@