44
55namespace Rector \PHPUnit \CodeQuality \Rector \Expression ;
66
7+ use Webmozart \Assert \Assert ;
8+ use Nette \Utils \Strings ;
79use PhpParser \Node ;
810use PhpParser \Node \Arg ;
911use PhpParser \Node \Expr ;
1012use PhpParser \Node \Expr \Array_ ;
1113use PhpParser \Node \Expr \Assign ;
1214use PhpParser \Node \Expr \MethodCall ;
1315use PhpParser \Node \Expr \New_ ;
16+ use PhpParser \Node \Expr \Variable ;
1417use PhpParser \Node \Name \FullyQualified ;
18+ use PhpParser \Node \Stmt ;
1519use PhpParser \Node \Stmt \Class_ ;
1620use PhpParser \Node \Stmt \Expression ;
21+ use PhpParser \Node \Stmt \Return_ ;
1722use PHPStan \Reflection \ReflectionProvider ;
1823use Rector \Doctrine \NodeAnalyzer \DoctrineEntityDetector ;
1924use Rector \PhpParser \AstResolver ;
@@ -81,31 +86,37 @@ public function test()
8186 */
8287 public function getNodeTypes (): array
8388 {
84- return [Expression::class];
89+ return [Expression::class, Return_::class ];
8590 }
8691
8792 /**
88- * @param Expression $node
89- * @return Expression []|null
93+ * @param Expression|Return_ $node
94+ * @return Stmt []|null
9095 */
9196 public function refactor (Node $ node ): ?array
9297 {
9398 if (! $ this ->testsNodeAnalyzer ->isInTestClass ($ node )) {
9499 return null ;
95100 }
96101
97- if (! $ node ->expr instanceof Assign) {
98- return null ;
99- }
102+ $ assign = null ;
103+ if ($ node instanceof Return_) {
104+ if ($ node ->expr instanceof MethodCall) {
105+ $ methodCall = $ node ->expr ;
106+ } else {
107+ return null ;
108+ }
109+ } elseif ($ node ->expr instanceof Assign) {
110+ $ assign = $ node ->expr ;
111+ if (! $ assign ->expr instanceof MethodCall) {
112+ return null ;
113+ }
100114
101- $ assign = $ node ->expr ;
102- if (! $ assign -> expr instanceof MethodCall) {
115+ $ methodCall = $ assign ->expr ;
116+ } else {
103117 return null ;
104118 }
105119
106- $ objectVariable = $ assign ->var ;
107-
108- $ methodCall = $ assign ->expr ;
109120 if (! $ this ->isName ($ methodCall ->name , 'createConfiguredMock ' )) {
110121 return null ;
111122 }
@@ -126,11 +137,13 @@ public function refactor(Node $node): ?array
126137 return null ;
127138 }
128139
129- $ assign ->expr = new New_ (new FullyQualified ($ doctrineClass ));
140+ if ($ node instanceof Expression) {
141+ Assert::isInstanceOf ($ assign , Assign::class);
142+ return $ this ->createForAssign ($ doctrineClass , $ assign , $ definedGettersArg ->value , $ node );
143+ }
130144
131- $ setterExpressions = $ this ->createEntitySetterExpressions ( $ definedGettersArg ->value , $ objectVariable );
145+ return $ this ->createForReturn ( $ doctrineClass , $ definedGettersArg ->value , $ node );
132146
133- return array_merge ([$ node ], $ setterExpressions );
134147 }
135148
136149 /**
@@ -191,4 +204,39 @@ private function matchDoctrineClassName(Expr $expr): string|null
191204
192205 return $ mockedClassValue ;
193206 }
207+
208+ /**
209+ * @return Stmt[]
210+ */
211+ private function createForReturn (string $ doctrineClass , Array_ $ array , Return_ $ return ): array
212+ {
213+ $ shortClassName = Strings::after ($ doctrineClass , '\\' , -1 );
214+ $ objectVariable = new Variable (lcfirst ((string ) $ shortClassName ));
215+
216+ $ new = new New_ (new FullyQualified ($ doctrineClass ));
217+ $ assign = new Assign ($ objectVariable , $ new );
218+
219+ $ setterExpressions = $ this ->createEntitySetterExpressions ($ array , $ objectVariable );
220+
221+ $ return ->expr = $ objectVariable ;
222+
223+ return array_merge ([new Expression ($ assign )], $ setterExpressions , [$ return ]);
224+ }
225+
226+ /**
227+ * @return Stmt[]
228+ */
229+ private function createForAssign (
230+ string $ doctrineClass ,
231+ Assign $ assign ,
232+ Array_ $ definedGettersArray ,
233+ Expression $ expression
234+ ): array {
235+ $ assign ->expr = new New_ (new FullyQualified ($ doctrineClass ));
236+ $ objectVariable = $ assign ->var ;
237+
238+ $ setterExpressions = $ this ->createEntitySetterExpressions ($ definedGettersArray , $ objectVariable );
239+
240+ return array_merge ([$ expression ], $ setterExpressions );
241+ }
194242}
0 commit comments