From e76c4b7d6a46e108e765b7e7e1ba5eaa547d7386 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 31 Oct 2025 21:33:07 +0100 Subject: [PATCH 1/2] add return type or empty if nothing to add --- .../Fixture/replace_return_type.php.inc | 40 +++++++++++++++++++ .../with_callback_and_will_return.php.inc | 2 +- .../MergeWithCallableAndWillReturnRector.php | 14 ++++++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/replace_return_type.php.inc 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..22635a75 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,21 @@ 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); + if ($returnTypeNode instanceof \PhpParser\Node) { + $innerClosure->returnType = $returnTypeNode; + } else { + $innerClosure->returnType = null; + } + return $parentCaller; } From 0b66961b98ff086c32a2d1956b702af0f8ba1dfc Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 31 Oct 2025 20:33:52 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../MethodCall/MergeWithCallableAndWillReturnRector.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php b/rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php index 22635a75..ea0577a5 100644 --- a/rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php +++ b/rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php @@ -143,11 +143,7 @@ public function refactor(Node $node): MethodCall|null $returnedExprType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnedExpr); $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnedExprType, TypeKind::RETURN); - if ($returnTypeNode instanceof \PhpParser\Node) { - $innerClosure->returnType = $returnTypeNode; - } else { - $innerClosure->returnType = null; - } + $innerClosure->returnType = $returnTypeNode instanceof Node ? $returnTypeNode : null; return $parentCaller; }