44
55namespace Rector \PHPUnit \CodeQuality \Rector \Expression ;
66
7+ use Nette \Utils \Strings ;
78use PhpParser \Node ;
89use PhpParser \Node \Arg ;
910use PhpParser \Node \Expr ;
1011use PhpParser \Node \Expr \Array_ ;
1112use PhpParser \Node \Expr \Assign ;
1213use PhpParser \Node \Expr \MethodCall ;
1314use PhpParser \Node \Expr \New_ ;
15+ use PhpParser \Node \Expr \Variable ;
1416use PhpParser \Node \Name \FullyQualified ;
1517use PhpParser \Node \Stmt \Class_ ;
1618use PhpParser \Node \Stmt \Expression ;
19+ use PhpParser \Node \Stmt \Return_ ;
1720use PHPStan \Reflection \ReflectionProvider ;
1821use Rector \Doctrine \NodeAnalyzer \DoctrineEntityDetector ;
1922use Rector \PhpParser \AstResolver ;
@@ -81,31 +84,37 @@ public function test()
8184 */
8285 public function getNodeTypes (): array
8386 {
84- return [Expression::class];
87+ return [Expression::class, Return_::class ];
8588 }
8689
8790 /**
88- * @param Expression $node
89- * @return Expression []|null
91+ * @param Expression|Return_ $node
92+ * @return Node\Stmt []|null
9093 */
9194 public function refactor (Node $ node ): ?array
9295 {
9396 if (! $ this ->testsNodeAnalyzer ->isInTestClass ($ node )) {
9497 return null ;
9598 }
9699
97- if (! $ node ->expr instanceof Assign) {
98- return null ;
99- }
100+ $ assign = null ;
101+ if ($ node instanceof Return_) {
102+ if ($ node ->expr instanceof MethodCall) {
103+ $ methodCall = $ node ->expr ;
104+ } else {
105+ return null ;
106+ }
107+ } elseif ($ node ->expr instanceof Assign) {
108+ $ assign = $ node ->expr ;
109+ if (! $ assign ->expr instanceof MethodCall) {
110+ return null ;
111+ }
100112
101- $ assign = $ node ->expr ;
102- if (! $ assign -> expr instanceof MethodCall) {
113+ $ methodCall = $ assign ->expr ;
114+ } else {
103115 return null ;
104116 }
105117
106- $ objectVariable = $ assign ->var ;
107-
108- $ methodCall = $ assign ->expr ;
109118 if (! $ this ->isName ($ methodCall ->name , 'createConfiguredMock ' )) {
110119 return null ;
111120 }
@@ -126,11 +135,12 @@ public function refactor(Node $node): ?array
126135 return null ;
127136 }
128137
129- $ assign ->expr = new New_ (new FullyQualified ($ doctrineClass ));
138+ if ($ assign instanceof Assign) {
139+ return $ this ->createForAssign ($ doctrineClass , $ assign , $ definedGettersArg ->value , $ node );
140+ }
130141
131- $ setterExpressions = $ this ->createEntitySetterExpressions ( $ definedGettersArg ->value , $ objectVariable );
142+ return $ this ->createForReturn ( $ doctrineClass , $ definedGettersArg ->value , $ node );
132143
133- return array_merge ([$ node ], $ setterExpressions );
134144 }
135145
136146 /**
@@ -191,4 +201,39 @@ private function matchDoctrineClassName(Expr $expr): string|null
191201
192202 return $ mockedClassValue ;
193203 }
204+
205+ /**
206+ * @return Node\Stmt[]
207+ */
208+ private function createForReturn (string $ doctrineClass , Array_ $ array , Return_ $ return ): array
209+ {
210+ $ shortClassName = Strings::after ($ doctrineClass , '\\' , -1 );
211+ $ objectVariable = new Variable (lcfirst ((string ) $ shortClassName ));
212+
213+ $ new = new New_ (new FullyQualified ($ doctrineClass ));
214+ $ assign = new Assign ($ objectVariable , $ new );
215+
216+ $ setterExpressions = $ this ->createEntitySetterExpressions ($ array , $ objectVariable );
217+
218+ $ return ->expr = $ objectVariable ;
219+
220+ return array_merge ([new Expression ($ assign )], $ setterExpressions , [$ return ]);
221+ }
222+
223+ /**
224+ * @return Expression[]
225+ */
226+ private function createForAssign (
227+ string $ doctrineClass ,
228+ Assign $ assign ,
229+ Array_ $ definedGettersArray ,
230+ Expression $ expression
231+ ): array {
232+ $ assign ->expr = new New_ (new FullyQualified ($ doctrineClass ));
233+ $ objectVariable = $ assign ->var ;
234+
235+ $ setterExpressions = $ this ->createEntitySetterExpressions ($ definedGettersArray , $ objectVariable );
236+
237+ return array_merge ([$ expression ], $ setterExpressions );
238+ }
194239}
0 commit comments