Skip to content

Commit

Permalink
TASK: Remove obsolete methods from TypeConverters
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Nov 13, 2022
1 parent 604c6dc commit 53a4759
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 16 deletions.
3 changes: 2 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/*'],
Expand Down
52 changes: 52 additions & 0 deletions src/NodeVisitor/RemoveExtbaseTypeConverterNodeVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use Rector\NodeNameResolver\NodeNameResolver;

final class RemoveExtbaseTypeConverterNodeVisitor implements NodeVisitor
{
private NodeNameResolver $nodeNameResolver;

public function __construct(NodeNameResolver $nodeNameResolver)
{
$this->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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);
namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\typo3\RegisterExtbaseTypeConvertersAsServicesRector\Source;

use TYPO3\CMS\Extbase\Property\TypeConverterInterface;
final class MySecondSpecialTypeConverter implements TypeConverterInterface
{
public function canConvertFrom($source, string $targetType): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);
namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\typo3\RegisterExtbaseTypeConvertersAsServicesRector\Source;

use TYPO3\CMS\Extbase\Property\TypeConverterInterface;
final class MySpecialTypeConverter implements TypeConverterInterface
{
public function canConvertFrom($source, string $targetType): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ services:
tags:
name: extbase.type_converter
sources: array
target: int
target: MySpecialEntity
priority: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ public function test(string $filePath): void
$this->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()
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function getSupportedSourceTypes(): array

public function getSupportedTargetType(): string
{
return 'int';
return MySpecialEntity::class;
}

public function getPriority(): int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\typo3\RegisterExtbaseTypeConvertersAsServicesRector\Source;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

final class MySpecialEntity extends AbstractEntity
{
}

0 comments on commit 53a4759

Please sign in to comment.