Skip to content

Commit 2de8654

Browse files
add return type or empty if nothing to add (#574)
* add return type or empty if nothing to add * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 8488dcc commit 2de8654

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ReplaceReturnType extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->createMock('SomeClass')
12+
->method('someMethod')
13+
->with($this->callback(function ($arg): bool {
14+
return true;
15+
}))
16+
->willReturn('someValue');
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class ReplaceReturnType extends TestCase
29+
{
30+
public function test()
31+
{
32+
$this->createMock('SomeClass')
33+
->method('someMethod')
34+
->willReturnCallback(function ($arg): string {
35+
return 'someValue';
36+
});
37+
}
38+
}
39+
40+
?>

rules-tests/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector/Fixture/with_callback_and_will_return.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class WithCallbackAndWillReturn extends TestCase
3131
{
3232
$this->createMock('SomeClass')
3333
->method('someMethod')
34-
->willReturnCallback(function ($arg) {
34+
->willReturnCallback(function ($arg): string {
3535
return 'someValue';
3636
});
3737
}

rules/CodeQuality/Rector/MethodCall/MergeWithCallableAndWillReturnRector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use PhpParser\Node\Identifier;
1212
use PhpParser\Node\Stmt\Return_;
1313
use Rector\PhpParser\Node\Value\ValueResolver;
14+
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
1415
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1516
use Rector\Rector\AbstractRector;
17+
use Rector\StaticTypeMapper\StaticTypeMapper;
1618
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1719
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1820

@@ -24,6 +26,7 @@ final class MergeWithCallableAndWillReturnRector extends AbstractRector
2426
public function __construct(
2527
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
2628
private readonly ValueResolver $valueResolver,
29+
private readonly StaticTypeMapper $staticTypeMapper,
2730
) {
2831
}
2932

@@ -131,12 +134,17 @@ public function refactor(Node $node): MethodCall|null
131134

132135
/** @var Return_ $return */
133136
$return = $innerClosure->stmts[count($innerClosure->stmts) - 1];
134-
$return->expr = $willReturnMethodCall->getArgs()[0]
137+
$returnedExpr = $willReturnMethodCall->getArgs()[0]
135138
->value;
139+
$return->expr = $returnedExpr;
136140

137141
$parentCaller->name = new Identifier('willReturnCallback');
138142
$parentCaller->args = [new Arg($innerClosure)];
139143

144+
$returnedExprType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnedExpr);
145+
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnedExprType, TypeKind::RETURN);
146+
$innerClosure->returnType = $returnTypeNode instanceof Node ? $returnTypeNode : null;
147+
140148
return $parentCaller;
141149
}
142150

0 commit comments

Comments
 (0)