From 6b07c667dfd2a34167720e7dc98319ae09187f82 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Fri, 13 Nov 2020 11:03:23 +0100 Subject: [PATCH] [TASK] Use method getTSConfig instead of property userTS (#1593) Resolves: #1054 --- src/Helper/Typo3NodeResolver.php | 5 ++ ...onfigOfBackendUserAuthenticationRector.php | 78 +++++++++++++++++++ .../BackendUserAuthentication.php | 26 +++++++ stubs/Core/Charset/CharsetConverter.php | 20 ++--- .../backend_user_user_ts_config.php.inc | 15 ++++ ...ckend_user_user_ts_config_in_class.php.inc | 51 ++++++++++++ ...gOfBackendUserAuthenticationRectorTest.php | 31 ++++++++ tests/bootstrap.php | 2 + 8 files changed, 214 insertions(+), 14 deletions(-) create mode 100644 src/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector.php create mode 100644 stubs/Core/Authentication/BackendUserAuthentication.php create mode 100644 tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/Fixture/backend_user_user_ts_config.php.inc create mode 100644 tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/Fixture/backend_user_user_ts_config_in_class.php.inc create mode 100644 tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRectorTest.php diff --git a/src/Helper/Typo3NodeResolver.php b/src/Helper/Typo3NodeResolver.php index bdde850f0..b90fc63ad 100644 --- a/src/Helper/Typo3NodeResolver.php +++ b/src/Helper/Typo3NodeResolver.php @@ -45,6 +45,11 @@ final class Typo3NodeResolver */ public const TYPO3_DB = 'TYPO3_DB'; + /** + * @var string + */ + public const BACKEND_USER = 'BE_USER'; + /** * @var string */ diff --git a/src/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector.php b/src/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector.php new file mode 100644 index 000000000..fd033b1bd --- /dev/null +++ b/src/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector.php @@ -0,0 +1,78 @@ +typo3NodeResolver = $typo3NodeResolver; + } + + public function getNodeTypes(): array + { + return [PropertyFetch::class]; + } + + /** + * @param PropertyFetch $node + */ + public function refactor(Node $node): ?Node + { + if ($this->shouldSkip($node)) { + return null; + } + + if (! $this->isName($node->name, 'userTS')) { + return null; + } + + return $this->createMethodCall($node->var, 'getTSConfig'); + } + + /** + * @codeCoverageIgnore + */ + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Use method getTSConfig instead of property userTS', [ + new CodeSample(<<<'PHP' +if(is_array($GLOBALS['BE_USER']->userTS['tx_news.']) && $GLOBALS['BE_USER']->userTS['tx_news.']['singleCategoryAcl'] === '1') { + return true; +} +PHP + , <<<'PHP' +if(is_array($GLOBALS['BE_USER']->getTSConfig()['tx_news.']) && $GLOBALS['BE_USER']->getTSConfig()['tx_news.']['singleCategoryAcl'] === '1') { + return true; +} +PHP + ), + ]); + } + + private function shouldSkip(PropertyFetch $node): bool + { + if ($this->typo3NodeResolver->isPropertyFetchOnAnyPropertyOfGlobals($node, Typo3NodeResolver::BACKEND_USER)) { + return false; + } + return ! $this->isObjectType($node->var, BackendUserAuthentication::class); + } +} diff --git a/stubs/Core/Authentication/BackendUserAuthentication.php b/stubs/Core/Authentication/BackendUserAuthentication.php new file mode 100644 index 000000000..5c9bd4a40 --- /dev/null +++ b/stubs/Core/Authentication/BackendUserAuthentication.php @@ -0,0 +1,26 @@ + [ + 'singleCategoryAcl' => 1 + ] + ]; + + public function getTSConfig($objectString = null, $config = null): array + { + return []; + } +} diff --git a/stubs/Core/Charset/CharsetConverter.php b/stubs/Core/Charset/CharsetConverter.php index 5ea4c82fd..9f4a498a7 100644 --- a/stubs/Core/Charset/CharsetConverter.php +++ b/stubs/Core/Charset/CharsetConverter.php @@ -15,35 +15,27 @@ public function getPreferredClientLanguage($languageCodesList): string return 'foo'; } - public function strlen($charset, $string) + public function strlen($charset, $string): void { - return mb_strlen($string, $charset); } - public function convCapitalize($charset, $string) + public function convCapitalize($charset, $string): void { - return mb_convert_case($string, MB_CASE_TITLE, $charset); } - public function substr($charset, $string, $start, $len = null) + public function substr($charset, $string, $start, $len = null): void { - return mb_substr($string, $start, $len, $charset); } - public function conv_case($charset, $string, $case) + public function conv_case($charset, $string, $case): void { - return $case === 'toLower' - ? mb_strtolower($string, $charset) - : mb_strtoupper($string, $charset); } - public function utf8_strpos($haystack, $needle, $offset = 0) + public function utf8_strpos($haystack, $needle, $offset = 0): void { - return mb_strpos($haystack, $needle, $offset, 'utf-8'); } - public function utf8_strrpos($haystack, $needle) + public function utf8_strrpos($haystack, $needle): void { - return mb_strrpos($haystack, $needle, 'utf-8'); } } diff --git a/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/Fixture/backend_user_user_ts_config.php.inc b/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/Fixture/backend_user_user_ts_config.php.inc new file mode 100644 index 000000000..57d9d572c --- /dev/null +++ b/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/Fixture/backend_user_user_ts_config.php.inc @@ -0,0 +1,15 @@ +userTS['tx_news.']) && $GLOBALS['BE_USER']->userTS['tx_news.']['singleCategoryAcl'] === '1') { + return true; +} + +?> +----- +getTSConfig()['tx_news.']) && $GLOBALS['BE_USER']->getTSConfig()['tx_news.']['singleCategoryAcl'] === '1') { + return true; +} + +?> diff --git a/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/Fixture/backend_user_user_ts_config_in_class.php.inc b/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/Fixture/backend_user_user_ts_config_in_class.php.inc new file mode 100644 index 000000000..5a228a0dc --- /dev/null +++ b/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/Fixture/backend_user_user_ts_config_in_class.php.inc @@ -0,0 +1,51 @@ +backendUserAuthentication = $GLOBALS['BE_USER']; + } + + public function getUserTsConfig(): array + { + return $this->backendUserAuthentication->userTS; + } +} + +?> +----- +backendUserAuthentication = $GLOBALS['BE_USER']; + } + + public function getUserTsConfig(): array + { + return $this->backendUserAuthentication->getTSConfig(); + } +} + +?> diff --git a/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRectorTest.php b/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRectorTest.php new file mode 100644 index 000000000..63e6c6a01 --- /dev/null +++ b/tests/Rector/v9/v3/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication/PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRectorTest.php @@ -0,0 +1,31 @@ +doTestFileInfo($fileInfo); + } + + public function provideDataForTest(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector::class; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ede92f993..9229a7821 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Ssch\TYPO3Rector\Stubs\StubLoader; +use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; use TYPO3\CMS\Core\Localization\LanguageService; use TYPO3\CMS\Core\Resource\Security\FileNameValidator; use TYPO3\CMS\Core\TimeTracker\TimeTracker; @@ -27,6 +28,7 @@ $GLOBALS['TSFE'] = $typoScriptFrontendController; $GLOBALS['TT'] = new TimeTracker(); +$GLOBALS['BE_USER'] = new BackendUserAuthentication(); $GLOBALS['TYPO3_LOADED_EXT'] = []; $GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class); $GLOBALS['PARSETIME_START'] = time();