-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Rector to commit c4dd05e2e3ae6359ff4eb57ce7d7bd7619da139d
rectorphp/rector-src@c4dd05e [CodeQuality] Add StaticToSelfStaticMethodCallOnFinalClassRector (#5621)
- Loading branch information
1 parent
53742c3
commit 6ef7f81
Showing
9 changed files
with
114 additions
and
9 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
100 changes: 100 additions & 0 deletions
100
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,100 @@ | ||
<?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; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.