-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Replace TSFE->ATagParams calls
Relates: #2670
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 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
110 changes: 110 additions & 0 deletions
110
src/Rector/v11/v5/ReplaceTSFEATagParamsCallOnGlobalsRector.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,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v11\v5; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\BinaryOp\Coalesce; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Scalar\String_; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/11.5/Deprecation-95219-TypoScriptFrontendController-ATagParams.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v5\ReplaceTSFEATagParamsCallOnGlobalsRector\ReplaceTSFEATagParamsCallOnGlobalsRectorTest | ||
*/ | ||
final class ReplaceTSFEATagParamsCallOnGlobalsRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private Typo3NodeResolver $typo3NodeResolver | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [PropertyFetch::class]; | ||
} | ||
|
||
/** | ||
* @param PropertyFetch $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
$propertyFetch = $this->nodeFactory->createPropertyFetch( | ||
new ArrayDimFetch( | ||
new Variable('GLOBALS'), | ||
new String_(Typo3NodeResolver::TYPO_SCRIPT_FRONTEND_CONTROLLER) | ||
), | ||
'config', | ||
); | ||
|
||
return new Coalesce( | ||
new ArrayDimFetch( | ||
new ArrayDimFetch($propertyFetch, new String_('config'),), | ||
new String_('ATagParams'), | ||
), | ||
new String_('') | ||
); | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Replaces all direct calls to $GLOBALS[\'TSFE\']->ATagParams.', | ||
[new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$foo = $GLOBALS['TSFE']->ATagParams; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$foo = $GLOBALS['TSFE']->config['config']['ATagParams'] ?? ''; | ||
CODE_SAMPLE | ||
)] | ||
); | ||
} | ||
|
||
private function shouldSkip(PropertyFetch $node): bool | ||
{ | ||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); | ||
|
||
// Check if we have an assigment to the property, if so do not change it | ||
if ($parentNode instanceof Assign && $parentNode->var instanceof PropertyFetch) { | ||
return true; | ||
} | ||
|
||
if (! $this->isName($node->name, 'ATagParams')) { | ||
return true; | ||
} | ||
|
||
if ($this->isObjectType( | ||
$node->var, | ||
new ObjectType('TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController') | ||
)) { | ||
return false; | ||
} | ||
|
||
return ! $this->typo3NodeResolver->isPropertyFetchOnAnyPropertyOfGlobals( | ||
$node, | ||
Typo3NodeResolver::TYPO_SCRIPT_FRONTEND_CONTROLLER | ||
); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
tests/Rector/v11/v5/ReplaceTSFEATagParamsCallOnGlobalsRector/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,15 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\ReplaceTSFEATagParamsCallOnGlobalsRector\Fixture; | ||
|
||
$foo = $GLOBALS['TSFE']->ATagParams; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\ReplaceTSFEATagParamsCallOnGlobalsRector\Fixture; | ||
|
||
$foo = $GLOBALS['TSFE']->config['config']['ATagParams'] ?? ''; | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...ReplaceTSFEATagParamsCallOnGlobalsRector/ReplaceTSFEATagParamsCallOnGlobalsRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\ReplaceTSFEATagParamsCallOnGlobalsRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ReplaceTSFEATagParamsCallOnGlobalsRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/v11/v5/ReplaceTSFEATagParamsCallOnGlobalsRector/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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$containerConfigurator->import(__DIR__ . '/../../../../../../config/config_test.php'); | ||
|
||
$services = $containerConfigurator->services(); | ||
$services->set(\Ssch\TYPO3Rector\Rector\v11\v5\ReplaceTSFEATagParamsCallOnGlobalsRector::class); | ||
}; |