diff --git a/config/v9/typo3-95.php b/config/v9/typo3-95.php index c7f3171e0..6ddc8efe2 100644 --- a/config/v9/typo3-95.php +++ b/config/v9/typo3-95.php @@ -6,6 +6,7 @@ use Rector\Renaming\ValueObject\MethodCallRename; use function Rector\SymfonyPhpConfig\inline_value_objects; use Ssch\TYPO3Rector\Rector\Migrations\RenameClassMapAliasRector; +use Ssch\TYPO3Rector\Rector\v9\v4\RefactorPropertiesOfTypoScriptFrontendControllerRector; use Ssch\TYPO3Rector\Rector\v9\v5\RefactorProcessOutputRector; use Ssch\TYPO3Rector\Rector\v9\v5\RemoveFlushCachesRector; @@ -59,4 +60,6 @@ $services->set(RemoveInitMethodFromPageRepositoryRector::class); $services->set(RefactorProcessOutputRector::class); + + $services->set(RefactorPropertiesOfTypoScriptFrontendControllerRector::class); }; diff --git a/src/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendControllerRector.php b/src/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendControllerRector.php new file mode 100644 index 000000000..a23736d37 --- /dev/null +++ b/src/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendControllerRector.php @@ -0,0 +1,98 @@ +typo3NodeResolver = $typo3NodeResolver; + } + + public function getNodeTypes(): array + { + return [PropertyFetch::class]; + } + + /** + * @param PropertyFetch $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isObjectType($node->var, TypoScriptFrontendController::class) + && ! $this->typo3NodeResolver->isPropertyFetchOnAnyPropertyOfGlobals( + $node, + Typo3NodeResolver::TYPO_SCRIPT_FRONTEND_CONTROLLER + )) { + return null; + } + + $parentNode = $node->getAttribute('parent'); + + // Check if we have an assigment to the property, if so do not change it + if ($parentNode instanceof Assign && $parentNode->var instanceof PropertyFetch) { + return null; + } + + if (! $this->isNames($node->name, ['ADMCMD_preview_BEUSER_uid', 'workspacePreview', 'loginAllowedInBranch'])) { + return null; + } + + if ($this->isName($node->name, 'loginAllowedInBranch')) { + return $this->createMethodCall($node->var, 'checkIfLoginAllowedInBranch'); + } + + $contextInstanceNode = $this->createStaticCall(GeneralUtility::class, 'makeInstance', [ + $this->createClassConstantReference(Context::class), + ]); + + if ($this->isName($node->name, 'ADMCMD_preview_BEUSER_uid')) { + return $this->createMethodCall($contextInstanceNode, 'getPropertyFromAspect', ['backend.user', 'id', 0]); + } + + return $this->createMethodCall($contextInstanceNode, 'getPropertyFromAspect', ['workspace', 'id', 0]); + } + + /** + * @codeCoverageIgnore + */ + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Refactor some properties of TypoScriptFrontendController', [ + new CodeSample(<<<'PHP' +$previewBeUserUid = $GLOBALS['TSFE']->ADMCMD_preview_BEUSER_uid; +$workspacePreview = $GLOBALS['TSFE']->workspacePreview; +$loginAllowedInBranch = $GLOBALS['TSFE']->loginAllowedInBranch; +PHP + , <<<'PHP' +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Context\Context; +$previewBeUserUid = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('backend.user', 'id', 0); +$workspacePreview = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('workspace', 'id', 0); +$loginAllowedInBranch = $GLOBALS['TSFE']->checkIfLoginAllowedInBranch(); +PHP + ), + ]); + } +} diff --git a/stubs/Frontend/Controller/TypoScriptFrontendController.php b/stubs/Frontend/Controller/TypoScriptFrontendController.php index 28d1442a0..711b56202 100644 --- a/stubs/Frontend/Controller/TypoScriptFrontendController.php +++ b/stubs/Frontend/Controller/TypoScriptFrontendController.php @@ -5,6 +5,7 @@ namespace TYPO3\CMS\Frontend\Controller; use TYPO3\CMS\Core\Charset\CharsetConverter; +use TYPO3\CMS\Core\Context\Context; use TYPO3\CMS\Core\Page\PageRenderer; use TYPO3\CMS\Core\Site\Entity\SiteLanguage; use TYPO3\CMS\Core\TypoScript\TemplateService; @@ -102,10 +103,30 @@ final class TypoScriptFrontendController */ public $sys_language_contentOL = 0; + /** + * @var int + */ + public $ADMCMD_preview_BEUSER_uid = 0; + + /** + * @var int + */ + public $workspacePreview = 0; + + /** + * @var bool + */ + public $loginAllowedInBranch = false; + public function initTemplate(): void { } + public function checkIfLoginAllowedInBranch(): bool + { + return false; + } + public function __construct() { //fake template object, otherwise tests cannot access this property diff --git a/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/Fixture/refactor_properties_tsfe.php.inc b/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/Fixture/refactor_properties_tsfe.php.inc new file mode 100644 index 000000000..8c5ad8f89 --- /dev/null +++ b/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/Fixture/refactor_properties_tsfe.php.inc @@ -0,0 +1,17 @@ +loginAllowedInBranch = false; +$previewBeUserUid = $GLOBALS['TSFE']->ADMCMD_preview_BEUSER_uid; +$workspacePreview = $GLOBALS['TSFE']->workspacePreview; +$loginAllowedInBranch = $GLOBALS['TSFE']->loginAllowedInBranch; + +?> +----- +loginAllowedInBranch = false; +$previewBeUserUid = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('backend.user', 'id', 0); +$workspacePreview = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('workspace', 'id', 0); +$loginAllowedInBranch = $GLOBALS['TSFE']->checkIfLoginAllowedInBranch(); diff --git a/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/Fixture/refactor_properties_tsfe_in_class.php.inc b/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/Fixture/refactor_properties_tsfe_in_class.php.inc new file mode 100644 index 000000000..09657db09 --- /dev/null +++ b/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/Fixture/refactor_properties_tsfe_in_class.php.inc @@ -0,0 +1,55 @@ +getTypoScriptFrontendController()->loginAllowedInBranch = false; + $previewBeUserUid = $this->getTypoScriptFrontendController()->ADMCMD_preview_BEUSER_uid; + $workspacePreview = $this->getTypoScriptFrontendController()->workspacePreview; + $loginAllowedInBranch = $this->getTypoScriptFrontendController()->loginAllowedInBranch; + } + + /** + * @return TypoScriptFrontendController + */ + private function getTypoScriptFrontendController(): TypoScriptFrontendController + { + return $GLOBALS['TSFE']; + } +} + +?> +----- +getTypoScriptFrontendController()->loginAllowedInBranch = false; + $previewBeUserUid = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('backend.user', 'id', 0); + $workspacePreview = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('workspace', 'id', 0); + $loginAllowedInBranch = $this->getTypoScriptFrontendController()->checkIfLoginAllowedInBranch(); + } + + /** + * @return TypoScriptFrontendController + */ + private function getTypoScriptFrontendController(): TypoScriptFrontendController + { + return $GLOBALS['TSFE']; + } +} + +?> diff --git a/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/RefactorPropertiesOfTypoScriptFrontendControllerRectorTest.php b/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/RefactorPropertiesOfTypoScriptFrontendControllerRectorTest.php new file mode 100644 index 000000000..6764a31ce --- /dev/null +++ b/tests/Rector/v9/v5/RefactorPropertiesOfTypoScriptFrontendController/RefactorPropertiesOfTypoScriptFrontendControllerRectorTest.php @@ -0,0 +1,31 @@ +doTestFileInfo($fileInfo); + } + + public function provideDataForTest(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return RefactorPropertiesOfTypoScriptFrontendControllerRector::class; + } +}