Skip to content

Commit e2c5f39

Browse files
authored
allow extra stmt (#579)
1 parent 8db0f5a commit e2c5f39

File tree

4 files changed

+74
-30
lines changed

4 files changed

+74
-30
lines changed
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/NodeFactory/FromBinaryAndAssertExpressionsFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node\Expr;
88
use PhpParser\Node\Expr\ArrayDimFetch;
9+
use PhpParser\Node\Expr\BinaryOp\Equal;
910
use PhpParser\Node\Expr\BinaryOp\Identical;
1011
use PhpParser\Node\Expr\ClassConstFetch;
1112
use PhpParser\Node\Expr\FuncCall;
@@ -84,7 +85,7 @@ public function create(array $exprs): ?array
8485
continue;
8586
}
8687

87-
if ($expr instanceof Identical || $expr instanceof Expr\BinaryOp\Equal) {
88+
if ($expr instanceof Identical || $expr instanceof Equal) {
8889
if ($expr->left instanceof FuncCall && $this->nodeNameResolver->isName($expr->left, 'count')) {
8990
if ($expr->right instanceof Int_) {
9091
$countedExpr = $expr->left->getArgs()[0]

rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Expr\ArrowFunction;
1111
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
1212
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
13+
use PhpParser\Node\Expr\BinaryOp\Equal;
1314
use PhpParser\Node\Expr\BinaryOp\Identical;
1415
use PhpParser\Node\Expr\Closure;
1516
use PhpParser\Node\Expr\FuncCall;
@@ -122,7 +123,7 @@ public function refactor(Node $node): MethodCall|null
122123
} elseif ($innerSoleExpr instanceof Identical || $innerSoleExpr instanceof Instanceof_ || $innerSoleExpr instanceof Isset_ || ($innerSoleExpr instanceof FuncCall && $this->isName(
123124
$innerSoleExpr->name,
124125
'array_key_exists'
125-
)) || $innerSoleExpr instanceof Expr\BinaryOp\Equal) {
126+
)) || $innerSoleExpr instanceof Equal) {
126127
$joinedExprs = [$innerSoleExpr];
127128
} else {
128129
return null;
@@ -137,13 +138,28 @@ public function refactor(Node $node): MethodCall|null
137138
return null;
138139
}
139140

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

143159
$innerFunctionLike = $argAndFunctionLike->getFunctionLike();
144160

145161
if ($innerFunctionLike instanceof Closure) {
146-
$innerFunctionLike->stmts = $assertExpressions;
162+
$innerFunctionLike->stmts = array_merge($functionStmts, $assertExpressions);
147163
} else {
148164
// arrow function -> flip to closure
149165
$functionLikeInArg = $argAndFunctionLike->getArg();
@@ -218,16 +234,15 @@ private function matchWithCallbackInnerClosure(MethodCall $methodCall): null|Arg
218234
private function matchInnerSoleExpr(Closure|ArrowFunction $functionLike): ?Expr
219235
{
220236
if ($functionLike instanceof Closure) {
221-
if (count($functionLike->stmts) !== 1) {
222-
return null;
223-
}
237+
foreach ($functionLike->getStmts() as $stmt) {
238+
if (! $stmt instanceof Return_) {
239+
continue;
240+
}
224241

225-
$innerStmt = $functionLike->stmts[0];
226-
if (! $innerStmt instanceof Return_) {
227-
return null;
242+
return $stmt->expr;
228243
}
229244

230-
return $innerStmt->expr;
245+
return null;
231246
}
232247

233248
return $functionLike->expr;

0 commit comments

Comments
 (0)