Skip to content

Commit

Permalink
[TASK] Use method getPageShortcut directly from PageRepository (#1594)
Browse files Browse the repository at this point in the history
Resolves: #1063
  • Loading branch information
sabbelasichon authored Nov 13, 2020
1 parent b720579 commit 9b0e17d
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/v9/typo3-93.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Rector\Renaming\ValueObject\MethodCallRename;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Ssch\TYPO3Rector\Rector\v9\v3\BackendUtilityGetModuleUrlRector;
use Ssch\TYPO3Rector\Rector\v9\v3\PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector;
use Ssch\TYPO3Rector\Rector\v9\v3\RemoveColPosParameterRector;
use Ssch\TYPO3Rector\Rector\v9\v3\UseMethodGetPageShortcutDirectlyFromSysPageRector;
use Ssch\TYPO3Rector\Rector\v9\v3\ValidateAnnotationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use TYPO3\CMS\Backend\Controller\Page\LocalizationController;
Expand All @@ -32,4 +34,6 @@
]]);

$services->set(BackendUtilityGetModuleUrlRector::class);
$services->set(PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector::class);
$services->set(UseMethodGetPageShortcutDirectlyFromSysPageRector::class);
};
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
);
}
}
5 changes: 5 additions & 0 deletions stubs/Frontend/Controller/TypoScriptFrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,9 @@ public function getPageRenderer(): PageRenderer
{
return new PageRenderer();
}

public function getPageShortcut($SC, $mode, $thisUid, $itera = 20, $pageLog = [], $disableGroupCheck = false): array
{
return [];
}
}
5 changes: 5 additions & 0 deletions stubs/Frontend/Page/PageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public function init($show_hidden): string
{
return 'foo';
}

public function getPageShortcut($SC, $mode, $thisUid, int $itera, array $pageLog, bool $disableGroupCheck): array
{
return [];
}
}
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);

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

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

0 comments on commit 9b0e17d

Please sign in to comment.