forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenameStaticMethodRector.php
108 lines (90 loc) · 3.15 KB
/
RenameStaticMethodRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace Rector\Renaming\Rector\StaticCall;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Renaming\ValueObject\RenameStaticMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Renaming\Rector\StaticCall\RenameStaticMethodRector\RenameStaticMethodRectorTest
*/
final class RenameStaticMethodRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const OLD_TO_NEW_METHODS_BY_CLASSES = 'old_to_new_method_by_classes';
/**
* @var string
*/
private const SOME_CLASS = 'SomeClass';
/**
* @var RenameStaticMethod[]
*/
private $staticMethodRenames = [];
public function getRuleDefinition(): RuleDefinition
{
$renameClassConfiguration = [
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
new RenameStaticMethod(self::SOME_CLASS, 'oldMethod', 'AnotherExampleClass', 'newStaticMethod'),
],
];
$renameMethodConfiguration = [
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
new RenameStaticMethod(self::SOME_CLASS, 'oldMethod', self::SOME_CLASS, 'newStaticMethod'),
],
];
return new RuleDefinition('Turns method names to new ones.', [
new ConfiguredCodeSample(
'SomeClass::oldStaticMethod();',
'AnotherExampleClass::newStaticMethod();',
$renameClassConfiguration
),
new ConfiguredCodeSample(
'SomeClass::oldStaticMethod();',
'SomeClass::newStaticMethod();',
$renameMethodConfiguration
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}
/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->staticMethodRenames as $staticMethodRename) {
if (! $this->isObjectType($node->class, $staticMethodRename->getOldObjectType())) {
continue;
}
if (! $this->isName($node->name, $staticMethodRename->getOldMethod())) {
continue;
}
return $this->rename($node, $staticMethodRename);
}
return null;
}
public function configure(array $configuration): void
{
$this->staticMethodRenames = $configuration[self::OLD_TO_NEW_METHODS_BY_CLASSES] ?? [];
}
private function rename(StaticCall $staticCall, RenameStaticMethod $renameStaticMethod): StaticCall
{
$staticCall->name = new Identifier($renameStaticMethod->getNewMethod());
if ($renameStaticMethod->hasClassChanged()) {
$staticCall->class = new FullyQualified($renameStaticMethod->getNewClass());
}
return $staticCall;
}
}