mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 03:35:01 +01:00
[CodingStyle] Make UnderscoreToPascalCaseVariableNameRector skip native variables, like _SERVER
This commit is contained in:
parent
d44e814fef
commit
5619e735b3
@ -7,6 +7,7 @@ namespace Rector\CodingStyle\Rector\Variable;
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use Rector\Core\Php\ReservedKeywordAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
@ -17,6 +18,16 @@ use Rector\Core\Util\StaticRectorStrings;
|
||||
*/
|
||||
final class UnderscoreToPascalCaseVariableNameRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var ReservedKeywordAnalyzer
|
||||
*/
|
||||
private $reservedKeywordAnalyzer;
|
||||
|
||||
public function __construct(ReservedKeywordAnalyzer $reservedKeywordAnalyzer)
|
||||
{
|
||||
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change under_score names to pascalCase', [
|
||||
@ -67,6 +78,10 @@ PHP
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->reservedKeywordAnalyzer->isNativeVariable($nodeName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pascalCaseName = StaticRectorStrings::underscoreToPascalCase($nodeName);
|
||||
if ($pascalCaseName === 'this') {
|
||||
return null;
|
||||
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableNameRector\Fixture;
|
||||
|
||||
final class SkipUnderscoredReservedNames
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return $_SERVER['host'];
|
||||
}
|
||||
}
|
@ -81,6 +81,16 @@ final class ReservedKeywordAnalyzer
|
||||
'yield',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const NATIVE_VARIABLE_NAMES = ['_ENV', '_POST', '_GET', '_COOKIE', '_SERVER', '_FILES', '_REQUEST'];
|
||||
|
||||
public function isNativeVariable(string $name): bool
|
||||
{
|
||||
return in_array($name, self::NATIVE_VARIABLE_NAMES, true);
|
||||
}
|
||||
|
||||
public function isReserved(string $keyword): bool
|
||||
{
|
||||
$keyword = strtolower($keyword);
|
||||
|
Loading…
x
Reference in New Issue
Block a user