Skip to content

Commit

Permalink
[FEAT] Replace TSFE->ATagParams calls
Browse files Browse the repository at this point in the history
Relates: #2670
  • Loading branch information
helsner committed Nov 3, 2021
1 parent b377f67 commit 97e6f9d
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/v11/typo3-115.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
],
]]);
$services->set(\Ssch\TYPO3Rector\Rector\v11\v5\RemoveDefaultInternalTypeDBRector::class);
$services->set(\Ssch\TYPO3Rector\Rector\v11\v5\ReplaceTSFEATagParamsCallOnGlobalsRector::class);
};
110 changes: 110 additions & 0 deletions src/Rector/v11/v5/ReplaceTSFEATagParamsCallOnGlobalsRector.php
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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,8 @@ public function getQueryArguments($conf, $overruleQueryArguments = [], $forceOve
{

}

public function getATagParams($conf, $addGlobal = 1)
{
}
}
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'] ?? '';

?>
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';
}
}
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);
};

0 comments on commit 97e6f9d

Please sign in to comment.