Skip to content

Commit 75564ba

Browse files
committed
allow extra stmt
1 parent 8db0f5a commit 75564ba

3 files changed

Lines changed: 69 additions & 29 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ExtraStmt extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
$item = 100;
17+
return count($args) === 5 && isset($args[0]) && $args[0] === 'some_value';
18+
}));
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
27+
28+
use PHPUnit\Framework\TestCase;
29+
30+
final class ExtraStmt extends TestCase
31+
{
32+
public function test()
33+
{
34+
$someMock = $this->getMockBuilder('AnyType')->getMock();
35+
36+
$someMock->expects($this->any())
37+
->method('trans')
38+
->with($this->callback(function ($args): bool {
39+
$item = 100;
40+
$this->assertCount(5, $args);
41+
$this->assertArrayHasKey(0, $args);
42+
$this->assertSame('some_value', $args[0]);
43+
return true;
44+
}));
45+
}
46+
}
47+
48+
?>

‎rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/skip_multiple_stmts.php.inc‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,28 @@ public function refactor(Node $node): MethodCall|null
137137
return null;
138138
}
139139

140+
// all stmts but last
141+
$functionLike = $argAndFunctionLike->getFunctionLike();
142+
143+
if ($functionLike instanceof Closure) {
144+
$functionStmts = $functionLike->stmts;
145+
146+
if (count($functionStmts) >= 2) {
147+
unset($functionStmts[array_key_last($functionStmts)]);
148+
} else {
149+
$functionStmts = [];
150+
}
151+
} else {
152+
$functionStmts = [];
153+
}
154+
140155
// last si return true;
141156
$assertExpressions[] = new Return_($this->nodeFactory->createTrue());
142157

143158
$innerFunctionLike = $argAndFunctionLike->getFunctionLike();
144159

145160
if ($innerFunctionLike instanceof Closure) {
146-
$innerFunctionLike->stmts = $assertExpressions;
161+
$innerFunctionLike->stmts = array_merge($functionStmts, $assertExpressions);
147162
} else {
148163
// arrow function -> flip to closure
149164
$functionLikeInArg = $argAndFunctionLike->getArg();
@@ -218,16 +233,13 @@ private function matchWithCallbackInnerClosure(MethodCall $methodCall): null|Arg
218233
private function matchInnerSoleExpr(Closure|ArrowFunction $functionLike): ?Expr
219234
{
220235
if ($functionLike instanceof Closure) {
221-
if (count($functionLike->stmts) !== 1) {
222-
return null;
223-
}
236+
foreach ($functionLike->getStmts() as $stmt) {
237+
if (! $stmt instanceof Return_) {
238+
continue;
239+
}
224240

225-
$innerStmt = $functionLike->stmts[0];
226-
if (! $innerStmt instanceof Return_) {
227-
return null;
241+
return $stmt->expr;
228242
}
229-
230-
return $innerStmt->expr;
231243
}
232244

233245
return $functionLike->expr;

0 commit comments

Comments
 (0)