Skip to content

Commit

Permalink
[TASK] Use method getMenu instead of getFirstWebPage (#1597)
Browse files Browse the repository at this point in the history
Resolves: 1080
  • Loading branch information
sabbelasichon authored Nov 13, 2020
1 parent 68f441a commit 705142c
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/v9/typo3-94.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Ssch\TYPO3Rector\Rector\v9\v4\SystemEnvironmentBuilderConstantsRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseContextApiForVersioningWorkspaceIdRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseContextApiRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseGetMenuInsteadOfGetFirstWebPageRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseLanguageAspectForTsfeLanguagePropertiesRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseSignalTablesDefinitionIsBeingBuiltSqlExpectedSchemaServiceRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand Down Expand Up @@ -44,4 +45,6 @@
$services->set(BackendUtilityShortcutExistsRector::class);

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

$services->set(UseGetMenuInsteadOfGetFirstWebPageRector::class);
};
124 changes: 124 additions & 0 deletions src/Rector/v9/v4/UseGetMenuInsteadOfGetFirstWebPageRector.php
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);
}
}
10 changes: 10 additions & 0 deletions stubs/Frontend/Page/PageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ public function getPageShortcut($SC, $mode, $thisUid, int $itera, array $pageLog
{
return [];
}

public function getFirstWebPage($uid): array
{
return [];
}

public function getMenu($pageId, $fields = '*', $sortField = 'sorting', $additionalWhereClause = '', $checkShortcuts = true): array
{
return [];
}
}
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);
}

?>
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);
}
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;
}
}

0 comments on commit 705142c

Please sign in to comment.