Skip to content

Commit 7095af8

Browse files
committed
[CodeQuality] Simplify single-statement void callback in CallbackSingleAssertToSimplerRector
1 parent 0629d20 commit 7095af8

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\CallbackSingleAssertToSimplerRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SingleStmtVoidCallback extends TestCase
8+
{
9+
public function test()
10+
{
11+
$builder = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$builder->expects($this->once())
14+
->method('queueWebhooksByType')
15+
->with($this->callback(function ($type): void {
16+
$this->assertSame($type, 'some_value');
17+
}));
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\CallbackSingleAssertToSimplerRector\Fixture;
26+
27+
use PHPUnit\Framework\TestCase;
28+
29+
final class SingleStmtVoidCallback extends TestCase
30+
{
31+
public function test()
32+
{
33+
$builder = $this->getMockBuilder('AnyType')->getMock();
34+
35+
$builder->expects($this->once())
36+
->method('queueWebhooksByType')
37+
->with($this->equalTo('some_value'));
38+
}
39+
}
40+
41+
?>

‎rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,20 @@ private function matchCallbackSoleAssertSameExpected(Expr $expr): ?Expr
133133
}
134134
}
135135

136-
// exactly assertSame() expression + "return true;"
136+
// sole assertSame() expression, optionally followed by "return true;"
137137
$closureStmts = $innerClosure->getStmts();
138-
if (count($closureStmts) !== 2) {
138+
if (count($closureStmts) === 2) {
139+
[$firstStmt, $secondStmt] = $closureStmts;
140+
141+
if (! $secondStmt instanceof Return_ || ! $this->isTrueReturn($secondStmt)) {
142+
return null;
143+
}
144+
} elseif (count($closureStmts) === 1) {
145+
$firstStmt = $closureStmts[0];
146+
} else {
139147
return null;
140148
}
141149

142-
[$firstStmt, $secondStmt] = $closureStmts;
143-
144150
if (! $firstStmt instanceof Expression) {
145151
return null;
146152
}
@@ -150,10 +156,6 @@ private function matchCallbackSoleAssertSameExpected(Expr $expr): ?Expr
150156
return null;
151157
}
152158

153-
if (! $secondStmt instanceof Return_ || ! $this->isTrueReturn($secondStmt)) {
154-
return null;
155-
}
156-
157159
$assertSameArgs = $firstStmtExpr->getArgs();
158160
if (count($assertSameArgs) !== 2) {
159161
return null;

0 commit comments

Comments
 (0)