Skip to content

Commit c063654

Browse files
authored
handle solo identical as well, to get better message (#572)
1 parent 57106e8 commit c063654

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
66

7+
use PhpParser\Node\Expr\BinaryOp\Identical;
78
use PhpParser\Node;
89
use PhpParser\Node\ClosureUse;
910
use PhpParser\Node\Expr;
@@ -38,7 +39,7 @@ public function __construct(
3839
public function getRuleDefinition(): RuleDefinition
3940
{
4041
return new RuleDefinition(
41-
'Replaces identical compare in $this->callable() to standalone PHPUnit asserts',
42+
'Replaces identical compare in $this->callable() sole return to standalone PHPUnit asserts that show more detailed failure messages',
4243
[
4344
new CodeSample(
4445
<<<'CODE_SAMPLE'
@@ -113,11 +114,14 @@ public function refactor(Node $node): MethodCall|null
113114
}
114115

115116
$innerSoleExpr = $this->matchInnerSoleExpr($argAndFunctionLike->getFunctionLike());
116-
if (! $innerSoleExpr instanceof BooleanAnd) {
117+
if ($innerSoleExpr instanceof BooleanAnd) {
118+
$joinedExprs = $this->extractJoinedExprs($innerSoleExpr);
119+
} elseif ($innerSoleExpr instanceof Identical) {
120+
$joinedExprs = [$innerSoleExpr];
121+
} else {
117122
return null;
118123
}
119124

120-
$joinedExprs = $this->extractJoinedExprs($innerSoleExpr);
121125
if ($joinedExprs === null || $joinedExprs === []) {
122126
return null;
123127
}

0 commit comments

Comments
 (0)