Skip to content

Commit

Permalink
[BUGFIX] Correct replacement for isCli (#1586)
Browse files Browse the repository at this point in the history
Resolves: #1584
  • Loading branch information
sabbelasichon authored Nov 11, 2020
1 parent 53ceb7b commit eb5ca7e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
43 changes: 30 additions & 13 deletions src/Rector/v9/v4/ConstantToEnvironmentCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,11 @@
*/
final class ConstantToEnvironmentCallRector extends AbstractRector
{
/**
* @var string[]
*/
private const ALLOWED_NAMES = ['TYPO3_REQUESTTYPE_CLI', 'TYPO3_REQUESTTYPE'];

/**
* @codeCoverageIgnore
*/
Expand All @@ -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');
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class ClassWithALotOfEnvironmentVariables
PATH_typo3conf;
PATH_typo3;
TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI;
TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_FE;
}
}

Expand All @@ -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;
}
}
11 changes: 11 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit eb5ca7e

Please sign in to comment.