From dcd32a170d46087c8dfe3d37378dee2638e800f3 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Fri, 4 Oct 2024 16:13:50 +0200 Subject: [PATCH] [BUGFIX] Add more cases to MigrateContentObjectRendererGetTypoLinkUrlRector --- ...tentObjectRendererGetTypoLinkUrlRector.php | 39 +++++++++++++++---- .../Fixture/fixture.php.inc | 20 ++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php b/rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php index bb0c79c8a..e54dcd333 100644 --- a/rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php +++ b/rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php @@ -5,8 +5,11 @@ namespace Ssch\TYPO3Rector\TYPO312\v0; use PhpParser\Node; +use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\Scalar\String_; use PHPStan\Type\ObjectType; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; @@ -65,19 +68,41 @@ public function refactor(Node $node): ?Node } // params - $parameter = $this->valueResolver->getValue($node->args[0]->value); - $arguments['parameter'] = $parameter; + $arguments['parameter'] = $node->args[0]; // 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)) { + if ($node->args[1]->value instanceof String_) { + $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; + } + } elseif ($node->args[1]->value instanceof Variable) { + $urlParameters = $this->valueResolver->getValue($node->args[1]->value); + if (is_string($urlParameters)) { + $arguments['additionalParams'] = $node->args[1]; + } elseif (is_array($urlParameters)) { + $staticCall = $this->nodeFactory->createStaticCall( + 'TYPO3\\CMS\\Core\\Utility\\HttpUtility', + 'buildQueryString', + [$node->args[1], $this->nodeFactory->createArg('&')] + ); + + $arguments['additionalParams'] = $staticCall; + } + } elseif ($node->args[1]->value instanceof Array_) { $staticCall = $this->nodeFactory->createStaticCall( 'TYPO3\\CMS\\Core\\Utility\\HttpUtility', 'buildQueryString', - [$this->nodeFactory->createArg($urlParameters), $this->nodeFactory->createArg('&')] + [$node->args[1], $this->nodeFactory->createArg('&')] ); $arguments['additionalParams'] = $staticCall; diff --git a/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/Fixture/fixture.php.inc b/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/Fixture/fixture.php.inc index afb1bf349..8726a44b8 100644 --- a/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/Fixture/fixture.php.inc +++ b/tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/Fixture/fixture.php.inc @@ -5,11 +5,21 @@ namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\MigrateContentObjectRendererGetTy use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; $contentObjectRenderer = new ContentObjectRenderer(); + $url = $contentObjectRenderer->getTypoLink_URL(12); $urlWithString = $contentObjectRenderer->getTypoLink_URL(12, '&foo=bar'); + +$parameters = '&foo=bar'; +$urlWithUrlParamsAsVariable = $contentObjectRenderer->getTypoLink_URL(12, $parameters); + +$parametersAsArray = ['foo' => 'bar']; +$urlWithUrlParamsAsArrayVariable = $contentObjectRenderer->getTypoLink_URL(12, $parametersAsArray); + $urlWithArray = $contentObjectRenderer->getTypoLink_URL(12, ['foo' => 'bar']); $urlWithArrayAndTarget = $contentObjectRenderer->getTypoLink_URL(12, ['foo' => 'bar'], '_blank'); +$pageId = 13; +$urlWithVariable = $contentObjectRenderer->getTypoLink_URL($pageId); ?> ----- createUrl(['parameter' => 12]); $urlWithString = $contentObjectRenderer->createUrl(['parameter' => 12, 'additionalParams' => '&foo=bar']); + +$parameters = '&foo=bar'; +$urlWithUrlParamsAsVariable = $contentObjectRenderer->createUrl(['parameter' => 12, 'additionalParams' => $parameters]); + +$parametersAsArray = ['foo' => 'bar']; +$urlWithUrlParamsAsArrayVariable = $contentObjectRenderer->createUrl(['parameter' => 12, 'additionalParams' => HttpUtility::buildQueryString($parametersAsArray, '&')]); + $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']); +$pageId = 13; +$urlWithVariable = $contentObjectRenderer->createUrl(['parameter' => $pageId]); ?>