diff --git a/rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/replace_return_type.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/replace_return_type.php.inc new file mode 100644 index 00000000..0a5c6a18 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/replace_return_type.php.inc @@ -0,0 +1,40 @@ +createMock('SomeClass') + ->method('someMethod') + ->with($this->callback(function ($arg): bool { + return true; + })) + ->willReturn('someValue'); + } +} + +?> +----- +createMock('SomeClass') + ->method('someMethod') + ->willReturnCallback(function ($arg): string { + return 'someValue'; + }); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/with_callback_and_will_return.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/with_callback_and_will_return.php.inc index d62d7525..b9446c73 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/with_callback_and_will_return.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/with_callback_and_will_return.php.inc @@ -31,7 +31,7 @@ final class WithCallbackAndWillReturn extends TestCase { $this->createMock('SomeClass') ->method('someMethod') - ->willReturnCallback(function ($arg) { + ->willReturnCallback(function ($arg): string { return 'someValue'; }); } diff --git a/rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php b/rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php index bc3147b1..ea0577a5 100644 --- a/rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php +++ b/rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php @@ -11,8 +11,10 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Return_; use Rector\PhpParser\Node\Value\ValueResolver; +use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; +use Rector\StaticTypeMapper\StaticTypeMapper; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -24,6 +26,7 @@ final class MergeWithCallableAndWillReturnRector extends AbstractRector public function __construct( private readonly TestsNodeAnalyzer $testsNodeAnalyzer, private readonly ValueResolver $valueResolver, + private readonly StaticTypeMapper $staticTypeMapper, ) { } @@ -131,12 +134,17 @@ public function refactor(Node $node): MethodCall|null /** @var Return_ $return */ $return = $innerClosure->stmts[count($innerClosure->stmts) - 1]; - $return->expr = $willReturnMethodCall->getArgs()[0] + $returnedExpr = $willReturnMethodCall->getArgs()[0] ->value; + $return->expr = $returnedExpr; $parentCaller->name = new Identifier('willReturnCallback'); $parentCaller->args = [new Arg($innerClosure)]; + $returnedExprType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnedExpr); + $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnedExprType, TypeKind::RETURN); + $innerClosure->returnType = $returnTypeNode instanceof Node ? $returnTypeNode : null; + return $parentCaller; }