diff --git a/config/set/coding-style/coding-style.yaml b/config/set/coding-style/coding-style.yaml index 4cc1451990bf..4cbae0ff2a1a 100644 --- a/config/set/coding-style/coding-style.yaml +++ b/config/set/coding-style/coding-style.yaml @@ -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 diff --git a/config/set/php/php53.yaml b/config/set/php/php53.yaml index c9f4edb4ed6f..878af69c323b 100644 --- a/config/set/php/php53.yaml +++ b/config/set/php/php53.yaml @@ -1,3 +1,5 @@ services: Rector\Php53\Rector\Ternary\TernaryToElvisRector: null Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector: null + Rector\Php53\Rector\Assign\ClearReturnNewByReferenceRector: null + Rector\Php53\Rector\Variable\ReplaceHttpServerVarsByServerRector: null diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index c7527539c591..2701ef56368a 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -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:
+### `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(); + } + } +``` + +
+ ### `RemoveUnusedAliasRector` - class: [`Rector\CodingStyle\Rector\Use_\RemoveUnusedAliasRector`](/../master/rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php) diff --git a/rules/coding-style/src/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector.php b/rules/coding-style/src/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector.php new file mode 100644 index 000000000000..d13ea029e609 --- /dev/null +++ b/rules/coding-style/src/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector.php @@ -0,0 +1,80 @@ +__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; + } +} diff --git a/rules/coding-style/src/ValueObject/ObjectMagicMethods.php b/rules/coding-style/src/ValueObject/ObjectMagicMethods.php new file mode 100644 index 000000000000..f9cf3c5352a6 --- /dev/null +++ b/rules/coding-style/src/ValueObject/ObjectMagicMethods.php @@ -0,0 +1,31 @@ +__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(); + } +} + +?> +----- +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(); + } +} + +?> diff --git a/rules/coding-style/tests/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector/Fixture/declaration.php.inc b/rules/coding-style/tests/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector/Fixture/declaration.php.inc new file mode 100644 index 000000000000..7508444e97b7 --- /dev/null +++ b/rules/coding-style/tests/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector/Fixture/declaration.php.inc @@ -0,0 +1,57 @@ + +----- + diff --git a/rules/coding-style/tests/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector/RemoveDoubleUnderscoreInMethodNameRectorTest.php b/rules/coding-style/tests/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector/RemoveDoubleUnderscoreInMethodNameRectorTest.php new file mode 100644 index 000000000000..5ec61aee51d9 --- /dev/null +++ b/rules/coding-style/tests/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector/RemoveDoubleUnderscoreInMethodNameRectorTest.php @@ -0,0 +1,34 @@ +doTestFile($file); + } + + /** + * @return Iterator + */ + public function provideDataForTest(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return RemoveDoubleUnderscoreInMethodNameRector::class; + } +} diff --git a/rules/php53/src/Rector/Assign/ClearReturnNewByReferenceRector.php b/rules/php53/src/Rector/Assign/ClearReturnNewByReferenceRector.php index cd8170300edc..2051cc10cdf3 100644 --- a/rules/php53/src/Rector/Assign/ClearReturnNewByReferenceRector.php +++ b/rules/php53/src/Rector/Assign/ClearReturnNewByReferenceRector.php @@ -14,7 +14,7 @@ /** * @sponsor Thanks https://twitter.com/afilina for sponsoring this rule - + * * @see https://3v4l.org/UJN6H * @see \Rector\Php53\Rector\Assign\ClearReturnNewByReferenceRector */