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,26 @@ 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+ $ assign ->expr = new New_ (new FullyQualified ($ doctrineClass ));
140+ $ objectVariable = $ assign ->var ;
141+
142+ $ setterExpressions = $ this ->createEntitySetterExpressions ($ definedGettersArg ->value , $ objectVariable );
143+
144+ return array_merge ([$ node ], $ setterExpressions );
145+ }
146+
147+ $ shortClassName = Strings::after ($ doctrineClass , '\\' , -1 );
148+ $ objectVariable = new Variable (lcfirst ((string ) $ shortClassName ));
149+
150+ $ new = new New_ (new FullyQualified ($ doctrineClass ));
151+ $ assign = new Assign ($ objectVariable , $ new );
130152
131153 $ setterExpressions = $ this ->createEntitySetterExpressions ($ definedGettersArg ->value , $ objectVariable );
132154
133- return array_merge ([$ node ], $ setterExpressions );
155+ $ node ->expr = $ objectVariable ;
156+ return array_merge ([new Expression ($ assign )], $ setterExpressions , [$ node ]);
157+
134158 }
135159
136160 /**
0 commit comments