Skip to content

Commit c31d89e

Browse files
committed
handle solo identical as well, to get better message
1 parent 57106e8 commit c31d89e

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

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 HandleSoloCompare extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->createMock('AnyType')
12+
->expects($this->any())
13+
->method('trans')
14+
->with(
15+
$this->callback(
16+
fn ($args): bool => count($args) === 5
17+
)
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 HandleSoloCompare extends TestCase
31+
{
32+
public function test()
33+
{
34+
$someMock = $this->createMock('AnyType')
35+
->expects($this->any())
36+
->method('trans')
37+
->with(
38+
$this->callback(
39+
function ($args): bool {
40+
$this->assertCount(5, $args);
41+
return true;
42+
}
43+
)
44+
);
45+
}
46+
}
47+
48+
?>

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838
public function getRuleDefinition(): RuleDefinition
3939
{
4040
return new RuleDefinition(
41-
'Replaces identical compare in $this->callable() to standalone PHPUnit asserts',
41+
'Replaces identical compare in $this->callable() sole return to standalone PHPUnit asserts that show more detailed failure messages',
4242
[
4343
new CodeSample(
4444
<<<'CODE_SAMPLE'
@@ -113,11 +113,14 @@ public function refactor(Node $node): MethodCall|null
113113
}
114114

115115
$innerSoleExpr = $this->matchInnerSoleExpr($argAndFunctionLike->getFunctionLike());
116-
if (! $innerSoleExpr instanceof BooleanAnd) {
116+
if ($innerSoleExpr instanceof BooleanAnd) {
117+
$joinedExprs = $this->extractJoinedExprs($innerSoleExpr);
118+
} elseif ($innerSoleExpr instanceof Expr\BinaryOp\Identical) {
119+
$joinedExprs = [$innerSoleExpr];
120+
} else {
117121
return null;
118122
}
119123

120-
$joinedExprs = $this->extractJoinedExprs($innerSoleExpr);
121124
if ($joinedExprs === null || $joinedExprs === []) {
122125
return null;
123126
}

0 commit comments

Comments
 (0)