1111use PhpParser \Node \Expr \Variable ;
1212use PhpParser \Node \Identifier ;
1313use PhpParser \Node \Scalar \Int_ ;
14+ use PhpParser \Node \Stmt \ClassMethod ;
1415use PhpParser \Node \Stmt \Expression ;
1516use PHPStan \Type \ObjectType ;
16- use Rector \PHPStan \ ScopeFetcher ;
17+ use Rector \PhpParser \ Node \ BetterNodeFinder ;
1718use Rector \PHPUnit \Enum \PHPUnitClassName ;
19+ use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
1820use Rector \Rector \AbstractRector ;
1921use Rector \ValueObject \MethodName ;
2022use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2527 */
2628final class DecorateWillReturnMapWithExpectsMockRector extends AbstractRector
2729{
30+ public function __construct (
31+ private readonly TestsNodeAnalyzer $ testsNodeAnalyzer ,
32+ private readonly BetterNodeFinder $ betterNodeFinder ,
33+ ) {
34+ }
35+
2836 public function getRuleDefinition (): RuleDefinition
2937 {
3038 return new RuleDefinition ('Decorate willReturnMap() calls with expects on the mock object if missing ' , [
@@ -78,54 +86,76 @@ protected function setUp(): void
7886
7987 public function getNodeTypes (): array
8088 {
81- return [Expression ::class];
89+ return [ClassMethod ::class];
8290 }
8391
8492 /**
85- * @param Expression $node
86- * @return Expression |null
93+ * @param ClassMethod $node
94+ * @return ClassMethod |null
8795 */
8896 public function refactor (Node $ node )
8997 {
90- if (! $ node ->expr instanceof MethodCall) {
98+ // allowed as can be flexible
99+ if ($ this ->isName ($ node , MethodName::SET_UP )) {
91100 return null ;
92101 }
93102
94- $ methodCall = $ node -> expr ;
95- if (! $ this ->isName ( $ methodCall -> name , ' willReturnMap ' )) {
103+ // only decorate inside test methods, skip helpers and other non-test methods
104+ if (! $ this ->testsNodeAnalyzer -> isTestClassMethod ( $ node )) {
96105 return null ;
97106 }
98107
99- $ scope = ScopeFetcher :: fetch ( $ node ) ;
108+ $ hasChanged = false ;
100109
101- // allowed as can be flexible
102- if ($ scope ->getFunctionName () === MethodName::SET_UP ) {
110+ /** @var Expression[] $expressions */
111+ $ expressions = $ this ->betterNodeFinder ->findInstancesOf ($ node , [Expression::class]);
112+ foreach ($ expressions as $ expression ) {
113+ if ($ this ->refactorExpression ($ expression )) {
114+ $ hasChanged = true ;
115+ }
116+ }
117+
118+ if (! $ hasChanged ) {
103119 return null ;
104120 }
105121
122+ return $ node ;
123+ }
124+
125+ private function refactorExpression (Expression $ expression ): bool
126+ {
127+ if (! $ expression ->expr instanceof MethodCall) {
128+ return false ;
129+ }
130+
131+ $ methodCall = $ expression ->expr ;
132+ if (! $ this ->isName ($ methodCall ->name , 'willReturnMap ' )) {
133+ return false ;
134+ }
135+
106136 $ topmostCall = $ this ->resolveTopmostCall ($ methodCall );
107137
108138 // already covered
109139 if ($ this ->isName ($ topmostCall ->name , 'expects ' )) {
110- return null ;
140+ return false ;
111141 }
112142
113143 if (! $ this ->isObjectType ($ topmostCall ->var , new ObjectType (PHPUnitClassName::MOCK_OBJECT ))) {
114- return null ;
144+ return false ;
115145 }
116146
117147 if ($ methodCall ->isFirstClassCallable ()) {
118- return null ;
148+ return false ;
119149 }
120150
121151 // count values in will map arg
122152 $ willReturnMapArg = $ methodCall ->getArgs ()[0 ] ?? null ;
123153 if (! $ willReturnMapArg instanceof Arg) {
124- return null ;
154+ return false ;
125155 }
126156
127157 if (! $ willReturnMapArg ->value instanceof Array_) {
128- return null ;
158+ return false ;
129159 }
130160
131161 $ array = $ willReturnMapArg ->value ;
@@ -141,7 +171,7 @@ public function refactor(Node $node)
141171 ))]
142172 );
143173
144- return $ node ;
174+ return true ;
145175 }
146176
147177 private function resolveTopmostCall (MethodCall $ methodCall ): MethodCall
0 commit comments