File tree Expand file tree Collapse file tree 2 files changed +54
-2
lines changed
rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture
rules/CodeQuality/Rector/MethodCall Expand file tree Collapse file tree 2 files changed +54
-2
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \PHPUnit \Tests \CodeQuality \Rector \MethodCall \MergeWithCallableAndWillReturnRector \Fixture ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+
7+ final class ReturnVariableUse extends TestCase
8+ {
9+ public function test ()
10+ {
11+ $ anotherValue = 1000 ;
12+
13+ $ this ->createMock ('SomeClass ' )
14+ ->method ('someMethod ' )
15+ ->with ($ this ->callback (function ($ arg ): bool {
16+ return true ;
17+ }))
18+ ->willReturn ($ anotherValue );
19+ }
20+ }
21+
22+ ?>
23+ -----
24+ <?php
25+
26+ namespace Rector \PHPUnit \Tests \CodeQuality \Rector \MethodCall \MergeWithCallableAndWillReturnRector \Fixture ;
27+
28+ use PHPUnit \Framework \TestCase ;
29+
30+ final class ReturnVariableUse extends TestCase
31+ {
32+ public function test ()
33+ {
34+ $ anotherValue = 1000 ;
35+
36+ $ this ->createMock ('SomeClass ' )
37+ ->method ('someMethod ' )
38+ ->willReturnCallback (function ($ arg ) use ($ anotherValue ): int {
39+ return $ anotherValue ;
40+ });
41+ }
42+ }
43+
44+ ?>
Original file line number Diff line number Diff line change 66
77use PhpParser \Node ;
88use PhpParser \Node \Arg ;
9+ use PhpParser \Node \ClosureUse ;
910use PhpParser \Node \Expr \Closure ;
1011use PhpParser \Node \Expr \MethodCall ;
12+ use PhpParser \Node \Expr \Variable ;
1113use PhpParser \Node \Identifier ;
1214use PhpParser \Node \Stmt \Return_ ;
1315use Rector \PhpParser \Node \Value \ValueResolver ;
@@ -141,9 +143,15 @@ public function refactor(Node $node): MethodCall|null
141143 $ parentCaller ->name = new Identifier ('willReturnCallback ' );
142144 $ parentCaller ->args = [new Arg ($ innerClosure )];
143145
146+ if ($ returnedExpr instanceof Variable) {
147+ $ innerClosure ->uses [] = new ClosureUse ($ returnedExpr );
148+ }
149+
144150 $ returnedExprType = $ this ->staticTypeMapper ->mapPhpParserNodePHPStanType ($ returnedExpr );
145- $ returnTypeNode = $ this ->staticTypeMapper ->mapPHPStanTypeToPhpParserNode ($ returnedExprType , TypeKind::RETURN );
146- $ innerClosure ->returnType = $ returnTypeNode instanceof Node ? $ returnTypeNode : null ;
151+ $ innerClosure ->returnType = $ this ->staticTypeMapper ->mapPHPStanTypeToPhpParserNode (
152+ $ returnedExprType ,
153+ TypeKind::RETURN
154+ );
147155
148156 return $ parentCaller ;
149157 }
You can’t perform that action at this time.
0 commit comments