-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
LocallyCalledStaticMethodToNonStaticRector.php
212 lines (208 loc) · 7.78 KB
/
LocallyCalledStaticMethodToNonStaticRector.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
211
212
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitor;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector\LocallyCalledStaticMethodToNonStaticRectorTest
*/
final class LocallyCalledStaticMethodToNonStaticRector extends AbstractRector
{
/**
* @readonly
*/
private ClassMethodVisibilityGuard $classMethodVisibilityGuard;
/**
* @readonly
*/
private VisibilityManipulator $visibilityManipulator;
/**
* @readonly
*/
private ReflectionResolver $reflectionResolver;
/**
* @readonly
*/
private ArrayCallableMethodMatcher $arrayCallableMethodMatcher;
public function __construct(ClassMethodVisibilityGuard $classMethodVisibilityGuard, VisibilityManipulator $visibilityManipulator, ReflectionResolver $reflectionResolver, ArrayCallableMethodMatcher $arrayCallableMethodMatcher)
{
$this->classMethodVisibilityGuard = $classMethodVisibilityGuard;
$this->visibilityManipulator = $visibilityManipulator;
$this->reflectionResolver = $reflectionResolver;
$this->arrayCallableMethodMatcher = $arrayCallableMethodMatcher;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change static method and local-only calls to non-static', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
self::someStatic();
}
private static function someStatic()
{
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$this->someStatic();
}
private function someStatic()
{
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Class_
{
$hasChanged = \false;
foreach ($node->getMethods() as $classMethod) {
if (!$classMethod->isPrivate()) {
continue;
}
$changedClassMethod = $this->refactorClassMethod($node, $classMethod);
if ($changedClassMethod instanceof ClassMethod) {
$hasChanged = \true;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
private function refactorClassMethod(Class_ $class, ClassMethod $classMethod) : ?ClassMethod
{
if (!$classMethod->isStatic()) {
return null;
}
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (!$classReflection instanceof ClassReflection) {
return null;
}
if ($this->classMethodVisibilityGuard->isClassMethodVisibilityGuardedByParent($classMethod, $classReflection)) {
return null;
}
if ($this->isClassMethodCalledInAnotherStaticClassMethod($class, $classMethod)) {
return null;
}
// replace all the calls
$classMethodName = $this->getName($classMethod);
$className = $this->getName($class) ?? '';
$shouldSkip = \false;
$this->traverseNodesWithCallable($class->getMethods(), function (Node $node) use(&$shouldSkip, $classMethodName, $className) : ?int {
if (($node instanceof Closure || $node instanceof ArrowFunction) && $node->static) {
$this->traverseNodesWithCallable($node->getStmts(), function (Node $subNode) use(&$shouldSkip, $classMethodName, $className) : ?int {
if (!$subNode instanceof StaticCall) {
return null;
}
if (!$this->isNames($subNode->class, ['self', 'static', $className])) {
return null;
}
if (!$this->isName($subNode->name, $classMethodName)) {
return null;
}
$shouldSkip = \true;
return NodeVisitor::STOP_TRAVERSAL;
});
if ($shouldSkip) {
return NodeVisitor::STOP_TRAVERSAL;
}
return null;
}
return null;
});
if ($shouldSkip) {
return null;
}
$this->traverseNodesWithCallable($class->getMethods(), function (Node $node) use($classMethodName, $className) : ?MethodCall {
if (!$node instanceof StaticCall) {
return null;
}
if (!$this->isNames($node->class, ['self', 'static', $className])) {
return null;
}
if (!$this->isName($node->name, $classMethodName)) {
return null;
}
return new MethodCall(new Variable('this'), $classMethodName, $node->args);
});
// change static calls to non-static ones, but only if in non-static method!!!
$this->visibilityManipulator->makeNonStatic($classMethod);
return $classMethod;
}
/**
* If the static class method is called in another static class method,
* we should keep it to avoid calling $this in static
*/
private function isClassMethodCalledInAnotherStaticClassMethod(Class_ $class, ClassMethod $classMethod) : bool
{
$currentClassNamespacedName = (string) $this->getName($class);
$currentClassMethodName = $this->getName($classMethod);
$isInsideStaticClassMethod = \false;
// check if called stati call somewhere in class, but only in static methods
foreach ($class->getMethods() as $checkedClassMethod) {
// not a problem
if (!$checkedClassMethod->isStatic()) {
continue;
}
$this->traverseNodesWithCallable($checkedClassMethod, function (Node $node) use($currentClassNamespacedName, $currentClassMethodName, &$isInsideStaticClassMethod) : ?int {
if ($node instanceof Array_) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope && $this->arrayCallableMethodMatcher->match($node, $scope, $currentClassMethodName)) {
$isInsideStaticClassMethod = \true;
return NodeVisitor::STOP_TRAVERSAL;
}
}
if (!$node instanceof StaticCall) {
return null;
}
if (!$this->isNames($node->class, ['self', 'static', $currentClassNamespacedName])) {
return null;
}
if (!$this->isName($node->name, $currentClassMethodName)) {
return null;
}
$isInsideStaticClassMethod = \true;
return NodeVisitor::STOP_TRAVERSAL;
});
if ($isInsideStaticClassMethod) {
return $isInsideStaticClassMethod;
}
}
return \false;
}
}