Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/coding-style/coding-style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions config/set/php/php53.yaml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 23 additions & 2 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 499 Rectors Overview
# All 500 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 31 additions & 0 deletions rules/coding-style/src/ValueObject/ObjectMagicMethods.php
Original file line number Diff line number Diff line change
@@ -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',
];
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

?>
Original file line number Diff line number Diff line change
@@ -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(){}
}

?>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down