-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
175 additions
and
45 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace EchoLabs\Prism\Rectors; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class ReorderMethodsRector extends AbstractRector | ||
{ | ||
#[\Override] | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
#[\Override] | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$methods = $node->getMethods(); | ||
|
||
if (count($methods) <= 1) { | ||
return null; | ||
} | ||
|
||
$reorderedMethods = $this->reorderMethods($methods); | ||
|
||
if ($methods === $reorderedMethods) { | ||
return null; | ||
} | ||
|
||
$node->stmts = array_merge( | ||
array_filter($node->stmts, fn ($stmt): bool => ! $stmt instanceof ClassMethod), | ||
$reorderedMethods | ||
); | ||
|
||
return $node; | ||
} | ||
|
||
#[\Override] | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Reorders class methods: magic methods first, then public, protected, and private.', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
private function privateMethod() | ||
{ | ||
} | ||
public function publicMethod() | ||
{ | ||
} | ||
protected function protectedMethod() | ||
{ | ||
} | ||
public function __construct() | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
public function publicMethod() | ||
{ | ||
} | ||
protected function protectedMethod() | ||
{ | ||
} | ||
private function privateMethod() | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
private function reorderMethods(array $methods): array | ||
Check failure on line 99 in src/Rectors/ReorderMethodsRector.php GitHub Actions / phpstan
|
||
{ | ||
usort($methods, fn (ClassMethod $a, ClassMethod $b): int => $this->getMethodWeight($a) <=> $this->getMethodWeight($b)); | ||
|
||
return $methods; | ||
} | ||
|
||
private function getMethodWeight(ClassMethod $method): int | ||
{ | ||
if ($this->isMagicMethod($method)) { | ||
return 0; | ||
} | ||
|
||
if ($method->isPublic()) { | ||
return 1; | ||
} | ||
|
||
if ($method->isProtected()) { | ||
return 2; | ||
} | ||
|
||
return 3; // private | ||
} | ||
|
||
private function isMagicMethod(ClassMethod $method): bool | ||
{ | ||
return str_starts_with($method->name->toString(), '__'); | ||
} | ||
} |