Merge pull request #3415 from rectorphp/coding-style-under

This commit is contained in:
Tomas Votruba 2020-05-25 17:38:33 +02:00 committed by GitHub
commit 8c83b60aea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 231 additions and 4 deletions

View File

@ -35,3 +35,4 @@ services:
pi: M_PI
Rector\CodingStyle\Rector\Function_\CamelCaseFunctionNamingToUnderscoreRector: null
Rector\CodingStyle\Rector\Use_\SplitGroupedUseImportsRector: null
Rector\CodingStyle\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector: null

View File

@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\Variable;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Core\Util\StaticRectorStrings;
/**
* @see \Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector\UnderscoreToPascalCaseVariableAndPropertyNameRectorTest
*/
final class UnderscoreToPascalCaseVariableAndPropertyNameRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change under_score names to pascalCase', [
new CodeSample(
<<<'PHP'
final class SomeClass
{
public function run($a_b)
{
$some_value = 5;
$this->run($a_b);
}
}
PHP
,
<<<'PHP'
final class SomeClass
{
public function run($aB)
{
$someValue = 5;
$this->run($aB);
}
}
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Variable::class, PropertyProperty::class, PropertyFetch::class, StaticPropertyFetch::class];
}
/**
* @param Variable|PropertyProperty|PropertyFetch|StaticPropertyFetch $node
*/
public function refactor(Node $node): ?Node
{
$nodeName = $this->getName($node);
if ($nodeName === null) {
return null;
}
if (Strings::startsWith($nodeName, '_')) {
return null;
}
if (! Strings::contains($nodeName, '_')) {
return null;
}
$camelCaseName = $this->createPascalName($nodeName, $node);
if ($camelCaseName === 'this') {
return null;
}
$node->name = new Identifier($camelCaseName);
return $node;
}
/**
* @param Variable|PropertyProperty|PropertyFetch|StaticPropertyFetch $node
*/
private function createPascalName(string $nodeName, Node $node): string
{
$camelCaseName = StaticRectorStrings::underscoreToPascalCase($nodeName);
if ($node instanceof StaticPropertyFetch || $node instanceof PropertyProperty) {
return '$' . $camelCaseName;
}
return $camelCaseName;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector\Fixture;
final class SomeClass
{
public function run($a_b)
{
$some_value = 5;
$this->run($a_b);
}
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector\Fixture;
final class SomeClass
{
public function run($aB)
{
$someValue = 5;
$this->run($aB);
}
}
?>

View File

@ -0,0 +1,35 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector\Fixture;
final class PropertyAndStaticProperty
{
public $some_value;
public static $another_value;
public function run()
{
return $this->some_value + self::$another_value;
}
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector\Fixture;
final class PropertyAndStaticProperty
{
public $someValue;
public static $anotherValue;
public function run()
{
return $this->someValue + self::$anotherValue;
}
}
?>

View File

@ -0,0 +1,15 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector\Fixture;
final class SkipFirstUnderscore
{
private $_value;
public function run($_a)
{
$_some_value = 5;
return $this->_value;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector\Fixture;
final class SkipReservedNames
{
public function run($this__)
{
return $this__;
}
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Tests\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector;
use Iterator;
use Rector\CodingStyle\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
final class UnderscoreToPascalCaseVariableAndPropertyNameRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return UnderscoreToPascalCaseVariableAndPropertyNameRector::class;
}
}

View File

@ -98,9 +98,9 @@ PHP
return true;
}
$args_count = count($funcCall->args);
$argsCount = count($funcCall->args);
if ($args_count <= 2) {
if ($argsCount <= 2) {
return true;
}
@ -108,7 +108,7 @@ PHP
return true;
}
if ($args_count === 3) {
if ($argsCount === 3) {
return $funcCall->args[2]->value instanceof Variable;
}

View File

@ -23,7 +23,7 @@ use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see \Rector\MinimalScope\Tests\Rector\Class_\ChangeLocalPropertyToVariableRector\ChangeLocalPropertyToVariableRectorTest
* @see \Rector\Privatization\Tests\Rector\Class_\ChangeLocalPropertyToVariableRector\ChangeLocalPropertyToVariableRectorTest
*/
final class ChangeLocalPropertyToVariableRector extends AbstractRector
{