-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add MigrateContentObjectRendererGetTypoLinkUrlRector
Resolves: #2934
- Loading branch information
1 parent
da3f5ea
commit 869052f
Showing
7 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
rules/TYPO312/v0/MigrateContentObjectRendererGetTypoLinkUrlRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...jectRendererGetTypoLinkUrlRector/MigrateContentObjectRendererGetTypoLinkUrlRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...Rector/v12/v0/MigrateContentObjectRendererGetTypoLinkUrlRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |