Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add StaticToSelfStaticMethodCallOnFinalClassRector (#5621)
* [CodeQuality] Add StaticToSelfStaticMethodCallOnFinalClassRector * dynamic test * fix * fix * fix phpstan * ensure args included * fix * skip not a Name * fix
- Loading branch information
1 parent
70fa502
commit c4dd05e
Showing
11 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...tor/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/call_statically.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture; | ||
|
||
final class CallStatically | ||
{ | ||
public function execute() | ||
{ | ||
$a = 1; | ||
static::run($a); | ||
} | ||
|
||
private static function run($a) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture; | ||
|
||
final class CallStatically | ||
{ | ||
public function execute() | ||
{ | ||
$a = 1; | ||
self::run($a); | ||
} | ||
|
||
private static function run($a) | ||
{ | ||
} | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...r/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/skip_already_self.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture; | ||
|
||
final class CallStatically | ||
{ | ||
public function execute() | ||
{ | ||
self::run(); | ||
} | ||
|
||
private static function run() | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...icToSelfStaticMethodCallOnFinalClassRector/Fixture/skip_call_non_existence_method.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture; | ||
|
||
final class SkipCallNonExistenceMethod | ||
{ | ||
public function execute($execute) | ||
{ | ||
static::run(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/skip_dynamic_method.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture; | ||
|
||
final class SkipDynamicMethod | ||
{ | ||
public function execute($execute) | ||
{ | ||
static::{$execute}(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/skip_from_variable.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture; | ||
|
||
final class SkipFromVariable | ||
{ | ||
public function execute($static) | ||
{ | ||
$static::run(); | ||
} | ||
|
||
private function run() | ||
{ | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ctor/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/skip_non_final.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture; | ||
|
||
class SkipNonFinal | ||
{ | ||
public function execute() | ||
{ | ||
static::run(); | ||
} | ||
|
||
private static function run() | ||
{ | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...lass_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/skip_non_static_call.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture; | ||
|
||
final class SkipNonStaticCall | ||
{ | ||
public function execute() | ||
{ | ||
$this->run(); | ||
} | ||
|
||
private function run() | ||
{ | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...StaticMethodCallOnFinalClassRector/StaticToSelfStaticMethodCallOnFinalClassRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class StaticToSelfStaticMethodCallOnFinalClassRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...y/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return RectorConfig::configure() | ||
->withRules([StaticToSelfStaticMethodCallOnFinalClassRector::class]); |
118 changes: 118 additions & 0 deletions
118
rules/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\Enum\ObjectReference; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://3v4l.org/JK1g2#v5.0.0 | ||
* @see \Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\StaticToSelfStaticMethodCallOnFinalClassRectorTest | ||
*/ | ||
final class StaticToSelfStaticMethodCallOnFinalClassRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change `static::methodCall()` to `self::methodCall()` on final class', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function d() | ||
{ | ||
echo static::run(); | ||
} | ||
private static function run() | ||
{ | ||
echo 'test'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function d() | ||
{ | ||
echo self::run(); | ||
} | ||
private static function run() | ||
{ | ||
echo 'test'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Class_ | ||
{ | ||
if (! $node->isFinal()) { | ||
return null; | ||
} | ||
|
||
$hasChanged = false; | ||
|
||
$this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use (&$hasChanged, $node): ?StaticCall { | ||
if (! $subNode instanceof StaticCall) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($subNode->class, ObjectReference::STATIC)) { | ||
return null; | ||
} | ||
|
||
// skip dynamic method | ||
if (! $subNode->name instanceof Identifier) { | ||
return null; | ||
} | ||
|
||
$methodName = (string) $this->getName($subNode->name); | ||
$targetClassMethod = $node->getMethod($methodName); | ||
|
||
// skip call non-existing method from current class to ensure transformation is safe | ||
if (! $targetClassMethod instanceof ClassMethod) { | ||
return null; | ||
} | ||
|
||
// avoid overlapped change | ||
if (! $targetClassMethod->isStatic()) { | ||
return null; | ||
} | ||
|
||
$hasChanged = true; | ||
$subNode->class = new Name('self'); | ||
return $subNode; | ||
}); | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
} |