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

[FEAT] Replace TSFE->ATagParams calls #2723

Merged
merged 2 commits into from
Nov 4, 2021
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
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,14 @@
<?php

declare(strict_types=1);

use Ssch\TYPO3Rector\Rector\v11\v5\ReplaceTSFEATagParamsCallOnGlobalsRector;

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(ReplaceTSFEATagParamsCallOnGlobalsRector::class);
};