13
13
use PhpParser \Node \Stmt \Class_ ;
14
14
use PhpParser \Node \Stmt \ClassMethod ;
15
15
use PhpParser \Node \Stmt \Interface_ ;
16
+ use PhpParser \Node \Stmt \Trait_ ;
16
17
use PHPStan \Analyser \Scope ;
17
18
use PHPStan \Reflection \ClassReflection ;
18
19
use PHPStan \Reflection \ReflectionProvider ;
@@ -71,15 +72,15 @@ public function getRuleDefinition() : RuleDefinition
71
72
*/
72
73
public function getNodeTypes () : array
73
74
{
74
- return [MethodCall::class, NullsafeMethodCall::class, StaticCall::class, Class_::class, Interface_::class];
75
+ return [MethodCall::class, NullsafeMethodCall::class, StaticCall::class, Class_::class, Trait_::class, Interface_::class];
75
76
}
76
77
/**
77
- * @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_ $node
78
+ * @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_|Trait_ $node
78
79
*/
79
80
public function refactor (Node $ node ) : ?Node
80
81
{
81
82
$ scope = ScopeFetcher::fetch ($ node );
82
- if ($ node instanceof Class_ || $ node instanceof Interface_) {
83
+ if ($ node instanceof Class_ || $ node instanceof Trait_ || $ node instanceof Interface_) {
83
84
return $ this ->refactorClass ($ node , $ scope );
84
85
}
85
86
return $ this ->refactorMethodCallAndStaticCall ($ node );
@@ -116,7 +117,7 @@ private function shouldSkipClassMethod($call, MethodCallRenameInterface $methodC
116
117
return $ classReflection ->hasMethod ($ methodCallRename ->getNewMethod ());
117
118
}
118
119
/**
119
- * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
120
+ * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Trait_|\PhpParser\Node\Stmt\ Interface_ $classOrInterface
120
121
*/
121
122
private function hasClassNewClassMethod ($ classOrInterface , MethodCallRenameInterface $ methodCallRename ) : bool
122
123
{
@@ -134,14 +135,11 @@ private function shouldKeepForParentInterface(MethodCallRenameInterface $methodC
134
135
return $ this ->classManipulator ->hasParentMethodOrInterface ($ methodCallRename ->getObjectType (), $ methodCallRename ->getOldMethod ());
135
136
}
136
137
/**
137
- * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
138
- * @return \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_|null
138
+ * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Trait_|\PhpParser\Node\Stmt\ Interface_ $classOrInterface
139
+ * @return \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Trait_|\PhpParser\Node\Stmt\ Interface_|null
139
140
*/
140
141
private function refactorClass ($ classOrInterface , Scope $ scope )
141
142
{
142
- if (!$ scope ->isInClass ()) {
143
- return null ;
144
- }
145
143
$ classReflection = $ scope ->getClassReflection ();
146
144
$ hasChanged = \false;
147
145
foreach ($ classOrInterface ->getMethods () as $ classMethod ) {
@@ -164,13 +162,16 @@ private function refactorClass($classOrInterface, Scope $scope)
164
162
return null ;
165
163
}
166
164
/**
167
- * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
165
+ * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Trait_|\PhpParser\Node\Stmt\ Interface_ $classOrInterface
168
166
*/
169
167
private function shouldSkipRename (string $ methodName , ClassMethod $ classMethod , MethodCallRenameInterface $ methodCallRename , $ classOrInterface , ?ClassReflection $ classReflection ) : bool
170
168
{
171
169
if (!$ this ->nodeNameResolver ->isStringName ($ methodName , $ methodCallRename ->getOldMethod ())) {
172
170
return \true;
173
171
}
172
+ if (!$ classReflection instanceof ClassReflection && $ classOrInterface instanceof Trait_) {
173
+ return $ this ->hasClassNewClassMethod ($ classOrInterface , $ methodCallRename );
174
+ }
174
175
if (!$ this ->nodeTypeResolver ->isMethodStaticCallOrClassMethodObjectType ($ classMethod , $ methodCallRename ->getObjectType ())) {
175
176
return \true;
176
177
}
0 commit comments