From 53a47597742fef09bc1742808f511ad8cb876ff0 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Sun, 13 Nov 2022 15:55:01 +0100 Subject: [PATCH] TASK: Remove obsolete methods from TypeConverters --- ecs.php | 3 +- .../RemoveExtbaseTypeConverterNodeVisitor.php | 52 ++++++++++++++++ ...rExtbaseTypeConvertersAsServicesRector.php | 59 +++++++++++++++---- .../ExpectedMySecondSpecialTypeConverter.php | 13 ++++ .../ExpectedMySpecialTypeConverter.php | 13 ++++ .../Fixture/ExpectedServices.yaml | 2 +- ...baseTypeConvertersAsServicesRectorTest.php | 13 +++- .../Source/MySecondSpecialTypeConverter.php | 2 +- .../Source/MySpecialEntity.php | 11 ++++ 9 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 src/NodeVisitor/RemoveExtbaseTypeConverterNodeVisitor.php create mode 100644 tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Fixture/ExpectedMySecondSpecialTypeConverter.php create mode 100644 tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Fixture/ExpectedMySpecialTypeConverter.php create mode 100644 tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Source/MySpecialEntity.php diff --git a/ecs.php b/ecs.php index 8ab2cee8e..313e3789a 100644 --- a/ecs.php +++ b/ecs.php @@ -39,12 +39,13 @@ ]); $ecsConfig->skip([ + __DIR__ . '/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Fixture/ExpectedMySpecialTypeConverter.php', + __DIR__ . '/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Fixture/ExpectedMySecondSpecialTypeConverter.php', // on php 8.1, it adds space on &$variable FunctionTypehintSpaceFixer::class => [ __DIR__ . '/src/FileProcessor/Yaml/Form/Rector/EmailFinisherRector.php', __DIR__ . '/src/FileProcessor/Yaml/Form/Rector/TranslationFileRector.php', ], - __DIR__ . '/config/composer', __DIR__ . '/utils/generator/templates/src', AssignmentInConditionSniff::class, DeclareStrictTypesFixer::class => ['*/Fixture/*'], diff --git a/src/NodeVisitor/RemoveExtbaseTypeConverterNodeVisitor.php b/src/NodeVisitor/RemoveExtbaseTypeConverterNodeVisitor.php new file mode 100644 index 000000000..508a33a66 --- /dev/null +++ b/src/NodeVisitor/RemoveExtbaseTypeConverterNodeVisitor.php @@ -0,0 +1,52 @@ +nodeNameResolver = $nodeNameResolver; + } + + public function beforeTraverse(array $nodes): ?array + { + return $nodes; + } + + public function enterNode(Node $node): Node + { + return $node; + } + + public function leaveNode(Node $node) + { + if (! $node instanceof ClassMethod) { + return $node; + } + + if (! $this->nodeNameResolver->isNames( + $node->name, + ['getSupportedSourceTypes', 'getSupportedTargetType', 'getPriority'] + )) { + return $node; + } + + return NodeTraverser::REMOVE_NODE; + } + + public function afterTraverse(array $nodes): ?array + { + return $nodes; + } +} diff --git a/src/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector.php b/src/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector.php index 43d5a5b14..375209d38 100644 --- a/src/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector.php +++ b/src/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector.php @@ -7,13 +7,17 @@ use PhpParser\Node; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\NodeTraverser; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\ObjectType; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector; use Rector\Core\PhpParser\Parser\SimplePhpParser; +use Rector\Core\PhpParser\Printer\NodesWithFileDestinationPrinter; use Rector\Core\Rector\AbstractRector; use Rector\FileSystemRector\ValueObject\AddedFileWithContent; +use Rector\FileSystemRector\ValueObject\AddedFileWithNodes; use Ssch\TYPO3Rector\Helper\FilesFinder; +use Ssch\TYPO3Rector\NodeVisitor\RemoveExtbaseTypeConverterNodeVisitor; use Symfony\Component\Yaml\Yaml; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -42,16 +46,20 @@ final class RegisterExtbaseTypeConvertersAsServicesRector extends AbstractRector private RemovedAndAddedFilesCollector $removedAndAddedFilesCollector; + private NodesWithFileDestinationPrinter $nodesWithFileDestinationPrinter; + public function __construct( ReflectionProvider $reflectionProvider, SimplePhpParser $simplePhpParser, FilesFinder $filesFinder, - RemovedAndAddedFilesCollector $removedAndAddedFilesCollector + RemovedAndAddedFilesCollector $removedAndAddedFilesCollector, + NodesWithFileDestinationPrinter $nodesWithFileDestinationPrinter ) { $this->reflectionProvider = $reflectionProvider; $this->simplePhpParser = $simplePhpParser; $this->filesFinder = $filesFinder; $this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector; + $this->nodesWithFileDestinationPrinter = $nodesWithFileDestinationPrinter; } /** @@ -94,8 +102,7 @@ public function refactor(Node $node): ?Node } $classStatements = $this->simplePhpParser->parseFile($fileName); - - $this->nodeRemover->removeNode($node); + $originalClassStatements = $classStatements; $collectServiceTags = $this->collectServiceTags($classStatements); @@ -116,6 +123,9 @@ public function refactor(Node $node): ?Node $servicesYaml = new AddedFileWithContent($existingServicesYamlFilePath, $yamlConfigurationAsYaml); $this->removedAndAddedFilesCollector->addAddedFile($servicesYaml); + $this->nodeRemover->removeNode($node); + $this->removeClassMethodsFromTypeConverter($fileName, $originalClassStatements); + return null; } @@ -151,18 +161,11 @@ protected function collectServiceTags(array $classStatements): array ]; $this->traverseNodesWithCallable($classStatements, function (Node $node) use (&$collectServiceTags) { - if (! $node instanceof ClassMethod) { + if ($this->shouldSkipNodeOfTypeConverter($node)) { return null; } - if (! $this->nodeNameResolver->isNames( - $node->name, - ['getSupportedSourceTypes', 'getSupportedTargetType', 'getPriority'] - )) { - return null; - } - - if (null === $node->stmts) { + if (! $node instanceof ClassMethod || null === $node->stmts) { return null; } @@ -231,4 +234,36 @@ private function getYamlConfiguration(string $existingServicesYamlFilePath): arr return $yamlConfiguration; } + + private function shouldSkipNodeOfTypeConverter(Node $node): bool + { + if (! $node instanceof ClassMethod) { + return true; + } + + if (! $this->nodeNameResolver->isNames( + $node->name, + ['getSupportedSourceTypes', 'getSupportedTargetType', 'getPriority'] + )) { + return true; + } + + return false; + } + + /** + * @param Node\Stmt[] $classStatements + */ + private function removeClassMethodsFromTypeConverter(string $fileName, array $classStatements): void + { + $nodeTraverser = new NodeTraverser(); + $visitor = new RemoveExtbaseTypeConverterNodeVisitor($this->nodeNameResolver); + + $nodeTraverser->addVisitor($visitor); + $nodeTraverser->traverse($classStatements); + + $addedFileWithNodes = new AddedFileWithNodes($fileName, $classStatements); + $content = $this->nodesWithFileDestinationPrinter->printNodesWithFileDestination($addedFileWithNodes); + $this->removedAndAddedFilesCollector->addAddedFile(new AddedFileWithContent($fileName, $content)); + } } diff --git a/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Fixture/ExpectedMySecondSpecialTypeConverter.php b/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Fixture/ExpectedMySecondSpecialTypeConverter.php new file mode 100644 index 000000000..01b362e11 --- /dev/null +++ b/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Fixture/ExpectedMySecondSpecialTypeConverter.php @@ -0,0 +1,13 @@ +doTestFile($filePath); $addedFilesWithContent = $this->removedAndAddedFilesCollector->getAddedFilesWithContent(); + self::assertStringEqualsFile( - __DIR__ . '/Fixture/ExpectedServices.yaml', + __DIR__ . '/Fixture/ExpectedMySpecialTypeConverter.php', $addedFilesWithContent[1]->getFileContent() ); + + self::assertStringEqualsFile( + __DIR__ . '/Fixture/ExpectedServices.yaml', + $addedFilesWithContent[2]->getFileContent() + ); + + self::assertStringEqualsFile( + __DIR__ . '/Fixture/ExpectedMySecondSpecialTypeConverter.php', + $addedFilesWithContent[3]->getFileContent() + ); } /** diff --git a/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Source/MySecondSpecialTypeConverter.php b/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Source/MySecondSpecialTypeConverter.php index 09076cfee..dd51daa5c 100644 --- a/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Source/MySecondSpecialTypeConverter.php +++ b/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Source/MySecondSpecialTypeConverter.php @@ -15,7 +15,7 @@ public function getSupportedSourceTypes(): array public function getSupportedTargetType(): string { - return 'int'; + return MySpecialEntity::class; } public function getPriority(): int diff --git a/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Source/MySpecialEntity.php b/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Source/MySpecialEntity.php new file mode 100644 index 000000000..6561fe85c --- /dev/null +++ b/tests/Rector/v12/v0/typo3/RegisterExtbaseTypeConvertersAsServicesRector/Source/MySpecialEntity.php @@ -0,0 +1,11 @@ +