55namespace Rector \PHPUnit \CodeQuality \Rector \Class_ ;
66
77use PhpParser \Node ;
8+ use PhpParser \Node \Attribute ;
9+ use PhpParser \Node \Scalar \String_ ;
810use PhpParser \Node \Stmt \Class_ ;
911use PhpParser \Node \Stmt \ClassMethod ;
1012use PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
13+ use PHPUnit \Framework \Attributes \Depends ;
1114use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfo ;
1215use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
16+ use Rector \Doctrine \NodeAnalyzer \AttrinationFinder ;
1317use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
1418use Rector \Rector \AbstractRector ;
1519use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
@@ -23,6 +27,7 @@ final class AddParamTypeFromDependsRector extends AbstractRector
2327 public function __construct (
2428 private readonly TestsNodeAnalyzer $ testsNodeAnalyzer ,
2529 private readonly PhpDocInfoFactory $ phpDocInfoFactory ,
30+ private readonly AttrinationFinder $ attrinationFinder
2631 ) {
2732 }
2833
@@ -128,6 +133,29 @@ public function refactor(Node $node): ?Node
128133
129134 private function resolveReturnTypeOfDependsMethod (ClassMethod $ classMethod , Class_ $ class ): ?Node
130135 {
136+ $ dependsMethodName = $ this ->resolveDependsAnnotationOrAttributeMethod ($ classMethod );
137+
138+ $ dependsClassMethod = $ class ->getMethod ($ dependsMethodName );
139+
140+ if (! $ dependsClassMethod instanceof ClassMethod) {
141+ return null ;
142+ }
143+
144+ // resolve return type here
145+ return $ dependsClassMethod ->returnType ;
146+ }
147+
148+ private function resolveDependsAnnotationOrAttributeMethod (ClassMethod $ classMethod ): ?string
149+ {
150+ $ dependsAttribute = $ this ->attrinationFinder ->getByOne ($ classMethod , Depends::class);
151+ if ($ dependsAttribute instanceof Attribute) {
152+ $ firstArg = $ dependsAttribute ->args [0 ];
153+ if ($ firstArg ->value instanceof String_) {
154+ $ dependsMethodName = $ firstArg ->value ->value ;
155+ return trim ($ dependsMethodName , '() ' );
156+ }
157+ }
158+
131159 $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNode ($ classMethod );
132160 if (! $ phpDocInfo instanceof PhpDocInfo) {
133161 return null ;
@@ -139,15 +167,6 @@ private function resolveReturnTypeOfDependsMethod(ClassMethod $classMethod, Clas
139167 }
140168
141169 $ dependsMethodName = (string ) $ dependsTagValueNode ->value ;
142- $ dependsMethodName = trim ($ dependsMethodName , '() ' );
143-
144- $ dependsClassMethod = $ class ->getMethod ($ dependsMethodName );
145-
146- if (! $ dependsClassMethod instanceof ClassMethod) {
147- return null ;
148- }
149-
150- // resolve return type here
151- return $ dependsClassMethod ->returnType ;
170+ return trim ($ dependsMethodName , '() ' );
152171 }
153172}
0 commit comments