Skip to content

Commit

Permalink
[BUGFIX] Add more cases to MigrateContentObjectRendererGetTypoLinkUrl…
Browse files Browse the repository at this point in the history
…Rector
  • Loading branch information
simonschaufi committed Oct 4, 2024
1 parent 2fdea7c commit dcd32a1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
?>
-----
<?php
Expand All @@ -20,9 +30,19 @@ use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

$contentObjectRenderer = new ContentObjectRenderer();

$url = $contentObjectRenderer->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]);
?>

0 comments on commit dcd32a1

Please sign in to comment.