-
-
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.
[TASK] Refactor BackendUtility::getPagesTSconfig where possible
- Can only transform calls where rootline is null Resolves: #948
- Loading branch information
1 parent
34daea4
commit 3248898
Showing
5 changed files
with
190 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
82 changes: 82 additions & 0 deletions
82
src/Rector/v9/v0/RefactorBackendUtilityGetPagesTSconfigRector.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,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 | ||
), | ||
]); | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
...ackendUtilityGetPagesTSconfig/Fixture/refactor_backend_utility_get_pages_tsconfig.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,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); | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...factorBackendUtilityGetPagesTSconfig/RefactorBackendUtilityGetPagesTSconfigRectorTest.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,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; | ||
} | ||
} |