Skip to content

Commit 8488dcc

Browse files
authored
[code-quality] Add MergeWithCallableAndWillReturnRector (#573)
1 parent c063654 commit 8488dcc

File tree

8 files changed

+271
-29
lines changed

8 files changed

+271
-29
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector;
3838
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\FlipAssertRector;
3939
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector;
40+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector;
4041
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector;
4142
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector;
4243
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveExpectAnyFromMockRector;
@@ -123,6 +124,7 @@
123124
FinalizeTestCaseClassRector::class,
124125
DeclareStrictTypesTestsRector::class,
125126
WithCallbackIdenticalToStandaloneAssertsRector::class,
127+
MergeWithCallableAndWillReturnRector::class,
126128

127129
// prefer simple mocking
128130
GetMockBuilderGetMockToCreateMockRector::class,

rules-tests/CodeQuality/Rector/Class_/YieldDataProviderRector/Fixture/skip_yield_from_expr.php

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipComplexCompare extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->createMock('SomeClass')
12+
->method('someMethod')
13+
->with($this->callback(function ($arg) {
14+
return $arg === 100 && $arg >= 50;
15+
}))
16+
->willReturn('someValue');
17+
}
18+
}
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 WithCallbackAndWillReturn extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->createMock('SomeClass')
12+
->method('someMethod')
13+
->with($this->callback(function ($arg) {
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 WithCallbackAndWillReturn extends TestCase
29+
{
30+
public function test()
31+
{
32+
$this->createMock('SomeClass')
33+
->method('someMethod')
34+
->willReturnCallback(function ($arg) {
35+
return 'someValue';
36+
});
37+
}
38+
}
39+
40+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class MergeWithCallableAndWillReturnRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(MergeWithCallableAndWillReturnRector::class);
10+
};
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\Closure;
10+
use PhpParser\Node\Expr\MethodCall;
11+
use PhpParser\Node\Identifier;
12+
use PhpParser\Node\Stmt\Return_;
13+
use Rector\PhpParser\Node\Value\ValueResolver;
14+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
15+
use Rector\Rector\AbstractRector;
16+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
17+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18+
19+
/**
20+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector\MergeWithCallableAndWillReturnRectorTest
21+
*/
22+
final class MergeWithCallableAndWillReturnRector extends AbstractRector
23+
{
24+
public function __construct(
25+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
26+
private readonly ValueResolver $valueResolver,
27+
) {
28+
}
29+
30+
public function getRuleDefinition(): RuleDefinition
31+
{
32+
return new RuleDefinition(
33+
'Merge split mocking method ->with($this->callback(...)) and ->willReturn(expr) to single ->willReturnCallback() call',
34+
[
35+
new CodeSample(
36+
<<<'CODE_SAMPLE'
37+
use PHPUnit\Framework\TestCase;
38+
39+
final class SomeTest extends TestCase
40+
{
41+
public function test()
42+
{
43+
$this->createMock('SomeClass')
44+
->expects($this->once())
45+
->method('someMethod')
46+
->with($this->callback(function (array $args): bool {
47+
return true;
48+
}))
49+
->willReturn(['some item']);
50+
}
51+
}
52+
CODE_SAMPLE
53+
,
54+
<<<'CODE_SAMPLE'
55+
use PHPUnit\Framework\TestCase;
56+
57+
final class SomeTest extends TestCase
58+
{
59+
public function test()
60+
{
61+
$this->createMock('SomeClass')
62+
->expects($this->once())
63+
->method('someMethod')
64+
->willReturnCallback(function (array $args): array {
65+
return ['some item'];
66+
});
67+
}
68+
}
69+
CODE_SAMPLE
70+
),
71+
]
72+
);
73+
}
74+
75+
/**
76+
* @return array<class-string<MethodCall>>
77+
*/
78+
public function getNodeTypes(): array
79+
{
80+
return [MethodCall::class];
81+
}
82+
83+
/**
84+
* @param MethodCall $node
85+
*/
86+
public function refactor(Node $node): MethodCall|null
87+
{
88+
if ($node->isFirstClassCallable()) {
89+
return null;
90+
}
91+
92+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
93+
return null;
94+
}
95+
96+
if (! $this->isName($node->name, 'willReturn')) {
97+
return null;
98+
}
99+
100+
$parentCaller = $node->var;
101+
if (! $parentCaller instanceof MethodCall) {
102+
return null;
103+
}
104+
105+
if (! $this->isName($parentCaller->name, 'with')) {
106+
return null;
107+
}
108+
109+
$willReturnMethodCall = $node;
110+
111+
$withMethodCall = $parentCaller;
112+
113+
$callbackMethodCall = $this->matchFirstArgCallbackMethodCall($withMethodCall);
114+
if (! $callbackMethodCall instanceof MethodCall) {
115+
return null;
116+
}
117+
118+
$innerClosure = $callbackMethodCall->getArgs()[0]
119+
->value;
120+
if (! $innerClosure instanceof Closure) {
121+
return null;
122+
}
123+
124+
if ($innerClosure->stmts === []) {
125+
return null;
126+
}
127+
128+
if (! $this->isLastStmtReturnTrue($innerClosure)) {
129+
return null;
130+
}
131+
132+
/** @var Return_ $return */
133+
$return = $innerClosure->stmts[count($innerClosure->stmts) - 1];
134+
$return->expr = $willReturnMethodCall->getArgs()[0]
135+
->value;
136+
137+
$parentCaller->name = new Identifier('willReturnCallback');
138+
$parentCaller->args = [new Arg($innerClosure)];
139+
140+
return $parentCaller;
141+
}
142+
143+
private function matchFirstArgCallbackMethodCall(MethodCall $withMethodCall): ?MethodCall
144+
{
145+
$firstArgValue = $withMethodCall->getArgs()[0]
146+
->value;
147+
if (! $firstArgValue instanceof MethodCall) {
148+
return null;
149+
}
150+
151+
if (! $this->isName($firstArgValue->name, 'callback')) {
152+
return null;
153+
}
154+
155+
return $firstArgValue;
156+
}
157+
158+
private function isLastStmtReturnTrue(Closure $closure): bool
159+
{
160+
$lastStmt = $closure->stmts[count($closure->stmts) - 1];
161+
162+
if (! $lastStmt instanceof Return_) {
163+
return false;
164+
}
165+
166+
if (! $lastStmt->expr instanceof Node) {
167+
return false;
168+
}
169+
170+
return $this->valueResolver->isTrue($lastStmt->expr);
171+
}
172+
}

rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
66

7-
use PhpParser\Node\Expr\BinaryOp\Identical;
87
use PhpParser\Node;
98
use PhpParser\Node\ClosureUse;
109
use PhpParser\Node\Expr;
1110
use PhpParser\Node\Expr\ArrowFunction;
1211
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
1312
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
13+
use PhpParser\Node\Expr\BinaryOp\Identical;
1414
use PhpParser\Node\Expr\Closure;
1515
use PhpParser\Node\Expr\MethodCall;
1616
use PhpParser\Node\Expr\Variable;

0 commit comments

Comments
 (0)