Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add MigrateContentObjectRendererGetTypoLinkUrlRector #4319

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/v12/typo3-120.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,4 +170,5 @@
$rectorConfig->rule(MigrateFetchAllToFetchAllAssociativeRector::class);
$rectorConfig->rule(MigrateFetchToFetchAssociativeRector::class);
$rectorConfig->rule(MigrateBackendModuleRegistrationRector::class);
$rectorConfig->rule(MigrateContentObjectRendererGetTypoLinkUrlRector::class);
};
112 changes: 112 additions & 0 deletions rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\TYPO312\v0;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Deprecation-96641-LinkRelatedFunctionalityInContentObjectRenderer.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\MigrateContentObjectRendererGetTypoLinkUrlRector\MigrateContentObjectRendererGetTypoLinkUrlRectorTest
*/
final class MigrateContentObjectRendererGetTypoLinkUrlRector extends AbstractRector
{
/**
* @readonly
*/
private ValueResolver $valueResolver;

public function __construct(ValueResolver $valueResolver)
{
$this->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<class-string<Node>>
*/
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');
}
}
4 changes: 1 addition & 3 deletions stubs/TYPO3/CMS/Core/Utility/HttpUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand All @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion stubs/TYPO3/CMS/Frontend/ContentObject/ContentObjectRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\MigrateContentObjectRendererGetTypoLinkUrlRector\Fixture;

use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

$contentObjectRenderer = new ContentObjectRenderer();
$url = $contentObjectRenderer->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');

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\MigrateContentObjectRendererGetTypoLinkUrlRector\Fixture;

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']);
$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']);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\MigrateContentObjectRendererGetTypoLinkUrlRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class MigrateContentObjectRendererGetTypoLinkUrlRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\TYPO312\v0\MigrateContentObjectRendererGetTypoLinkUrlRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../../config/config_test.php');
$rectorConfig->rule(MigrateContentObjectRendererGetTypoLinkUrlRector::class);
};