Skip to content

Commit b640d62

Browse files
authored
[CodeQuality] Add WillReturnCallbackFallbackToReturnFalseRector (#784)
1 parent cca51c7 commit b640d62

8 files changed

Lines changed: 324 additions & 0 deletions

File tree

‎config/sets/phpunit-code-quality.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;
5757
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
5858
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector;
59+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector;
5960
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector;
6061
use Rector\PHPUnit\CodeQuality\Rector\StmtsAwareInterface\DeclareStrictTypesTestsRector;
6162
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
@@ -87,6 +88,7 @@
8788
// narrow with consecutive
8889
NarrowIdenticalWithConsecutiveRector::class,
8990
NarrowSingleWillReturnCallbackRector::class,
91+
WillReturnCallbackFallbackToReturnFalseRector::class,
9092
SingleWithConsecutiveToWithRector::class,
9193

9294
// type declarations
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AddReturnFalse extends TestCase
8+
{
9+
public function test()
10+
{
11+
$matcher = $this->exactly(2);
12+
13+
$eventMock = $this->createMock(\stdClass::class);
14+
$eventMock->expects($matcher)->method('checkContext')->willReturnCallback(function (...$parameters) use ($matcher) {
15+
if (1 === $matcher->numberOfInvocations()) {
16+
$this->assertSame(['email.stats', 'emails'], $parameters[0]);
17+
18+
return true;
19+
}
20+
if (2 === $matcher->numberOfInvocations()) {
21+
$this->assertSame('emails', $parameters[0]);
22+
23+
return false;
24+
}
25+
});
26+
}
27+
}
28+
29+
?>
30+
-----
31+
<?php
32+
33+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector\Fixture;
34+
35+
use PHPUnit\Framework\TestCase;
36+
37+
final class AddReturnFalse extends TestCase
38+
{
39+
public function test()
40+
{
41+
$matcher = $this->exactly(2);
42+
43+
$eventMock = $this->createMock(\stdClass::class);
44+
$eventMock->expects($matcher)->method('checkContext')->willReturnCallback(function (...$parameters) use ($matcher) {
45+
if (1 === $matcher->numberOfInvocations()) {
46+
$this->assertSame(['email.stats', 'emails'], $parameters[0]);
47+
48+
return true;
49+
}
50+
if (2 === $matcher->numberOfInvocations()) {
51+
$this->assertSame('emails', $parameters[0]);
52+
53+
return false;
54+
}
55+
return false;
56+
});
57+
}
58+
}
59+
60+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipAlreadyHasReturn extends TestCase
8+
{
9+
public function test()
10+
{
11+
$matcher = $this->exactly(2);
12+
13+
$eventMock = $this->createMock(\stdClass::class);
14+
$eventMock->expects($matcher)->method('checkContext')->willReturnCallback(function (...$parameters) use ($matcher) {
15+
if (1 === $matcher->numberOfInvocations()) {
16+
return true;
17+
}
18+
19+
return false;
20+
});
21+
}
22+
}
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\WillReturnCallbackFallbackToReturnFalseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipNoReturn extends TestCase
8+
{
9+
public function test()
10+
{
11+
$matcher = $this->exactly(1);
12+
13+
$eventMock = $this->createMock(\stdClass::class);
14+
$eventMock->expects($matcher)->method('run')->willReturnCallback(function (...$parameters) use ($matcher) {
15+
$this->assertSame(['emails'], $parameters[0]);
16+
});
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipNonBoolReturn extends TestCase
8+
{
9+
public function test()
10+
{
11+
$matcher = $this->exactly(2);
12+
13+
$eventMock = $this->createMock(\stdClass::class);
14+
$eventMock->expects($matcher)->method('run')->willReturnCallback(function (...$parameters) use ($matcher) {
15+
if (1 === $matcher->numberOfInvocations()) {
16+
return 1;
17+
}
18+
if (2 === $matcher->numberOfInvocations()) {
19+
return false;
20+
}
21+
});
22+
}
23+
}
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\WillReturnCallbackFallbackToReturnFalseRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class WillReturnCallbackFallbackToReturnFalseRectorTest 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\WillReturnCallbackFallbackToReturnFalseRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(WillReturnCallbackFallbackToReturnFalseRector::class);
10+
};
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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\Expr\Closure;
9+
use PhpParser\Node\Expr\ConstFetch;
10+
use PhpParser\Node\Expr\MethodCall;
11+
use PhpParser\Node\Stmt\Return_;
12+
use Rector\PhpParser\Node\BetterNodeFinder;
13+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
14+
use Rector\Rector\AbstractRector;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
/**
19+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector\WillReturnCallbackFallbackToReturnFalseRectorTest
20+
*/
21+
final class WillReturnCallbackFallbackToReturnFalseRector extends AbstractRector
22+
{
23+
public function __construct(
24+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
25+
private readonly BetterNodeFinder $betterNodeFinder,
26+
) {
27+
}
28+
29+
public function getRuleDefinition(): RuleDefinition
30+
{
31+
return new RuleDefinition(
32+
'Add a "return false" fallback to a willReturnCallback() closure that only returns booleans, but can fall through without a return',
33+
[
34+
new CodeSample(
35+
<<<'CODE_SAMPLE'
36+
use PHPUnit\Framework\TestCase;
37+
38+
final class SomeTest extends TestCase
39+
{
40+
public function test()
41+
{
42+
$matcher = $this->exactly(2);
43+
44+
$this->eventMock->expects($matcher)
45+
->method('checkContext')
46+
->willReturnCallback(function (...$parameters) use ($matcher) {
47+
if ($matcher->numberOfInvocations() === 1) {
48+
return true;
49+
}
50+
if ($matcher->numberOfInvocations() === 2) {
51+
return false;
52+
}
53+
});
54+
}
55+
}
56+
CODE_SAMPLE
57+
,
58+
<<<'CODE_SAMPLE'
59+
use PHPUnit\Framework\TestCase;
60+
61+
final class SomeTest extends TestCase
62+
{
63+
public function test()
64+
{
65+
$matcher = $this->exactly(2);
66+
67+
$this->eventMock->expects($matcher)
68+
->method('checkContext')
69+
->willReturnCallback(function (...$parameters) use ($matcher) {
70+
if ($matcher->numberOfInvocations() === 1) {
71+
return true;
72+
}
73+
if ($matcher->numberOfInvocations() === 2) {
74+
return false;
75+
}
76+
77+
return false;
78+
});
79+
}
80+
}
81+
CODE_SAMPLE
82+
),
83+
]
84+
);
85+
}
86+
87+
/**
88+
* @return array<class-string<Node>>
89+
*/
90+
public function getNodeTypes(): array
91+
{
92+
return [MethodCall::class];
93+
}
94+
95+
/**
96+
* @param MethodCall $node
97+
*/
98+
public function refactor(Node $node): ?MethodCall
99+
{
100+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
101+
return null;
102+
}
103+
104+
if (! $this->isName($node->name, 'willReturnCallback')) {
105+
return null;
106+
}
107+
108+
if ($node->isFirstClassCallable()) {
109+
return null;
110+
}
111+
112+
if (count($node->getArgs()) !== 1) {
113+
return null;
114+
}
115+
116+
$closure = $node->getArgs()[0]
117+
->value;
118+
if (! $closure instanceof Closure) {
119+
return null;
120+
}
121+
122+
if ($closure->stmts === []) {
123+
return null;
124+
}
125+
126+
// already ends with an explicit return, no fallback needed
127+
$lastStmt = $closure->stmts[count($closure->stmts) - 1];
128+
if ($lastStmt instanceof Return_) {
129+
return null;
130+
}
131+
132+
if (! $this->hasOnlyBoolReturns($closure)) {
133+
return null;
134+
}
135+
136+
$closure->stmts[] = new Return_($this->nodeFactory->createFalse());
137+
138+
return $node;
139+
}
140+
141+
private function hasOnlyBoolReturns(Closure $closure): bool
142+
{
143+
/** @var Return_[] $returns */
144+
$returns = $this->betterNodeFinder->findInstancesOfScoped($closure->stmts, [Return_::class]);
145+
if ($returns === []) {
146+
return false;
147+
}
148+
149+
foreach ($returns as $return) {
150+
if (! $return->expr instanceof ConstFetch) {
151+
return false;
152+
}
153+
154+
if (! $this->isNames($return->expr->name, ['true', 'false'])) {
155+
return false;
156+
}
157+
}
158+
159+
return true;
160+
}
161+
}

0 commit comments

Comments
 (0)