rector/rules/php73/src/Rector/ConstFetch/SensitiveConstantNameRector.php

145 lines
3.5 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2018-10-12 05:03:37 +00:00
namespace Rector\Php73\Rector\ConstFetch;
2018-10-12 05:03:37 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2018-10-12 05:03:37 +00:00
/**
* @see https://wiki.php.net/rfc/case_insensitive_constant_deprecation
* @see \Rector\Php73\Tests\Rector\ConstFetch\SensitiveConstantNameRector\SensitiveConstantNameRectorTest
2018-10-12 05:03:37 +00:00
*/
final class SensitiveConstantNameRector extends AbstractRector
{
/**
* @see http://php.net/manual/en/reserved.constants.php
* @var string[]
*/
2020-02-18 22:09:25 +00:00
private const PHP_RESERVED_CONSTANTS = [
2018-10-12 05:03:37 +00:00
'PHP_VERSION',
'PHP_MAJOR_VERSION',
'PHP_MINOR_VERSION',
'PHP_RELEASE_VERSION',
'PHP_VERSION_ID',
'PHP_EXTRA_VERSION',
'PHP_ZTS',
'PHP_DEBUG',
'PHP_MAXPATHLEN',
'PHP_OS',
'PHP_OS_FAMILY',
'PHP_SAPI',
'PHP_EOL',
'PHP_INT_MAX',
'PHP_INT_MIN',
'PHP_INT_SIZE',
'PHP_FLOAT_DIG',
'PHP_FLOAT_EPSILON',
'PHP_FLOAT_MIN',
'PHP_FLOAT_MAX',
'DEFAULT_INCLUDE_PATH',
'PEAR_INSTALL_DIR',
'PEAR_EXTENSION_DIR',
'PHP_EXTENSION_DIR',
'PHP_PREFIX',
'PHP_BINDIR',
'PHP_BINARY',
'PHP_MANDIR',
'PHP_LIBDIR',
'PHP_DATADIR',
'PHP_SYSCONFDIR',
'PHP_LOCALSTATEDIR',
'PHP_CONFIG_FILE_PATH',
'PHP_CONFIG_FILE_SCAN_DIR',
'PHP_SHLIB_SUFFIX',
'PHP_FD_SETSIZE',
'E_ERROR',
'E_WARNING',
'E_PARSE',
'E_NOTICE',
'E_CORE_ERROR',
'E_CORE_WARNING',
'E_COMPILE_ERROR',
'E_COMPILE_WARNING',
'E_USER_ERROR',
'E_USER_WARNING',
'E_USER_NOTICE',
'E_RECOVERABLE_ERROR',
'E_DEPRECATED',
'E_USER_DEPRECATED',
'E_ALL',
'E_STRICT',
'__COMPILER_HALT_OFFSET__',
'TRUE',
'FALSE',
'NULL',
];
public function getRuleDefinition(): RuleDefinition
2018-10-12 05:03:37 +00:00
{
return new RuleDefinition(
2018-10-12 05:03:37 +00:00
'Changes case insensitive constants to sensitive ones.',
[
new CodeSample(
<<<'CODE_SAMPLE'
2018-10-12 05:03:37 +00:00
define('FOO', 42, true);
var_dump(FOO);
var_dump(foo);
CODE_SAMPLE
2018-10-12 05:03:37 +00:00
,
<<<'CODE_SAMPLE'
2018-10-12 05:03:37 +00:00
define('FOO', 42, true);
var_dump(FOO);
var_dump(FOO);
CODE_SAMPLE
2018-10-12 05:03:37 +00:00
),
]
);
}
/**
* @return array<class-string<Node>>
2018-10-12 05:03:37 +00:00
*/
public function getNodeTypes(): array
{
return [ConstFetch::class];
}
/**
* @param ConstFetch $node
2018-10-12 05:03:37 +00:00
*/
public function refactor(Node $node): ?Node
2018-10-12 05:03:37 +00:00
{
$constantName = $this->getName($node);
if ($constantName === null) {
return null;
}
$uppercasedConstantName = strtoupper($constantName);
2018-10-12 05:03:37 +00:00
// is system constant?
2020-02-18 22:09:25 +00:00
if (in_array($uppercasedConstantName, self::PHP_RESERVED_CONSTANTS, true)) {
return null;
2018-10-12 05:03:37 +00:00
}
// constant is defined in current lower/upper case
if (defined($constantName)) {
return null;
}
2018-10-12 05:03:37 +00:00
// is uppercase, all good
if ($constantName === $uppercasedConstantName) {
return null;
2018-10-12 05:03:37 +00:00
}
$node->name = new FullyQualified($uppercasedConstantName);
2018-10-12 05:03:37 +00:00
return $node;
2018-10-12 05:03:37 +00:00
}
}