-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathRenameMethodRector.php
210 lines (208 loc) · 8.35 KB
/
RenameMethodRector.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
declare (strict_types=1);
namespace Rector\Renaming\Rector\MethodCall;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeManipulator\ClassManipulator;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Renaming\Contract\MethodCallRenameInterface;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202501\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\RenameMethodRectorTest
*/
final class RenameMethodRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @readonly
*/
private ClassManipulator $classManipulator;
/**
* @readonly
*/
private ReflectionResolver $reflectionResolver;
/**
* @readonly
*/
private ReflectionProvider $reflectionProvider;
/**
* @var MethodCallRenameInterface[]
*/
private array $methodCallRenames = [];
public function __construct(ClassManipulator $classManipulator, ReflectionResolver $reflectionResolver, ReflectionProvider $reflectionProvider)
{
$this->classManipulator = $classManipulator;
$this->reflectionResolver = $reflectionResolver;
$this->reflectionProvider = $reflectionProvider;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Turns method names to new ones.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
$someObject = new SomeExampleClass;
$someObject->oldMethod();
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$someObject = new SomeExampleClass;
$someObject->newMethod();
CODE_SAMPLE
, [new MethodCallRename('SomeExampleClass', 'oldMethod', 'newMethod')])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [MethodCall::class, NullsafeMethodCall::class, StaticCall::class, Class_::class, Interface_::class];
}
/**
* @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_ $node
*/
public function refactor(Node $node) : ?Node
{
$scope = ScopeFetcher::fetch($node);
if ($node instanceof Class_ || $node instanceof Interface_) {
return $this->refactorClass($node, $scope);
}
return $this->refactorMethodCallAndStaticCall($node);
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
Assert::allIsAOf($configuration, MethodCallRenameInterface::class);
$this->methodCallRenames = $configuration;
}
/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall|\PhpParser\Node\Expr\StaticCall $call
*/
private function shouldSkipClassMethod($call, MethodCallRenameInterface $methodCallRename) : bool
{
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($call);
if (!$classReflection instanceof ClassReflection) {
return \false;
}
$targetClass = $methodCallRename->getClass();
if (!$this->reflectionProvider->hasClass($targetClass)) {
return \false;
}
$targetClassReflection = $this->reflectionProvider->getClass($targetClass);
if ($classReflection->getName() === $targetClassReflection->getName()) {
return \false;
}
// different with configured ClassLike source? it is a child, which may has old and new exists
if (!$classReflection->hasMethod($methodCallRename->getOldMethod())) {
return \false;
}
return $classReflection->hasMethod($methodCallRename->getNewMethod());
}
/**
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
*/
private function hasClassNewClassMethod($classOrInterface, MethodCallRenameInterface $methodCallRename) : bool
{
return (bool) $classOrInterface->getMethod($methodCallRename->getNewMethod());
}
private function shouldKeepForParentInterface(MethodCallRenameInterface $methodCallRename, ?ClassReflection $classReflection) : bool
{
if (!$classReflection instanceof ClassReflection) {
return \false;
}
// interface can change current method, as parent contract is still valid
if (!$classReflection->isInterface()) {
return \false;
}
return $this->classManipulator->hasParentMethodOrInterface($methodCallRename->getObjectType(), $methodCallRename->getOldMethod());
}
/**
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
* @return \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_|null
*/
private function refactorClass($classOrInterface, Scope $scope)
{
if (!$scope->isInClass()) {
return null;
}
$classReflection = $scope->getClassReflection();
$hasChanged = \false;
foreach ($classOrInterface->getMethods() as $classMethod) {
$methodName = $this->getName($classMethod->name);
if ($methodName === null) {
continue;
}
foreach ($this->methodCallRenames as $methodCallRename) {
if ($this->shouldSkipRename($methodName, $classMethod, $methodCallRename, $classOrInterface, $classReflection)) {
continue;
}
$classMethod->name = new Identifier($methodCallRename->getNewMethod());
$hasChanged = \true;
continue 2;
}
}
if ($hasChanged) {
return $classOrInterface;
}
return null;
}
/**
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
*/
private function shouldSkipRename(string $methodName, ClassMethod $classMethod, MethodCallRenameInterface $methodCallRename, $classOrInterface, ?ClassReflection $classReflection) : bool
{
if (!$this->nodeNameResolver->isStringName($methodName, $methodCallRename->getOldMethod())) {
return \true;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($classMethod, $methodCallRename->getObjectType())) {
return \true;
}
if ($this->shouldKeepForParentInterface($methodCallRename, $classReflection)) {
return \true;
}
return $this->hasClassNewClassMethod($classOrInterface, $methodCallRename);
}
/**
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall $call
* @return \PhpParser\Node\Expr\ArrayDimFetch|null|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\NullsafeMethodCall
*/
private function refactorMethodCallAndStaticCall($call)
{
$callName = $this->getName($call->name);
if ($callName === null) {
return null;
}
foreach ($this->methodCallRenames as $methodCallRename) {
if (!$this->nodeNameResolver->isStringName($callName, $methodCallRename->getOldMethod())) {
continue;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($call, $methodCallRename->getObjectType())) {
continue;
}
if ($this->shouldSkipClassMethod($call, $methodCallRename)) {
continue;
}
$call->name = new Identifier($methodCallRename->getNewMethod());
if ($methodCallRename instanceof MethodCallRenameWithArrayKey) {
return new ArrayDimFetch($call, BuilderHelpers::normalizeValue($methodCallRename->getArrayKey()));
}
return $call;
}
return null;
}
}