mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
[Naming] Removing underscore rules, rather handle with coding standards (#5868)
This commit is contained in:
parent
683f8319a8
commit
0ac0e54b02
|
@ -11,8 +11,6 @@ use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector;
|
|||
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector;
|
||||
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector;
|
||||
use Rector\Naming\Rector\Property\MakeBoolPropertyRespectIsHasWasMethodNamingRector;
|
||||
use Rector\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector;
|
||||
use Rector\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
|
@ -25,7 +23,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$services->set(MakeIsserClassMethodNameStartWithIsRector::class);
|
||||
$services->set(RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class);
|
||||
$services->set(MakeBoolPropertyRespectIsHasWasMethodNamingRector::class);
|
||||
$services->set(UnderscoreToCamelCasePropertyNameRector::class);
|
||||
$services->set(UnderscoreToCamelCaseVariableNameRector::class);
|
||||
$services->set(RenameForeachValueVariableToMatchExprVariableRector::class);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# 659 Rules Overview
|
||||
# 656 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -56,7 +56,7 @@
|
|||
|
||||
- [MysqlToMysqli](#mysqltomysqli) (4)
|
||||
|
||||
- [Naming](#naming) (12)
|
||||
- [Naming](#naming) (9)
|
||||
|
||||
- [Nette](#nette) (21)
|
||||
|
||||
|
@ -7068,67 +7068,6 @@ Rename variable to match new ClassType
|
|||
|
||||
<br>
|
||||
|
||||
### UnderscoreToCamelCaseLocalVariableNameRector
|
||||
|
||||
Change under_score local variable names to camelCase
|
||||
|
||||
- class: [`Rector\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector`](../rules/Naming/Rector/Variable/UnderscoreToCamelCaseLocalVariableNameRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
- $some_value = $a_b;
|
||||
+ $someValue = $a_b;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### UnderscoreToCamelCasePropertyNameRector
|
||||
|
||||
Change under_score names to camelCase
|
||||
|
||||
- class: [`Rector\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector`](../rules/Naming/Rector/Property/UnderscoreToCamelCasePropertyNameRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
- public $property_name;
|
||||
+ public $propertyName;
|
||||
|
||||
public function run($a)
|
||||
{
|
||||
- $this->property_name = 5;
|
||||
+ $this->propertyName = 5;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### UnderscoreToCamelCaseVariableNameRector
|
||||
|
||||
Change under_score names to camelCase
|
||||
|
||||
- class: [`Rector\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector`](../rules/Naming/Rector/Variable/UnderscoreToCamelCaseVariableNameRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
- public function run($a_b)
|
||||
+ public function run($aB)
|
||||
{
|
||||
- $some_value = $a_b;
|
||||
+ $someValue = $aB;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## Nette
|
||||
|
||||
### AddNextrasDatePickerToDateControlRector
|
||||
|
|
|
@ -42,7 +42,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
SetList::CODE_QUALITY,
|
||||
SetList::CODE_QUALITY_STRICT,
|
||||
SetList::DEAD_CODE,
|
||||
SetList::DEAD_CODE_STRICT,
|
||||
SetList::NETTE_UTILS_CODE_QUALITY,
|
||||
SetList::PRIVATIZATION,
|
||||
SetList::NAMING,
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
private $some_property;
|
||||
|
||||
private static $some_static_property;
|
||||
|
||||
private $_first_and_multiple_underscore_property;
|
||||
|
||||
private $_underscore;
|
||||
|
||||
public function run()
|
||||
{
|
||||
return
|
||||
$this->some_property
|
||||
+ self::$some_static_property
|
||||
+ $this->_first_and_multiple_underscore_property
|
||||
+ $this->_underscore;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
private $someProperty;
|
||||
|
||||
private static $someStaticProperty;
|
||||
|
||||
private $firstAndMultipleUnderscoreProperty;
|
||||
|
||||
private $underscore;
|
||||
|
||||
public function run()
|
||||
{
|
||||
return
|
||||
$this->someProperty
|
||||
+ self::$someStaticProperty
|
||||
+ $this->firstAndMultipleUnderscoreProperty
|
||||
+ $this->underscore;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector\Fixture;
|
||||
|
||||
class SkipMagicGet
|
||||
{
|
||||
private $underscore_value;
|
||||
|
||||
public function get()
|
||||
{
|
||||
$this->underscore_value = 5;
|
||||
}
|
||||
|
||||
public function __get($key)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector\Fixture;
|
||||
|
||||
class SkipMagicSet
|
||||
{
|
||||
private $underscore_value;
|
||||
|
||||
public function set()
|
||||
{
|
||||
$this->underscore_value = 5;
|
||||
}
|
||||
|
||||
public function __set($key, $value)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class UnderscoreToCamelCasePropertyNameRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return UnderscoreToCamelCasePropertyNameRector::class;
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
<?php
|
||||
|
||||
$_a = 'a';
|
||||
$_some_value = &$_a;
|
||||
-----
|
||||
<?php
|
||||
|
||||
$a = 'a';
|
||||
$someValue = &$a;
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
function run()
|
||||
{
|
||||
$_a = 'a';
|
||||
$_some_value = $_a;
|
||||
(function ($_some_value) use ($_a) {
|
||||
$_some_value;
|
||||
$_a;
|
||||
});
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
function run()
|
||||
{
|
||||
$a = 'a';
|
||||
$someValue = $a;
|
||||
(function ($someValue) use ($a) {
|
||||
$someValue;
|
||||
$a;
|
||||
});
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class AssignNotFromParameter
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$_a = 'a';
|
||||
$_some_value = $_a;
|
||||
$this->run($_some_value);
|
||||
$this->run(function ($_some_value) use ($_a) {
|
||||
$_some_value;
|
||||
$_a;
|
||||
});
|
||||
}
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class AssignNotFromParameter
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$a = 'a';
|
||||
$someValue = $a;
|
||||
$this->run($someValue);
|
||||
$this->run(function ($someValue) use ($a) {
|
||||
$someValue;
|
||||
$a;
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
$_a = 'a';
|
||||
$_some_value = $_a;
|
||||
(function ($_some_value) use ($_a) {
|
||||
$_some_value;
|
||||
$_a;
|
||||
});
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
$a = 'a';
|
||||
$someValue = $a;
|
||||
(function ($someValue) use ($a) {
|
||||
$someValue;
|
||||
$a;
|
||||
});
|
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class DoNotChangeSameParameterNameWithVariableName
|
||||
{
|
||||
public function run(string $a_b)
|
||||
{
|
||||
$a_b = trim($a_b);
|
||||
$a_b_c = $a_b;
|
||||
}
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class DoNotChangeSameParameterNameWithVariableName
|
||||
{
|
||||
public function run(string $a_b)
|
||||
{
|
||||
$a_b = trim($a_b);
|
||||
$aBC = $a_b;
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class FirstUnderscore
|
||||
{
|
||||
public function run($_a)
|
||||
{
|
||||
$_some_value = $_a;
|
||||
}
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class FirstUnderscore
|
||||
{
|
||||
public function run($_a)
|
||||
{
|
||||
$someValue = $_a;
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$some_value = 5;
|
||||
|
||||
$this->run($a_b);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$someValue = 5;
|
||||
|
||||
$this->run($a_b);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class InsideExpression
|
||||
{
|
||||
public function run($_a)
|
||||
{
|
||||
if ($_a) {
|
||||
$_some_value = $_a;
|
||||
}
|
||||
}
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class InsideExpression
|
||||
{
|
||||
public function run($_a)
|
||||
{
|
||||
if ($_a) {
|
||||
$someValue = $_a;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class ParamAssignReturn
|
||||
{
|
||||
public function run($_a)
|
||||
{
|
||||
$_some_value = $_a;
|
||||
return $_some_value;
|
||||
}
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class ParamAssignReturn
|
||||
{
|
||||
public function run($_a)
|
||||
{
|
||||
$someValue = $_a;
|
||||
return $someValue;
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipAssignToClassPropertyFromParameter
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$this->a_b = $a_b;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipFromExpressionWithCheckValueRight
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
if ($a_b === false) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipFromExpressionWithCheckValueYoda
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
if (false === $a_b) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
function execute($a_b)
|
||||
{
|
||||
if (! $a_b) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
function runskip()
|
||||
{
|
||||
$_1 = 'a';
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
function runskipOneCharUnderscore()
|
||||
{
|
||||
$_ = 'a';
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipPassFunctionParameterToClosure
|
||||
{
|
||||
public function run(string $a_b, string $c_d)
|
||||
{
|
||||
function ($a_b) use ($c_d) {
|
||||
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipPropertyVariable
|
||||
{
|
||||
public function run(string $a_b)
|
||||
{
|
||||
$this->{$a_b} = $a_b;
|
||||
$this->$a_b = $a_b;
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipReservedNames
|
||||
{
|
||||
public function run($this__)
|
||||
{
|
||||
return $this__;
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipUnderscoredReservedNames
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
isset($_SESSION);
|
||||
return $_SERVER['host'];
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipUsedInNextOrPrevAssignArrayDimFetch
|
||||
{
|
||||
public function foo()
|
||||
{
|
||||
$foo_bar[] = 4;
|
||||
|
||||
$fooBar = 2;
|
||||
|
||||
$testExample = 5;
|
||||
|
||||
$test_example[] = 7;
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
final class SkipUsedInNextOrPrevAssignVar
|
||||
{
|
||||
public function foo()
|
||||
{
|
||||
$foo_bar = 4;
|
||||
$fooBar = 2;
|
||||
|
||||
$testExample = 5;
|
||||
$test_example = 7;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
trait SomeTrait
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$some_value = 5;
|
||||
|
||||
$this->run($a_b);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$some_value = 5;
|
||||
|
||||
$this->run($some_value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\Fixture;
|
||||
|
||||
trait SomeTrait
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$someValue = 5;
|
||||
|
||||
$this->run($a_b);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$someValue = 5;
|
||||
|
||||
$this->run($someValue);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class UnderscoreToCamelCaseLocalVariableNameRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return UnderscoreToCamelCaseLocalVariableNameRector::class;
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
final class FirstUnderscore
|
||||
{
|
||||
public function run($_a)
|
||||
{
|
||||
$_some_value = $_a;
|
||||
}
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
final class FirstUnderscore
|
||||
{
|
||||
public function run($a)
|
||||
{
|
||||
$someValue = $a;
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$some_value = 5;
|
||||
|
||||
$this->run($a_b);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
public function run($aB)
|
||||
{
|
||||
$someValue = 5;
|
||||
|
||||
$this->run($aB);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
function runskip()
|
||||
{
|
||||
$_1 = 'a';
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
function runskipOneCharUnderscore()
|
||||
{
|
||||
$_ = 'a';
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
final class SkipReservedNames
|
||||
{
|
||||
public function run($this__)
|
||||
{
|
||||
return $this__;
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
final class SkipUnderscoredReservedNames
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
isset($_SESSION);
|
||||
return $_SERVER['host'];
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
final class WithParamDocblock
|
||||
{
|
||||
/**
|
||||
* @param string $x
|
||||
* @param string $y_z
|
||||
*/
|
||||
public function run(string $x, string $y_z)
|
||||
{
|
||||
$_something_a = $x;
|
||||
$_something_b = $y_z;
|
||||
}
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
final class WithParamDocblock
|
||||
{
|
||||
/**
|
||||
* @param string $x
|
||||
* @param string $yZ
|
||||
*/
|
||||
public function run(string $x, string $yZ)
|
||||
{
|
||||
$somethingA = $x;
|
||||
$somethingB = $yZ;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
function() {
|
||||
/**
|
||||
* @param string $x
|
||||
* @param string $y_z
|
||||
*/
|
||||
function run(string $x, string $y_z)
|
||||
{
|
||||
$_something_a = $x;
|
||||
$_something_b = $y_z;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\Fixture;
|
||||
|
||||
function() {
|
||||
/**
|
||||
* @param string $x
|
||||
* @param string $yZ
|
||||
*/
|
||||
function run(string $x, string $yZ)
|
||||
{
|
||||
$somethingA = $x;
|
||||
$somethingB = $yZ;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class UnderscoreToCamelCaseVariableNameRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return UnderscoreToCamelCaseVariableNameRector::class;
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Naming\ExpectedNameResolver;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Core\Util\StaticRectorStrings;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
||||
final class UnderscoreCamelCaseExpectedNameResolver
|
||||
{
|
||||
/**
|
||||
* @var NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
|
||||
public function __construct(NodeNameResolver $nodeNameResolver)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Param|Property $node
|
||||
*/
|
||||
public function resolve(Node $node): ?string
|
||||
{
|
||||
$currentName = $this->nodeNameResolver->getName($node);
|
||||
if ($currentName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return StaticRectorStrings::underscoreToCamelCase($currentName);
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Naming\Guard\PropertyConflictingNameGuard;
|
||||
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use Rector\Naming\ExpectedNameResolver\UnderscoreCamelCaseExpectedNameResolver;
|
||||
use Rector\Naming\PhpArray\ArrayFilter;
|
||||
use Rector\Naming\ValueObject\PropertyRename;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
||||
final class UnderscoreCamelCaseConflictingNameGuard
|
||||
{
|
||||
/**
|
||||
* @var UnderscoreCamelCaseExpectedNameResolver
|
||||
*/
|
||||
private $underscoreCamelCaseExpectedNameResolver;
|
||||
|
||||
/**
|
||||
* @var NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
|
||||
/**
|
||||
* @var ArrayFilter
|
||||
*/
|
||||
private $arrayFilter;
|
||||
|
||||
public function __construct(
|
||||
UnderscoreCamelCaseExpectedNameResolver $underscoreCamelCaseExpectedNameResolver,
|
||||
NodeNameResolver $nodeNameResolver,
|
||||
ArrayFilter $arrayFilter
|
||||
) {
|
||||
$this->underscoreCamelCaseExpectedNameResolver = $underscoreCamelCaseExpectedNameResolver;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->arrayFilter = $arrayFilter;
|
||||
}
|
||||
|
||||
public function isConflicting(PropertyRename $propertyRename): bool
|
||||
{
|
||||
$conflictingPropertyNames = $this->resolve($propertyRename->getClassLike());
|
||||
return in_array($propertyRename->getExpectedName(), $conflictingPropertyNames, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function resolve(ClassLike $classLike): array
|
||||
{
|
||||
$expectedNames = [];
|
||||
foreach ($classLike->getProperties() as $property) {
|
||||
$expectedName = $this->underscoreCamelCaseExpectedNameResolver->resolve($property);
|
||||
if ($expectedName === null) {
|
||||
// fallback to existing name
|
||||
$expectedName = $this->nodeNameResolver->getName($property);
|
||||
}
|
||||
|
||||
$expectedNames[] = $expectedName;
|
||||
}
|
||||
|
||||
return $this->arrayFilter->filterWithAtLeastTwoOccurences($expectedNames);
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Naming\PropertyRenamer;
|
||||
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Naming\Guard\PropertyConflictingNameGuard\UnderscoreCamelCaseConflictingNameGuard;
|
||||
use Rector\Naming\ValueObject\PropertyRename;
|
||||
|
||||
final class UnderscoreCamelCasePropertyRenamer
|
||||
{
|
||||
/**
|
||||
* @var UnderscoreCamelCaseConflictingNameGuard
|
||||
*/
|
||||
private $underscoreCamelCaseConflictingNameGuard;
|
||||
|
||||
/**
|
||||
* @var PropertyRenamer
|
||||
*/
|
||||
private $propertyRenamer;
|
||||
|
||||
public function __construct(
|
||||
UnderscoreCamelCaseConflictingNameGuard $underscoreCamelCaseConflictingNameGuard,
|
||||
PropertyRenamer $propertyRenamer
|
||||
) {
|
||||
$this->underscoreCamelCaseConflictingNameGuard = $underscoreCamelCaseConflictingNameGuard;
|
||||
$this->propertyRenamer = $propertyRenamer;
|
||||
}
|
||||
|
||||
public function rename(PropertyRename $propertyRename): ?Property
|
||||
{
|
||||
if ($this->underscoreCamelCaseConflictingNameGuard->isConflicting($propertyRename)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->propertyRenamer->rename($propertyRename);
|
||||
}
|
||||
}
|
|
@ -1,109 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Naming\Rector\Property;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Naming\ExpectedNameResolver\UnderscoreCamelCaseExpectedNameResolver;
|
||||
use Rector\Naming\PropertyRenamer\UnderscoreCamelCasePropertyRenamer;
|
||||
use Rector\Naming\ValueObject\PropertyRename;
|
||||
use Rector\Naming\ValueObjectFactory\PropertyRenameFactory;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector\UnderscoreToCamelCasePropertyNameRectorTest
|
||||
*/
|
||||
final class UnderscoreToCamelCasePropertyNameRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var PropertyRenameFactory
|
||||
*/
|
||||
private $propertyRenameFactory;
|
||||
|
||||
/**
|
||||
* @var UnderscoreCamelCasePropertyRenamer
|
||||
*/
|
||||
private $underscoreCamelCasePropertyRenamer;
|
||||
|
||||
/**
|
||||
* @var UnderscoreCamelCaseExpectedNameResolver
|
||||
*/
|
||||
private $underscoreCamelCaseExpectedNameResolver;
|
||||
|
||||
public function __construct(
|
||||
UnderscoreCamelCasePropertyRenamer $underscoreCamelCasePropertyRenamer,
|
||||
PropertyRenameFactory $propertyRenameFactory,
|
||||
UnderscoreCamelCaseExpectedNameResolver $underscoreCamelCaseExpectedNameResolver
|
||||
) {
|
||||
$this->underscoreCamelCasePropertyRenamer = $underscoreCamelCasePropertyRenamer;
|
||||
$this->propertyRenameFactory = $propertyRenameFactory;
|
||||
$this->underscoreCamelCaseExpectedNameResolver = $underscoreCamelCaseExpectedNameResolver;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Change under_score names to camelCase', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public $property_name;
|
||||
|
||||
public function run($a)
|
||||
{
|
||||
$this->property_name = 5;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public $propertyName;
|
||||
|
||||
public function run($a)
|
||||
{
|
||||
$this->propertyName = 5;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Property::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Property $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$propertyName = $this->getName($node);
|
||||
if (! Strings::contains($propertyName, '_')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$expectedPropertyName = $this->underscoreCamelCaseExpectedNameResolver->resolve($node);
|
||||
if ($expectedPropertyName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$propertyRename = $this->propertyRenameFactory->createFromExpectedName($node, $expectedPropertyName);
|
||||
if (! $propertyRename instanceof PropertyRename) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->underscoreCamelCasePropertyRenamer->rename($propertyRename);
|
||||
}
|
||||
}
|
|
@ -1,214 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Naming\Rector\Variable;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use Rector\Core\Php\ReservedKeywordAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\Util\StaticRectorStrings;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector\UnderscoreToCamelCaseLocalVariableNameRectorTest
|
||||
*/
|
||||
final class UnderscoreToCamelCaseLocalVariableNameRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var ReservedKeywordAnalyzer
|
||||
*/
|
||||
private $reservedKeywordAnalyzer;
|
||||
|
||||
public function __construct(ReservedKeywordAnalyzer $reservedKeywordAnalyzer)
|
||||
{
|
||||
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Change under_score local variable names to camelCase',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$some_value = $a_b;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$someValue = $a_b;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Variable::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Variable $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$nodeName = $this->getName($node);
|
||||
if ($nodeName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! Strings::contains($nodeName, '_')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->reservedKeywordAnalyzer->isNativeVariable($nodeName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$camelCaseName = StaticRectorStrings::underscoreToCamelCase($nodeName);
|
||||
if ($this->isReserved($camelCaseName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentNode instanceof Expr && $this->isFoundInParentNode($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (($parentNode instanceof Arg || $parentNode instanceof Param || $parentNode instanceof Stmt)
|
||||
&& $this->isFoundInParentNode($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isUsedNextPreviousAssignVar($node, $camelCaseName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->name = $camelCaseName;
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function isUsedNextPreviousAssignVar(Node $variable, string $camelCaseName): bool
|
||||
{
|
||||
$parent = $variable->getAttribute(AttributeKey::PARENT_NODE);
|
||||
|
||||
if ($parent instanceof ArrayDimFetch) {
|
||||
return $this->isUsedNextPreviousAssignVar($parent, $camelCaseName);
|
||||
}
|
||||
|
||||
if (! $parent instanceof Assign) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($parent->var !== $variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$variableMethodNode = $variable->getAttribute(AttributeKey::METHOD_NODE);
|
||||
if (! $variableMethodNode instanceof Node) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$usedInNext = (bool) $this->betterNodeFinder->findFirstNext($variable, function (Node $node) use (
|
||||
$variableMethodNode,
|
||||
$camelCaseName
|
||||
): bool {
|
||||
return $this->hasEqualVariable($node, $variableMethodNode, $camelCaseName);
|
||||
});
|
||||
|
||||
if ($usedInNext) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($variable, function (Node $node) use (
|
||||
$variableMethodNode,
|
||||
$camelCaseName
|
||||
): bool {
|
||||
return $this->hasEqualVariable($node, $variableMethodNode, $camelCaseName);
|
||||
});
|
||||
}
|
||||
|
||||
private function hasEqualVariable(Node $node, ?Node $variableMethodNode, string $camelCaseName): bool
|
||||
{
|
||||
if (! $node instanceof Variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$methodNode = $node->getAttribute(AttributeKey::METHOD_NODE);
|
||||
if ($variableMethodNode !== $methodNode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (! $parent instanceof Assign) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isName($parent->var, $camelCaseName);
|
||||
}
|
||||
|
||||
private function isReserved(string $string): bool
|
||||
{
|
||||
if ($string === 'this') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($string === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return is_numeric($string[0]);
|
||||
}
|
||||
|
||||
private function isFoundInParentNode(Variable $variable): bool
|
||||
{
|
||||
/** @var ClassMethod|Function_|null $classMethodOrFunction */
|
||||
$classMethodOrFunction = $this->betterNodeFinder->findParentTypes(
|
||||
$variable,
|
||||
[ClassMethod::class, Function_::class]
|
||||
);
|
||||
|
||||
if ($classMethodOrFunction === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var Param[] $params */
|
||||
$params = $classMethodOrFunction->getParams();
|
||||
|
||||
foreach ($params as $param) {
|
||||
if ($this->nodeNameResolver->areNamesEqual($param->var, $variable)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,162 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Naming\Rector\Variable;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Param;
|
||||
use Rector\Core\Php\ReservedKeywordAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\Util\StaticRectorStrings;
|
||||
use Rector\Naming\ExpectedNameResolver\UnderscoreCamelCaseExpectedNameResolver;
|
||||
use Rector\Naming\ParamRenamer\ParamRenamer;
|
||||
use Rector\Naming\ValueObject\ParamRename;
|
||||
use Rector\Naming\ValueObjectFactory\ParamRenameFactory;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector\UnderscoreToCamelCaseVariableNameRectorTest
|
||||
*/
|
||||
final class UnderscoreToCamelCaseVariableNameRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var ReservedKeywordAnalyzer
|
||||
*/
|
||||
private $reservedKeywordAnalyzer;
|
||||
|
||||
/**
|
||||
* @var ParamRenameFactory
|
||||
*/
|
||||
private $paramRenameFactory;
|
||||
|
||||
/**
|
||||
* @var UnderscoreCamelCaseExpectedNameResolver
|
||||
*/
|
||||
private $underscoreCamelCaseExpectedNameResolver;
|
||||
|
||||
/**
|
||||
* @var ParamRenamer
|
||||
*/
|
||||
private $paramRenamer;
|
||||
|
||||
public function __construct(
|
||||
ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
|
||||
ParamRenameFactory $paramRenameFactory,
|
||||
ParamRenamer $underscoreCamelCaseParamRenamer,
|
||||
UnderscoreCamelCaseExpectedNameResolver $underscoreCamelCaseExpectedNameResolver
|
||||
) {
|
||||
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
|
||||
$this->paramRenameFactory = $paramRenameFactory;
|
||||
$this->underscoreCamelCaseExpectedNameResolver = $underscoreCamelCaseExpectedNameResolver;
|
||||
$this->paramRenamer = $underscoreCamelCaseParamRenamer;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Change under_score names to camelCase', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($a_b)
|
||||
{
|
||||
$some_value = $a_b;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($aB)
|
||||
{
|
||||
$someValue = $aB;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Variable::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Variable $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$nodeName = $this->getName($node);
|
||||
if ($nodeName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! Strings::contains($nodeName, '_')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->reservedKeywordAnalyzer->isNativeVariable($nodeName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$camelCaseName = StaticRectorStrings::underscoreToCamelCase($nodeName);
|
||||
if ($camelCaseName === 'this') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($camelCaseName === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_numeric($camelCaseName[0])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parent instanceof Param) {
|
||||
return $this->renameParam($parent);
|
||||
}
|
||||
|
||||
if ($this->isName($node, $camelCaseName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->name = $camelCaseName;
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function renameParam(Param $param): ?Variable
|
||||
{
|
||||
$resolvedExpectedName = $this->underscoreCamelCaseExpectedNameResolver->resolve($param);
|
||||
if ($resolvedExpectedName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$paramRename = $this->paramRenameFactory->createFromResolvedExpectedName($param, $resolvedExpectedName);
|
||||
if (! $paramRename instanceof ParamRename) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$renamedParam = $this->paramRenamer->rename($paramRename);
|
||||
if (! $renamedParam instanceof Param) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $renamedParam->var instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $renamedParam->var;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user