From eb5ca7e5398ab93e10ab26df772976ce96cca029 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Wed, 11 Nov 2020 14:22:34 +0100 Subject: [PATCH] [BUGFIX] Correct replacement for isCli (#1586) Resolves: #1584 --- .../v9/v4/ConstantToEnvironmentCallRector.php | 43 +++++++++++++------ .../Fixture/environment_constants.php.inc | 4 +- tests/bootstrap.php | 11 +++++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/Rector/v9/v4/ConstantToEnvironmentCallRector.php b/src/Rector/v9/v4/ConstantToEnvironmentCallRector.php index b1a0ed8a6..a978eb1e9 100644 --- a/src/Rector/v9/v4/ConstantToEnvironmentCallRector.php +++ b/src/Rector/v9/v4/ConstantToEnvironmentCallRector.php @@ -5,6 +5,7 @@ namespace Ssch\TYPO3Rector\Rector\v9\v4; use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp\BitwiseAnd; use PhpParser\Node\Expr\BinaryOp\BooleanOr; use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\ConstFetch; @@ -19,6 +20,11 @@ */ final class ConstantToEnvironmentCallRector extends AbstractRector { + /** + * @var string[] + */ + private const ALLOWED_NAMES = ['TYPO3_REQUESTTYPE_CLI', 'TYPO3_REQUESTTYPE']; + /** * @codeCoverageIgnore */ @@ -34,30 +40,43 @@ public function getDefinition(): RectorDefinition */ public function getNodeTypes(): array { - return [ConstFetch::class]; + return [ConstFetch::class, BitwiseAnd::class]; } + /** + * @param ConstFetch|BitwiseAnd $node + */ public function refactor(Node $node): ?Node + { + if ($node instanceof ConstFetch) { + return $this->refactorConstants($node); + } + + if (! $node->left instanceof ConstFetch || ! $node->right instanceof ConstFetch) { + return null; + } + if (! $this->isNames($node->left, self::ALLOWED_NAMES) || ! $this->isNames($node->right, self::ALLOWED_NAMES)) { + return null; + } + + return $this->createStaticCall(Environment::class, 'isCli'); + } + + private function refactorConstants(ConstFetch $node): ?Node { $constantName = $this->getName($node); if (null === $constantName) { return null; } + if (! in_array( $constantName, - [ - 'PATH_thisScript', - 'PATH_site', - 'PATH_typo3', - 'TYPO3_REQUESTTYPE', - 'TYPO3_REQUESTTYPE_CLI', - 'PATH_typo3conf', - 'TYPO3_OS', - ], + ['PATH_thisScript', 'PATH_site', 'PATH_typo3', 'PATH_typo3conf', 'TYPO3_OS'], false )) { return null; } + switch ($constantName) { case 'PATH_thisScript': return $this->createStaticCall(Environment::class, 'getCurrentScript'); @@ -72,10 +91,8 @@ public function refactor(Node $node): ?Node Environment::class, 'isWindows' )); - case 'TYPO3_REQUESTTYPE_CLI': - case 'TYPO3_REQUESTTYPE': - return $this->createStaticCall(Environment::class, 'isCli'); } + return null; } } diff --git a/tests/Rector/v9/v4/ConstantToEnvironmentCall/Fixture/environment_constants.php.inc b/tests/Rector/v9/v4/ConstantToEnvironmentCall/Fixture/environment_constants.php.inc index 661c6ae39..9226942fa 100644 --- a/tests/Rector/v9/v4/ConstantToEnvironmentCall/Fixture/environment_constants.php.inc +++ b/tests/Rector/v9/v4/ConstantToEnvironmentCall/Fixture/environment_constants.php.inc @@ -10,6 +10,7 @@ final class ClassWithALotOfEnvironmentVariables PATH_typo3conf; PATH_typo3; TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI; + TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_FE; } } @@ -27,6 +28,7 @@ final class ClassWithALotOfEnvironmentVariables Environment::isUnix() || Environment::isWindows(); Environment::getLegacyConfigPath(); Environment::getBackendPath(); - Environment::isCli() & Environment::isCli(); + Environment::isCli(); + TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_FE; } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8bf581cfa..ede92f993 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -75,3 +75,14 @@ define('TYPO3_URL_EXCEPTION', 'https://typo3.org/go/exception/CMS/'); define('TYPO3_URL_DONATE', 'https://typo3.org/community/contribute/donate/'); define('TYPO3_URL_WIKI_OPCODECACHE', 'https://wiki.typo3.orgOpcode_Cache/'); + +define('TYPO3_REQUESTTYPE_FE', 1); +define('TYPO3_REQUESTTYPE_BE', 2); +define('TYPO3_REQUESTTYPE_CLI', 4); +define('TYPO3_REQUESTTYPE_AJAX', 8); +define('TYPO3_REQUESTTYPE_INSTALL', 16); +define('PATH_thisScript', 'foo'); +define('TYPO3_OS', 'foo'); +define('PATH_typo3conf', 'foo'); +define('PATH_typo3', 'foo'); +define('TYPO3_REQUESTTYPE', 'foo');