[CodingStyle] Add RemoveDoubleUnderscoreInMethodNameRector

This commit is contained in:
TomasVotruba 2020-05-31 18:02:17 +02:00
parent 6c428c4bd0
commit 4fb1f982f9
8 changed files with 289 additions and 3 deletions

View File

@ -36,3 +36,5 @@ services:
Rector\CodingStyle\Rector\Function_\CamelCaseFunctionNamingToUnderscoreRector: null
Rector\CodingStyle\Rector\Use_\SplitGroupedUseImportsRector: null
Rector\CodingStyle\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector: null
Rector\CodingStyle\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector: null

View File

@ -1,4 +1,4 @@
# All 499 Rectors Overview
# All 500 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -11,7 +11,7 @@
- [CakePHP](#cakephp) (5)
- [Celebrity](#celebrity) (3)
- [CodeQuality](#codequality) (53)
- [CodingStyle](#codingstyle) (31)
- [CodingStyle](#codingstyle) (32)
- [DeadCode](#deadcode) (40)
- [Doctrine](#doctrine) (16)
- [DoctrineCodeQuality](#doctrinecodequality) (2)
@ -1919,6 +1919,27 @@ services:
<br>
### `RemoveDoubleUnderscoreInMethodNameRector`
- class: [`Rector\CodingStyle\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector`](/../master/rules/coding-style/src/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector.php)
- [test fixtures](/../master/rules/coding-style/tests/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector/Fixture)
Non-magic PHP object methods cannot start with "__"
```diff
class SomeClass
{
- public function __getName($anotherObject)
+ public function getName($anotherObject)
{
- $anotherObject->__getSurname();
+ $anotherObject->getSurname();
}
}
```
<br>
### `RemoveUnusedAliasRector`
- class: [`Rector\CodingStyle\Rector\Use_\RemoveUnusedAliasRector`](/../master/rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php)

View File

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\ClassMethod;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\CodingStyle\ValueObject\ObjectMagicMethods;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
/**
* @sponsor Thanks https://twitter.com/afilina for sponsoring this rule
*
* @see \Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\RemoveDoubleUnderscoreInMethodNameRectorTest
*/
final class RemoveDoubleUnderscoreInMethodNameRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Non-magic PHP object methods cannot start with "__"', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function __getName($anotherObject)
{
$anotherObject->__getSurname();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function getName($anotherObject)
{
$anotherObject->getSurname();
}
}
CODE_SAMPLE
),
]);
}
public function getNodeTypes(): array
{
return [ClassMethod::class, MethodCall::class, StaticCall::class];
}
/**
* @param ClassMethod|MethodCall|StaticCall $node
*/
public function refactor(Node $node): ?Node
{
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}
if (in_array($methodName, ObjectMagicMethods::METHOD_NAMES, true)) {
return null;
}
if (! Strings::match($methodName, '#__(.*?)#')) {
return null;
}
$newName = Strings::substring($methodName, 2);
$node->name = new Identifier($newName);
return $node;
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\ValueObject;
final class ObjectMagicMethods
{
/**
* @var string[]
*/
public const METHOD_NAMES = [
'__call',
'__callStatic',
'__clone',
'__construct',
'__debugInfo',
'__destruct',
'__get',
'__invoke',
'__isset',
'__serialize',
'__set',
'__set_state',
'__sleep',
'__toString',
'__unserialize',
'__unset',
'__wakeup',
];
}

View File

@ -0,0 +1,61 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
final class MethodCalling
{
public function run(MyClass $myClass)
{
$myClass->__doSomething();
$myClass->__construct();
$myClass->__destruct();
$myClass->__call();
$myClass->__callStatic();
$myClass->__get();
$myClass->__set();
$myClass->__isset();
$myClass->__unset();
$myClass->__sleep();
$myClass->__wakeup();
$myClass->__serialize();
$myClass->__unserialize();
$myClass->__toString();
$myClass->__invoke();
$myClass->__set_state();
$myClass->__debugInfo();
}
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
final class MethodCalling
{
public function run(MyClass $myClass)
{
$myClass->doSomething();
$myClass->__construct();
$myClass->__destruct();
$myClass->__call();
$myClass->__callStatic();
$myClass->__get();
$myClass->__set();
$myClass->__isset();
$myClass->__unset();
$myClass->__sleep();
$myClass->__wakeup();
$myClass->__serialize();
$myClass->__unserialize();
$myClass->__toString();
$myClass->__invoke();
$myClass->__set_state();
$myClass->__debugInfo();
}
}
?>

View File

@ -0,0 +1,57 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
class MyClass
{
function __doSomething() {}
function __construct(){}
function __destruct(){}
function __call($name, $value){}
static function __callStatic($name, $value){}
function __get($value){}
function __set($key, $value){}
function __isset($key){}
function __unset($key){}
function __sleep(){}
function __wakeup(){}
function __serialize(){}
function __unserialize(){}
function __toString(){}
function __invoke(){}
function __set_state(){}
function __clone(){}
function __debugInfo(){}
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
class MyClass
{
function doSomething() {}
function __construct(){}
function __destruct(){}
function __call($name, $value){}
static function __callStatic($name, $value){}
function __get($value){}
function __set($key, $value){}
function __isset($key){}
function __unset($key){}
function __sleep(){}
function __wakeup(){}
function __serialize(){}
function __unserialize(){}
function __toString(){}
function __invoke(){}
function __set_state(){}
function __clone(){}
function __debugInfo(){}
}
?>

View File

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

View File

@ -14,7 +14,7 @@ use Rector\Core\RectorDefinition\RectorDefinition;
/**
* @sponsor Thanks https://twitter.com/afilina for sponsoring this rule
*
* @see https://3v4l.org/UJN6H
* @see \Rector\Php53\Rector\Assign\ClearReturnNewByReferenceRector
*/