33declare (strict_types=1 );
44namespace Rector \PHPUnit \CodeQuality \Rector \Expression ;
55
6- use PhpParser \ Node \ Stmt \ Class_ ;
6+ use RectorPrefix202602 \ Nette \ Utils \ Strings ;
77use PhpParser \Node ;
88use PhpParser \Node \Arg ;
99use PhpParser \Node \Expr ;
1010use PhpParser \Node \Expr \Array_ ;
1111use PhpParser \Node \Expr \Assign ;
1212use PhpParser \Node \Expr \MethodCall ;
1313use PhpParser \Node \Expr \New_ ;
14+ use PhpParser \Node \Expr \Variable ;
1415use PhpParser \Node \Name \FullyQualified ;
16+ use PhpParser \Node \Stmt ;
17+ use PhpParser \Node \Stmt \Class_ ;
1518use PhpParser \Node \Stmt \Expression ;
19+ use PhpParser \Node \Stmt \Return_ ;
1620use PHPStan \Reflection \ReflectionProvider ;
1721use Rector \Doctrine \NodeAnalyzer \DoctrineEntityDetector ;
1822use Rector \PhpParser \AstResolver ;
2125use Rector \Rector \AbstractRector ;
2226use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2327use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
28+ use RectorPrefix202602 \Webmozart \Assert \Assert ;
2429/**
2530 * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\ConfiguredMockEntityToSetterObjectRector\ConfiguredMockEntityToSetterObjectRectorTest
2631 */
@@ -90,26 +95,33 @@ public function test()
9095 */
9196 public function getNodeTypes (): array
9297 {
93- return [Expression::class];
98+ return [Expression::class, Return_::class ];
9499 }
95100 /**
96- * @param Expression $node
97- * @return Expression []|null
101+ * @param Expression|Return_ $node
102+ * @return Stmt []|null
98103 */
99104 public function refactor (Node $ node ): ?array
100105 {
101106 if (!$ this ->testsNodeAnalyzer ->isInTestClass ($ node )) {
102107 return null ;
103108 }
104- if (!$ node ->expr instanceof Assign) {
105- return null ;
106- }
107- $ assign = $ node ->expr ;
108- if (!$ assign ->expr instanceof MethodCall) {
109+ $ assign = null ;
110+ if ($ node instanceof Return_) {
111+ if ($ node ->expr instanceof MethodCall) {
112+ $ methodCall = $ node ->expr ;
113+ } else {
114+ return null ;
115+ }
116+ } elseif ($ node ->expr instanceof Assign) {
117+ $ assign = $ node ->expr ;
118+ if (!$ assign ->expr instanceof MethodCall) {
119+ return null ;
120+ }
121+ $ methodCall = $ assign ->expr ;
122+ } else {
109123 return null ;
110124 }
111- $ objectVariable = $ assign ->var ;
112- $ methodCall = $ assign ->expr ;
113125 if (!$ this ->isName ($ methodCall ->name , 'createConfiguredMock ' )) {
114126 return null ;
115127 }
@@ -125,9 +137,11 @@ public function refactor(Node $node): ?array
125137 if (!$ definedGettersArg ->value instanceof Array_) {
126138 return null ;
127139 }
128- $ assign ->expr = new New_ (new FullyQualified ($ doctrineClass ));
129- $ setterExpressions = $ this ->createEntitySetterExpressions ($ definedGettersArg ->value , $ objectVariable );
130- return array_merge ([$ node ], $ setterExpressions );
140+ if ($ node instanceof Expression) {
141+ Assert::isInstanceOf ($ assign , Assign::class);
142+ return $ this ->createForAssign ($ doctrineClass , $ assign , $ definedGettersArg ->value , $ node );
143+ }
144+ return $ this ->createForReturn ($ doctrineClass , $ definedGettersArg ->value , $ node );
131145 }
132146 /**
133147 * @return Expression[]
@@ -162,6 +176,10 @@ private function matchDoctrineClassName(Expr $expr): ?string
162176 if (!$ this ->reflectionProvider ->hasClass ($ mockedClassValue )) {
163177 return null ;
164178 }
179+ $ classReflection = $ this ->reflectionProvider ->getClass ($ mockedClassValue );
180+ if ($ classReflection ->isInterface () || $ classReflection ->isAbstract ()) {
181+ return null ;
182+ }
165183 $ mockedClass = $ this ->astResolver ->resolveClassFromName ($ mockedClassValue );
166184 if (!$ mockedClass instanceof Class_) {
167185 return null ;
@@ -171,4 +189,27 @@ private function matchDoctrineClassName(Expr $expr): ?string
171189 }
172190 return $ mockedClassValue ;
173191 }
192+ /**
193+ * @return Stmt[]
194+ */
195+ private function createForReturn (string $ doctrineClass , Array_ $ array , Return_ $ return ): array
196+ {
197+ $ shortClassName = Strings::after ($ doctrineClass , '\\' , -1 );
198+ $ objectVariable = new Variable (lcfirst ((string ) $ shortClassName ));
199+ $ new = new New_ (new FullyQualified ($ doctrineClass ));
200+ $ assign = new Assign ($ objectVariable , $ new );
201+ $ setterExpressions = $ this ->createEntitySetterExpressions ($ array , $ objectVariable );
202+ $ return ->expr = $ objectVariable ;
203+ return array_merge ([new Expression ($ assign )], $ setterExpressions , [$ return ]);
204+ }
205+ /**
206+ * @return Stmt[]
207+ */
208+ private function createForAssign (string $ doctrineClass , Assign $ assign , Array_ $ definedGettersArray , Expression $ expression ): array
209+ {
210+ $ assign ->expr = new New_ (new FullyQualified ($ doctrineClass ));
211+ $ objectVariable = $ assign ->var ;
212+ $ setterExpressions = $ this ->createEntitySetterExpressions ($ definedGettersArray , $ objectVariable );
213+ return array_merge ([$ expression ], $ setterExpressions );
214+ }
174215}
0 commit comments