From 8f87dd58d623fbe7fa4d36346821f1768e011494 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Wed, 19 Jul 2017 00:42:50 +0200 Subject: [PATCH] cleanup --- src/Analyzer/ClassAnalyzer.php | 39 ------------------- src/Console/Command/ReconstructCommand.php | 3 +- ...jectAnnotationToConstructorNodeVisitor.php | 2 +- .../NamedServicesToConstructorNodeVisitor.php | 22 +---------- src/Testing/Application/FileReconstructor.php | 4 +- .../PHPUnit/AbstractReconstructorTestCase.php | 11 +++--- .../Test.php | 2 +- .../Test.php | 2 +- 8 files changed, 12 insertions(+), 73 deletions(-) delete mode 100644 src/Analyzer/ClassAnalyzer.php diff --git a/src/Analyzer/ClassAnalyzer.php b/src/Analyzer/ClassAnalyzer.php deleted file mode 100644 index caf219084f7..00000000000 --- a/src/Analyzer/ClassAnalyzer.php +++ /dev/null @@ -1,39 +0,0 @@ -extends instanceof Name) { - return Strings::endsWith($node->extends->getLast(), 'Controller'); - } - - return false; - } - - public function isContainerAwareClassNode(Node $node): bool - { - if (! $node instanceof Class_) { - return false; - } - - foreach ($node->implements as $nameNode) { - if (Strings::endsWith($nameNode->getLast(), 'ContainerAwareInterface')) { - return true; - } - } - - return false; - } -} diff --git a/src/Console/Command/ReconstructCommand.php b/src/Console/Command/ReconstructCommand.php index 5120e92f9fb..ad2a90f6764 100644 --- a/src/Console/Command/ReconstructCommand.php +++ b/src/Console/Command/ReconstructCommand.php @@ -57,10 +57,9 @@ final class ReconstructCommand extends Command } /** - * @param string[] $directories * @return SplFileInfo[] array */ - private function findPhpFilesInDirectories(array $directories): array + private function findPhpFilesInDirectories(string ...$directories): array { $finder = Finder::find('*.php') ->in($directories); diff --git a/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorNodeVisitor.php b/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorNodeVisitor.php index 65676dc015c..b890577a602 100644 --- a/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorNodeVisitor.php +++ b/src/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorNodeVisitor.php @@ -99,6 +99,6 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract return $node; } - return NodeTraverser::DONT_TRAVERSE_CHILDREN; + return null; } } diff --git a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructorNodeVisitor.php b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructorNodeVisitor.php index 5dc51ce09a4..a3468c742e7 100644 --- a/src/NodeVisitor/DependencyInjection/NamedServicesToConstructorNodeVisitor.php +++ b/src/NodeVisitor/DependencyInjection/NamedServicesToConstructorNodeVisitor.php @@ -13,7 +13,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; -use Rector\Analyzer\ClassAnalyzer; use Rector\Builder\ConstructorMethodBuilder; use Rector\Builder\Naming\NameResolver; use Rector\Builder\PropertyBuilder; @@ -38,36 +37,19 @@ final class NamedServicesToConstructorNodeVisitor extends NodeVisitorAbstract */ private $nameResolver; - /** - * @var ClassAnalyzer - */ - private $classAnalyzer; - public function __construct( ConstructorMethodBuilder $constructorMethodBuilder, PropertyBuilder $propertyBuilder, - NameResolver $nameResolver, - ClassAnalyzer $classAnalyzer + NameResolver $nameResolver ) { $this->constructorMethodBuilder = $constructorMethodBuilder; $this->propertyBuilder = $propertyBuilder; $this->nameResolver = $nameResolver; - $this->classAnalyzer = $classAnalyzer; } private function isCandidate(Node $node): bool { - // OR? Maybe listen on MethodCall... $this-> +get('...') - - if ($this->classAnalyzer->isControllerClassNode($node)) { - return true; - } - - if ($this->classAnalyzer->isContainerAwareClassNode($node)) { - return true; - } - - return false; + return $node instanceof Class_; } /** diff --git a/src/Testing/Application/FileReconstructor.php b/src/Testing/Application/FileReconstructor.php index cc831fa93cc..979367370fc 100644 --- a/src/Testing/Application/FileReconstructor.php +++ b/src/Testing/Application/FileReconstructor.php @@ -40,15 +40,13 @@ final class FileReconstructor } # ref: https://github.com/nikic/PHP-Parser/issues/344#issuecomment-298162516 - public function processFileWithReconstructor(SplFileInfo $file, NodeVisitor $nodeVisitor): string + public function processFileWithNodeVisitor(SplFileInfo $file, NodeVisitor $nodeVisitor): string { $fileContent = file_get_contents($file->getRealPath()); /** @var Node[] $nodes */ $oldStmts = $this->parser->parse($fileContent); - // before recontruct event? - // keep format printer $oldTokens = $this->lexer->getTokens(); diff --git a/src/Testing/PHPUnit/AbstractReconstructorTestCase.php b/src/Testing/PHPUnit/AbstractReconstructorTestCase.php index d0f93b85e8f..0e120f611ed 100644 --- a/src/Testing/PHPUnit/AbstractReconstructorTestCase.php +++ b/src/Testing/PHPUnit/AbstractReconstructorTestCase.php @@ -5,7 +5,6 @@ namespace Rector\Testing\PHPUnit; use PhpParser\NodeVisitor; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; -use Rector\Contract\Dispatcher\ReconstructorInterface; use Rector\DependencyInjection\ContainerFactory; use Rector\Testing\Application\FileReconstructor; use SplFileInfo; @@ -30,17 +29,17 @@ abstract class AbstractReconstructorTestCase extends TestCase protected function doTestFileMatchesExpectedContent(string $file, string $reconstructedFile): void { - $reconstructedFileContent = $this->fileReconstructor->processFileWithReconstructor( - new SplFileInfo($file), $this->getReconstructor() + $reconstructedFileContent = $this->fileReconstructor->processFileWithNodeVisitor( + new SplFileInfo($file), $this->getNodeVisitor() ); $this->assertStringEqualsFile($reconstructedFile, $reconstructedFileContent); } - abstract protected function getReconstructorClass(): string; + abstract protected function getNodeVisitorClass(): string; - private function getReconstructor(): NodeVisitor + private function getNodeVisitor(): NodeVisitor { - return $this->container->get($this->getReconstructorClass()); + return $this->container->get($this->getNodeVisitorClass()); } } diff --git a/tests/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorReconstructor/Test.php b/tests/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorReconstructor/Test.php index 49143c0ebfe..30d12ff9c64 100644 --- a/tests/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorReconstructor/Test.php +++ b/tests/NodeVisitor/DependencyInjection/InjectAnnotationToConstructorReconstructor/Test.php @@ -15,7 +15,7 @@ final class Test extends AbstractReconstructorTestCase ); } - protected function getReconstructorClass(): string + protected function getNodeVisitorClass(): string { return InjectAnnotationToConstructorNodeVisitor::class; } diff --git a/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Test.php b/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Test.php index f8aa4576116..59ba7e24a63 100644 --- a/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Test.php +++ b/tests/NodeVisitor/DependencyInjection/NamedServicesToConstructorReconstructor/Test.php @@ -15,7 +15,7 @@ final class Test extends AbstractReconstructorTestCase // $this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong3.php.inc', __DIR__ . '/correct/correct3.php.inc'); } - protected function getReconstructorClass(): string + protected function getNodeVisitorClass(): string { return NamedServicesToConstructorNodeVisitor::class; }