Skip to content

Commit e5672be

Browse files
committed
add use var
1 parent 2de8654 commit e5672be

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
?>

rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\ClosureUse;
910
use PhpParser\Node\Expr\Closure;
1011
use PhpParser\Node\Expr\MethodCall;
12+
use PhpParser\Node\Expr\Variable;
1113
use PhpParser\Node\Identifier;
1214
use PhpParser\Node\Stmt\Return_;
1315
use 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
}

0 commit comments

Comments
 (0)