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

[TASK] Refactor BackendUtility::getPagesTSconfig where possible #1621

Merged
merged 1 commit into from
Nov 20, 2020
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
3 changes: 3 additions & 0 deletions config/v9/typo3-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Ssch\TYPO3Rector\Rector\v9\v0\InjectAnnotationRector;
use Ssch\TYPO3Rector\Rector\v9\v0\MetaTagManagementRector;
use Ssch\TYPO3Rector\Rector\v9\v0\MoveRenderArgumentsToInitializeArgumentsMethodRector;
use Ssch\TYPO3Rector\Rector\v9\v0\RefactorBackendUtilityGetPagesTSconfigRector;
use Ssch\TYPO3Rector\Rector\v9\v0\RefactorDeprecationLogRector;
use Ssch\TYPO3Rector\Rector\v9\v0\RefactorMethodsFromExtensionManagementUtilityRector;
use Ssch\TYPO3Rector\Rector\v9\v0\RemoveMethodInitTCARector;
Expand Down Expand Up @@ -91,4 +92,6 @@
);

$services->set(UseNewComponentIdForPageTreeRector::class);

$services->set(RefactorBackendUtilityGetPagesTSconfigRector::class);
};
82 changes: 82 additions & 0 deletions src/Rector/v9/v0/RefactorBackendUtilityGetPagesTSconfigRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v9\v0;

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use TYPO3\CMS\Backend\Utility\BackendUtility;

/**
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.0/Deprecation-54152-DeprecateArgumentsOfBackendUtilityGetPagesTSconfig.html
*/
final class RefactorBackendUtilityGetPagesTSconfigRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [StaticCall::class];
}

/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isMethodStaticCallOrClassMethodObjectType($node, BackendUtility::class)) {
return null;
}

if (! $this->isName($node->name, 'getPagesTSconfig')) {
return null;
}

if (! isset($node->args[1], $node->args[2])) {
return null;
}

$rootLine = $this->getValue($node->args[1]->value);
$returnPartArray = $this->getValue($node->args[2]->value);

// If a custom non default rootline is given, nothing can be done
if ('null' !== $rootLine) {
return null;
}

// Just remove the arguments if equals to default ones
if (false === $returnPartArray) {
$node->args = [$node->args[0]];

return null;
}

// Change to method name getRawPagesTSconfig if argument $returnPartArray is true and rootline is null
$node->name = new Identifier('getRawPagesTSconfig');
$node->args = [$node->args[0]];

return null;
}

/**
* @codeCoverageIgnore
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Refactor method getPagesTSconfig of class BackendUtility if possible', [
new CodeSample(<<<'PHP'
use TYPO3\CMS\Backend\Utility\BackendUtility;
$pagesTsConfig = BackendUtility::getPagesTSconfig(1, $rootLine = null, $returnPartArray = true);
PHP
, <<<'PHP'
use TYPO3\CMS\Backend\Utility\BackendUtility;
$pagesTsConfig = BackendUtility::getRawPagesTSconfig(1, $rootLine = null);
PHP
),
]);
}
}
9 changes: 9 additions & 0 deletions stubs/Backend/Utility/BackendUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@ public static function getPidForModTSconfig($table, $uid, $pid): int
{
return 1;
}

public static function getPagesTSconfig($id, $rootLine = null, $returnPartArray = false): void
{
}

public static function getRawPagesTSconfig($id, array $rootLine = null): void
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v0\RefactorBackendUtilityGetPagesTSconfig\Fixture;

use TYPO3\CMS\Backend\Utility\BackendUtility;

class MyClass
{
/**
* @var array|null
*/
private $rootline;

public function getPagesTsConfigWrapper($id, $rootLine = null, $returnPartArray = false)
{
BackendUtility::getPagesTSconfig($id, $rootLine, $returnPartArray);
}

public function getPagesTsConfigWrapper2($id, $returnPartArray = false)
{
BackendUtility::getPagesTSconfig($id, $this->rootline, $returnPartArray);
}
}

$id = 1;
BackendUtility::getPagesTSconfig($id);
BackendUtility::getPagesTSconfig($id, null, false);
$returnPartArray = true;
BackendUtility::getPagesTSconfig($id, null, $returnPartArray);
BackendUtility::getPagesTSconfig($id, ['rootline'], true);

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v0\RefactorBackendUtilityGetPagesTSconfig\Fixture;

use TYPO3\CMS\Backend\Utility\BackendUtility;

class MyClass
{
/**
* @var array|null
*/
private $rootline;

public function getPagesTsConfigWrapper($id, $rootLine = null, $returnPartArray = false)
{
BackendUtility::getPagesTSconfig($id, $rootLine, $returnPartArray);
}

public function getPagesTsConfigWrapper2($id, $returnPartArray = false)
{
BackendUtility::getPagesTSconfig($id, $this->rootline, $returnPartArray);
}
}

$id = 1;
BackendUtility::getPagesTSconfig($id);
BackendUtility::getPagesTSconfig($id);
$returnPartArray = true;
BackendUtility::getRawPagesTSconfig($id);
BackendUtility::getPagesTSconfig($id, ['rootline'], true);

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

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v0\RefactorBackendUtilityGetPagesTSconfig;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Ssch\TYPO3Rector\Rector\v9\v0\RefactorBackendUtilityGetPagesTSconfigRector;
use Symplify\SmartFileSystem\SmartFileInfo;

final class RefactorBackendUtilityGetPagesTSconfigRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return RefactorBackendUtilityGetPagesTSconfigRector::class;
}
}