diff --git a/config/v12/typo3-120.php b/config/v12/typo3-120.php index 580d9f87c..2011f2c40 100644 --- a/config/v12/typo3-120.php +++ b/config/v12/typo3-120.php @@ -10,6 +10,7 @@ use Ssch\TYPO3Rector\TYPO312\v0\ChangeExtbaseValidatorsRector; use Ssch\TYPO3Rector\TYPO312\v0\ImplementSiteLanguageAwareInterfaceRector; use Ssch\TYPO3Rector\TYPO312\v0\MigrateBackendModuleRegistrationRector; +use Ssch\TYPO3Rector\TYPO312\v0\MigrateContentObjectRendererGetTypoLinkUrlRector; use Ssch\TYPO3Rector\TYPO312\v0\MigrateContentObjectRendererLastTypoLinkPropertiesRector; use Ssch\TYPO3Rector\TYPO312\v0\MigrateFetchAllToFetchAllAssociativeRector; use Ssch\TYPO3Rector\TYPO312\v0\MigrateFetchColumnToFetchOneRector; @@ -169,4 +170,5 @@ $rectorConfig->rule(MigrateFetchAllToFetchAllAssociativeRector::class); $rectorConfig->rule(MigrateFetchToFetchAssociativeRector::class); $rectorConfig->rule(MigrateBackendModuleRegistrationRector::class); + $rectorConfig->rule(MigrateContentObjectRendererGetTypoLinkUrlRector::class); }; diff --git a/rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php b/rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php new file mode 100644 index 000000000..bb0c79c8a --- /dev/null +++ b/rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php @@ -0,0 +1,112 @@ +valueResolver = $valueResolver; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Migrate ContentObjectRenderer->getTypoLink_URL to ContentObjectRenderer->createUrl', + [ + new CodeSample( + <<<'CODE_SAMPLE' +$contentObjectRenderer->typoLink_URL(12); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$contentObjectRenderer->createUrl(['parameter' => 12]); +CODE_SAMPLE + ), + + ]); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if ($this->shouldSkip($node)) { + return null; + } + + // params + $parameter = $this->valueResolver->getValue($node->args[0]->value); + $arguments['parameter'] = $parameter; + + // urlParameters + if (isset($node->args[1])) { + $urlParameters = $this->valueResolver->getValue($node->args[1]->value); + if (is_string($urlParameters)) { + $arguments['additionalParams'] = $urlParameters; + } elseif (is_array($urlParameters)) { + $staticCall = $this->nodeFactory->createStaticCall( + 'TYPO3\\CMS\\Core\\Utility\\HttpUtility', + 'buildQueryString', + [$this->nodeFactory->createArg($urlParameters), $this->nodeFactory->createArg('&')] + ); + + $arguments['additionalParams'] = $staticCall; + } + } + + // Target + if (isset($node->args[2])) { + $target = $this->valueResolver->getValue($node->args[2]->value); + $arguments['target'] = $target; + $arguments['extTarget'] = $target; + $arguments['fileTarget'] = $target; + } + + $node->name = new Identifier('createUrl'); + $node->args = $this->nodeFactory->createArgs([$this->nodeFactory->createArray($arguments)]); + + return $node; + } + + private function shouldSkip(MethodCall $node): bool + { + if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( + $node, + new ObjectType('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer') + )) { + return true; + } + + return ! $this->nodeNameResolver->isName($node->name, 'getTypoLink_URL'); + } +} diff --git a/stubs/TYPO3/CMS/Core/Utility/HttpUtility.php b/stubs/TYPO3/CMS/Core/Utility/HttpUtility.php index 364b69666..7807e1d5f 100644 --- a/stubs/TYPO3/CMS/Core/Utility/HttpUtility.php +++ b/stubs/TYPO3/CMS/Core/Utility/HttpUtility.php @@ -7,14 +7,13 @@ class HttpUtility { - const HTTP_STATUS_400 = 'HTTP/1.1 400 Bad Request'; public const HTTP_STATUS_303 = 'HTTP/1.1 303 See Other'; /** * @return string */ - public static function buildQueryString(array $queryParams) + public static function buildQueryString(array $parameters, string $prependCharacter = '', bool $skipEmptyParameters = false) { return ''; } @@ -25,7 +24,6 @@ public static function buildQueryString(array $queryParams) */ public static function setResponseCode($httpStatus) { - } public static function redirect($url, $httpStatus = self::HTTP_STATUS_303) diff --git a/stubs/TYPO3/CMS/Frontend/ContentObject/ContentObjectRenderer.php b/stubs/TYPO3/CMS/Frontend/ContentObject/ContentObjectRenderer.php index c9113e090..1c53e3457 100644 --- a/stubs/TYPO3/CMS/Frontend/ContentObject/ContentObjectRenderer.php +++ b/stubs/TYPO3/CMS/Frontend/ContentObject/ContentObjectRenderer.php @@ -139,11 +139,25 @@ public function getTypoScriptFrontendController() public function start($data, $table = '', ?ServerRequestInterface $request = null): void { - } public function setRequest(ServerRequestInterface $request): void { + } + + /** + * @param string $params + * @param array|string $urlParameters + * @param string $target + * @return string + */ + public function getTypoLink_URL(string $params, $urlParameters = [], string $target = ''): string + { + return ''; + } + public function createUrl(array $conf): string + { + return ''; } } diff --git a/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/Fixture/fixture.php.inc b/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/Fixture/fixture.php.inc new file mode 100644 index 000000000..afb1bf349 --- /dev/null +++ b/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/Fixture/fixture.php.inc @@ -0,0 +1,28 @@ +getTypoLink_URL(12); +$urlWithString = $contentObjectRenderer->getTypoLink_URL(12, '&foo=bar'); +$urlWithArray = $contentObjectRenderer->getTypoLink_URL(12, ['foo' => 'bar']); +$urlWithArrayAndTarget = $contentObjectRenderer->getTypoLink_URL(12, ['foo' => 'bar'], '_blank'); + +?> +----- +createUrl(['parameter' => 12]); +$urlWithString = $contentObjectRenderer->createUrl(['parameter' => 12, 'additionalParams' => '&foo=bar']); +$urlWithArray = $contentObjectRenderer->createUrl(['parameter' => 12, 'additionalParams' => HttpUtility::buildQueryString(['foo' => 'bar'], '&')]); +$urlWithArrayAndTarget = $contentObjectRenderer->createUrl(['parameter' => 12, 'additionalParams' => HttpUtility::buildQueryString(['foo' => 'bar'], '&'), 'target' => '_blank', 'extTarget' => '_blank', 'fileTarget' => '_blank']); + +?> diff --git a/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/MigrateContentObjectRendererGetTypoLinkUrlRectorTest.php b/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/MigrateContentObjectRendererGetTypoLinkUrlRectorTest.php new file mode 100644 index 000000000..a1347d4f0 --- /dev/null +++ b/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/MigrateContentObjectRendererGetTypoLinkUrlRectorTest.php @@ -0,0 +1,32 @@ +doTestFile($filePath); + } + + /** + * @return Iterator> + */ + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/config/configured_rule.php b/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/config/configured_rule.php new file mode 100644 index 000000000..ece6ac799 --- /dev/null +++ b/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/config/configured_rule.php @@ -0,0 +1,11 @@ +import(__DIR__ . '/../../../../../../config/config_test.php'); + $rectorConfig->rule(MigrateContentObjectRendererGetTypoLinkUrlRector::class); +};