From 8e35fd5e682f3bc3c3381fdc77098ff19eb08974 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Tue, 9 Jan 2024 13:38:50 +0100 Subject: [PATCH] TASK: Return nodes for RefactorDeprecatedConcatenateMethodsPageRendererRector (#3998) * TASK: Remove NodesToAddCollector. Not used * TASK: Return nodes RefactorDeprecatedConcatenateMethodsPageRendererRector --- .../Conditions/GlobalVarConditionMatcher.php | 20 +++--- ...eParsingInTsfeAndTemplateServiceRector.php | 9 +-- ...edConcatenateMethodsPageRendererRector.php | 69 +++++++++++-------- ...tor_deprecated_concatenate_methods.php.inc | 1 - 4 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/FileProcessor/TypoScript/Conditions/GlobalVarConditionMatcher.php b/src/FileProcessor/TypoScript/Conditions/GlobalVarConditionMatcher.php index ef9130cbe..03f0831c2 100644 --- a/src/FileProcessor/TypoScript/Conditions/GlobalVarConditionMatcher.php +++ b/src/FileProcessor/TypoScript/Conditions/GlobalVarConditionMatcher.php @@ -53,34 +53,34 @@ public function change(string $condition): ?string } switch ($type) { - case 'TSFE' : + case 'TSFE': $conditions[$key][] = $this->refactorTsfe($property, $operator, $value); break; - case 'GP' : + case 'GP': $conditions[$key][] = $this->refactorGetPost($property, $operator, $value); break; - case 'LIT' : + case 'LIT': $conditions[$key][] = sprintf('"%s" %s "%s"', $value, self::OPERATOR_MAPPING[$operator], $property); break; - case 'ENV' : + case 'ENV': $conditions[$key][] = $this->createEnvCondition($property, $operator, $value); break; - case 'IENV' : + case 'IENV': $conditions[$key][] = $this->createIndependentCondition($property, $operator, $value); break; - case 'BE_USER' : + case 'BE_USER': $conditions[$key][] = $this->createBackendUserCondition($property, $operator, $value); break; - case '_GET' : + case '_GET': $conditions[$key][] = $this->refactorGet($property, $operator, $value); break; - case 'GPmerged' : + case 'GPmerged': $conditions[$key][] = $this->refactorGetPost($property, $operator, $value); break; - case '_POST' : + case '_POST': $conditions[$key][] = $this->refactorPost($property, $operator, $value); break; - default : + default: $conditions[$key][] = $condition; break; } diff --git a/src/Rector/v10/v0/ForceTemplateParsingInTsfeAndTemplateServiceRector.php b/src/Rector/v10/v0/ForceTemplateParsingInTsfeAndTemplateServiceRector.php index 773f31d39..ac0f6bff2 100644 --- a/src/Rector/v10/v0/ForceTemplateParsingInTsfeAndTemplateServiceRector.php +++ b/src/Rector/v10/v0/ForceTemplateParsingInTsfeAndTemplateServiceRector.php @@ -12,7 +12,6 @@ use PhpParser\Node\Name; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; -use Rector\PostRector\Collector\NodesToAddCollector; use Ssch\TYPO3Rector\Helper\Typo3NodeResolver; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -33,20 +32,14 @@ final class ForceTemplateParsingInTsfeAndTemplateServiceRector extends AbstractR */ private const TYPOSCRIPT = 'typoscript'; - /** - * @readonly - */ - public NodesToAddCollector $nodesToAddCollector; - /** * @readonly */ private Typo3NodeResolver $typo3NodeResolver; - public function __construct(Typo3NodeResolver $typo3NodeResolver, NodesToAddCollector $nodesToAddCollector) + public function __construct(Typo3NodeResolver $typo3NodeResolver) { $this->typo3NodeResolver = $typo3NodeResolver; - $this->nodesToAddCollector = $nodesToAddCollector; } /** diff --git a/src/Rector/v9/v4/RefactorDeprecatedConcatenateMethodsPageRendererRector.php b/src/Rector/v9/v4/RefactorDeprecatedConcatenateMethodsPageRendererRector.php index 8e9d14611..ba3d79cd8 100644 --- a/src/Rector/v9/v4/RefactorDeprecatedConcatenateMethodsPageRendererRector.php +++ b/src/Rector/v9/v4/RefactorDeprecatedConcatenateMethodsPageRendererRector.php @@ -9,9 +9,9 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Identifier; +use PhpParser\Node\Stmt\Expression; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; -use Rector\PostRector\Collector\NodesToAddCollector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -21,46 +21,44 @@ */ final class RefactorDeprecatedConcatenateMethodsPageRendererRector extends AbstractRector { - /** - * @readonly - */ - public NodesToAddCollector $nodesToAddCollector; - - public function __construct(NodesToAddCollector $nodesToAddCollector) - { - $this->nodesToAddCollector = $nodesToAddCollector; - } - /** * @return array> */ public function getNodeTypes(): array { - return [MethodCall::class]; + return [MethodCall::class, Expression::class]; } /** - * @param MethodCall $node + * @param MethodCall|Expression $node + * @return Node[]|null|Node */ - public function refactor(Node $node): ?Node + public function refactor(Node $node) { - if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( - $node, - new ObjectType('TYPO3\CMS\Core\Page\PageRenderer') - )) { + if ($node instanceof MethodCall) { + if ($this->shouldSkip($node)) { + return null; + } + + if ($this->isName($node->name, 'getConcatenateFiles')) { + return $this->createArrayMergeCall($node); + } + return null; } - if ($this->isName($node->name, 'getConcatenateFiles')) { - return $this->createArrayMergeCall($node); + $methodCall = $node->expr; + + if (! $methodCall instanceof MethodCall) { + return null; } - if ($this->isName($node->name, 'enableConcatenateFiles')) { - return $this->splitMethodCall($node, 'enableConcatenateJavascript', 'enableConcatenateCss'); + if ($this->isName($methodCall->name, 'enableConcatenateFiles')) { + return $this->splitMethodCall($methodCall, 'enableConcatenateJavascript', 'enableConcatenateCss'); } - if ($this->isName($node->name, 'disableConcatenateFiles')) { - return $this->splitMethodCall($node, 'disableConcatenateJavascript', 'disableConcatenateCss'); + if ($this->isName($methodCall->name, 'disableConcatenateFiles')) { + return $this->splitMethodCall($methodCall, 'disableConcatenateJavascript', 'disableConcatenateCss'); } return null; @@ -93,14 +91,31 @@ private function createArrayMergeCall(MethodCall $methodCall): FuncCall return $this->nodeFactory->createFuncCall('array_merge', [new Arg($node1), new Arg($node2)]); } - private function splitMethodCall(MethodCall $methodCall, string $firstMethod, string $secondMethod): MethodCall + /** + * @return Node[] + */ + private function splitMethodCall(MethodCall $methodCall, string $firstMethod, string $secondMethod): array { $methodCall->name = new Identifier($firstMethod); $node1 = clone $methodCall; $node1->name = new Identifier($secondMethod); - $this->nodesToAddCollector->addNodeBeforeNode($node1, $methodCall); - return $methodCall; + return [new Expression($node1), new Expression($methodCall)]; + } + + private function shouldSkip(MethodCall $node): bool + { + if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( + $node, + new ObjectType('TYPO3\CMS\Core\Page\PageRenderer') + )) { + return true; + } + + return ! $this->isNames( + $node->name, + ['getConcatenateFiles', 'enableConcatenateFiles', 'disableConcatenateFiles'] + ); } } diff --git a/tests/Rector/v9/v4/RefactorDeprecatedConcatenateMethodsPageRendererRector/Fixture/refactor_deprecated_concatenate_methods.php.inc b/tests/Rector/v9/v4/RefactorDeprecatedConcatenateMethodsPageRendererRector/Fixture/refactor_deprecated_concatenate_methods.php.inc index 0327fe2ee..0723d6954 100644 --- a/tests/Rector/v9/v4/RefactorDeprecatedConcatenateMethodsPageRendererRector/Fixture/refactor_deprecated_concatenate_methods.php.inc +++ b/tests/Rector/v9/v4/RefactorDeprecatedConcatenateMethodsPageRendererRector/Fixture/refactor_deprecated_concatenate_methods.php.inc @@ -51,7 +51,6 @@ final class ClassWithOldConcatenateMethodsPageRenderer $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); $files = array_merge($pageRenderer->getConcatenateCss(), $pageRenderer->getConcatenateJavascript()); $this->pageRenderer->enableConcatenateCss(); - $this->pageRenderer->enableConcatenateJavascript(); $this->pageRenderer->disableConcatenateCss(); $this->pageRenderer->disableConcatenateJavascript();