-
-
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 getMenu instead of getFirstWebPage (#1597)
Resolves: 1080
- Loading branch information
1 parent
68f441a
commit 705142c
Showing
6 changed files
with
202 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
124 changes: 124 additions & 0 deletions
124
src/Rector/v9/v4/UseGetMenuInsteadOfGetFirstWebPageRector.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,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v9\v4; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\BooleanNot; | ||
use PhpParser\Node\Expr\Empty_; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\If_; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver; | ||
use TYPO3\CMS\Frontend\Page\PageRepository; | ||
|
||
/** | ||
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.4/Deprecation-85971-DeprecatePageRepository-getFirstWebPage.html | ||
*/ | ||
final class UseGetMenuInsteadOfGetFirstWebPageRector 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, 'getFirstWebPage')) { | ||
return null; | ||
} | ||
|
||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); | ||
|
||
if (! $parentNode instanceof Assign) { | ||
return null; | ||
} | ||
|
||
$rootLevelPagesVariable = new Variable('rootLevelPages'); | ||
$this->addRootLevelPagesAssignment($rootLevelPagesVariable, $node); | ||
|
||
$resetRootLevelPagesNode = $this->createFuncCall('reset', [$rootLevelPagesVariable]); | ||
|
||
$ifNode = new If_(new BooleanNot(new Empty_($rootLevelPagesVariable))); | ||
$parentNode->expr = $resetRootLevelPagesNode; | ||
$ifNode->stmts[] = new Expression($parentNode); | ||
|
||
$this->addNodeBeforeNode($ifNode, $node); | ||
|
||
try { | ||
$this->removeNode($node); | ||
} catch (ShouldNotHappenException $shouldNotHappenException) { | ||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); | ||
$this->removeNode($parentNode); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Use method getMenu instead of getFirstWebPage', [ | ||
new CodeSample(<<<'PHP' | ||
$theFirstPage = $GLOBALS['TSFE']->sys_page->getFirstWebPage(0); | ||
PHP | ||
, <<<'PHP' | ||
$rootLevelPages = $GLOBALS['TSFE']->sys_page->getMenu(0, 'uid', 'sorting', '', false); | ||
if (!empty($rootLevelPages)) { | ||
$theFirstPage = reset($rootLevelPages); | ||
} | ||
PHP | ||
), | ||
]); | ||
} | ||
|
||
private function shouldSkip(MethodCall $node): bool | ||
{ | ||
if ($this->isMethodStaticCallOrClassMethodObjectType($node, PageRepository::class)) { | ||
return false; | ||
} | ||
|
||
return ! $this->typo3NodeResolver->isMethodCallOnPropertyOfGlobals( | ||
$node, | ||
Typo3NodeResolver::TYPO_SCRIPT_FRONTEND_CONTROLLER, | ||
'sys_page' | ||
); | ||
} | ||
|
||
private function addRootLevelPagesAssignment(Variable $rootLevelPagesVariable, MethodCall $node): void | ||
{ | ||
$rootLevelPagesNode = new Assign($rootLevelPagesVariable, $this->createMethodCall( | ||
$node->var, | ||
'getMenu', | ||
[$node->args[0], 'uid', 'sorting', '', false] | ||
)); | ||
$this->addNodeBeforeNode($rootLevelPagesNode, $node); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
...etMenuInsteadOfGetFirstWebPage/Fixture/use_get_menu_instead_of_get_first_web_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,22 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Frontend\Page\PageRepository; | ||
|
||
$pageRepository = GeneralUtility::makeInstance(PageRepository::class); | ||
$theFirstPage = $pageRepository->getFirstWebPage(0); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Frontend\Page\PageRepository; | ||
|
||
$pageRepository = GeneralUtility::makeInstance(PageRepository::class); | ||
$rootLevelPages = $pageRepository->getMenu(0, 'uid', 'sorting', '', false); | ||
if (!empty($rootLevelPages)) { | ||
$theFirstPage = reset($rootLevelPages); | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...uInsteadOfGetFirstWebPage/Fixture/use_get_menu_instead_of_get_first_web_page_tsfe.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,12 @@ | ||
<?php | ||
|
||
$theFirstPage = $GLOBALS['TSFE']->sys_page->getFirstWebPage(0); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
$rootLevelPages = $GLOBALS['TSFE']->sys_page->getMenu(0, 'uid', 'sorting', '', false); | ||
if (!empty($rootLevelPages)) { | ||
$theFirstPage = reset($rootLevelPages); | ||
} |
31 changes: 31 additions & 0 deletions
31
...v9/v4/UseGetMenuInsteadOfGetFirstWebPage/UseGetMenuInsteadOfGetFirstWebPageRectorTest.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\v4\UseGetMenuInsteadOfGetFirstWebPage; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Ssch\TYPO3Rector\Rector\v9\v4\UseGetMenuInsteadOfGetFirstWebPageRector; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class UseGetMenuInsteadOfGetFirstWebPageRectorTest 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 UseGetMenuInsteadOfGetFirstWebPageRector::class; | ||
} | ||
} |