-
-
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] Use method getPageShortcut directly from PageRepository (#1594)
Resolves: #1063
- Loading branch information
1 parent
b720579
commit 9b0e17d
Showing
7 changed files
with
160 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
83 changes: 83 additions & 0 deletions
83
src/Rector/v9/v3/UseMethodGetPageShortcutDirectlyFromSysPageRector.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,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v9\v3; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Identifier; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver; | ||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | ||
|
||
/** | ||
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.3/Deprecation-85130-TSFE-getPageShortcutMovedToPageRepository.html | ||
*/ | ||
final class UseMethodGetPageShortcutDirectlyFromSysPageRector extends AbstractRector | ||
{ | ||
/** | ||
* @var Typo3NodeResolver | ||
*/ | ||
private $typo3NodeResolver; | ||
|
||
public function __construct(Typo3NodeResolver $typo3NodeResolver) | ||
{ | ||
$this->typo3NodeResolver = $typo3NodeResolver; | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, 'getPageShortcut')) { | ||
return null; | ||
} | ||
|
||
return $this->createMethodCall( | ||
new PropertyFetch($node->var, new Identifier('sys_page')), | ||
'getPageShortcut', | ||
$node->args | ||
); | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Use method getPageShortcut directly from PageRepository', [ | ||
new CodeSample(<<<'PHP' | ||
$GLOBALS['TSFE']->getPageShortcut('shortcut', 1, 1); | ||
PHP | ||
, <<<'PHP' | ||
$GLOBALS['TSFE']->sys_page->getPageShortcut('shortcut', 1, 1); | ||
PHP | ||
), | ||
]); | ||
} | ||
|
||
private function shouldSkip(MethodCall $node): bool | ||
{ | ||
if ($this->isMethodStaticCallOrClassMethodObjectType($node, TypoScriptFrontendController::class)) { | ||
return false; | ||
} | ||
return ! $this->typo3NodeResolver->isAnyMethodCallOnGlobals( | ||
$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
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
13 changes: 13 additions & 0 deletions
13
...hodGetPageShortcutDirectlyFromSysPage/Fixture/use_get_page_shortcut_from_sys_page.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,13 @@ | ||
<?php | ||
|
||
$shortCutPage1 = $GLOBALS['TSFE']->getPageShortcut('shortcut', 1, 1); | ||
$shortCutPage2 = $GLOBALS['TSFE']->getPageShortcut('shortcut', 1, 1, 50); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
$shortCutPage1 = $GLOBALS['TSFE']->sys_page->getPageShortcut('shortcut', 1, 1); | ||
$shortCutPage2 = $GLOBALS['TSFE']->sys_page->getPageShortcut('shortcut', 1, 1, 50); | ||
|
||
?> |
19 changes: 19 additions & 0 deletions
19
...eShortcutDirectlyFromSysPage/Fixture/use_get_page_shortcut_from_sys_page_in_class.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,19 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | ||
|
||
/** @var TypoScriptFrontendController $typoScriptFrontendController */ | ||
$typoScriptFrontendController = $GLOBALS['TSFE']; | ||
$shortCutPage1 = $typoScriptFrontendController->getPageShortcut('shortcut', 1, 1); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | ||
|
||
/** @var TypoScriptFrontendController $typoScriptFrontendController */ | ||
$typoScriptFrontendController = $GLOBALS['TSFE']; | ||
$shortCutPage1 = $typoScriptFrontendController->sys_page->getPageShortcut('shortcut', 1, 1); | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...PageShortcutDirectlyFromSysPage/UseMethodGetPageShortcutDirectlyFromSysPageRectorTest.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\v3\UseMethodGetPageShortcutDirectlyFromSysPage; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Ssch\TYPO3Rector\Rector\v9\v3\UseMethodGetPageShortcutDirectlyFromSysPageRector; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class UseMethodGetPageShortcutDirectlyFromSysPageRectorTest 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 UseMethodGetPageShortcutDirectlyFromSysPageRector::class; | ||
} | ||
} |