Skip to content

Commit 3b54ca1

Browse files
authored
register ReplaceAtMethodWithDesiredMatcherRector to code-quality (#563)
* simplify code * register ReplaceAtMethodWithDesiredMatcherRector to code-quality
1 parent b05ac84 commit 3b54ca1

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
4747
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector;
4848
use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
49+
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
4950

5051
return static function (RectorConfig $rectorConfig): void {
5152
$rectorConfig->rules([
@@ -119,5 +120,6 @@
119120
// prefer simple mocking
120121
GetMockBuilderGetMockToCreateMockRector::class,
121122
EntityDocumentCreateMockToDirectNewRector::class,
123+
ReplaceAtMethodWithDesiredMatcherRector::class,
122124
]);
123125
};

rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\Expr\MethodCall;
10-
use PhpParser\Node\Identifier;
1110
use PhpParser\Node\Scalar\Int_;
1211
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1312
use Rector\Rector\AbstractRector;
@@ -19,8 +18,6 @@
1918
*/
2019
final class ReplaceAtMethodWithDesiredMatcherRector extends AbstractRector
2120
{
22-
private bool $hasChanged = false;
23-
2421
public function __construct(
2522
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
2623
) {
@@ -57,70 +54,69 @@ public function getNodeTypes(): array
5754
}
5855

5956
/**
60-
* @param MethodCall $node
57+
* @param MethodCall $node
6158
*/
6259
public function refactor(Node $node): null|MethodCall
6360
{
64-
$this->hasChanged = false;
65-
6661
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
6762
return null;
6863
}
6964

70-
if ($node->var instanceof MethodCall && $arg = $this->findAtMethodCall($node->var)) {
71-
$this->replaceWithDesiredMatcher($arg);
72-
}
73-
74-
if ($this->hasChanged) {
75-
return $node;
76-
}
77-
78-
return null;
79-
}
80-
81-
private function findAtMethodCall(MethodCall $methodCall): ?Arg
82-
{
83-
foreach ($methodCall->getArgs() as $arg) {
84-
if ($arg->value instanceof MethodCall &&
85-
$arg->value->name instanceof Identifier &&
86-
$arg->value->name->toString() === 'at'
87-
) {
88-
return $arg;
89-
}
65+
if (! $node->var instanceof MethodCall) {
66+
return null;
9067
}
9168

92-
if ($methodCall->var instanceof MethodCall) {
93-
$this->findAtMethodCall($methodCall->var);
69+
$arg = $this->findAtMethodCall($node->var);
70+
if (! $arg instanceof Arg) {
71+
return null;
9472
}
9573

96-
return null;
97-
}
98-
99-
private function replaceWithDesiredMatcher(Arg $arg): void
100-
{
10174
if (! $arg->value instanceof MethodCall) {
102-
return;
75+
return null;
10376
}
10477

78+
$count = null;
10579
foreach ($arg->value->getArgs() as $item) {
10680
if ($item->value instanceof Int_) {
10781
$count = $item->value->value;
10882
}
10983
}
11084

11185
if (! isset($count)) {
112-
return;
86+
return null;
11387
}
11488

11589
if ($count === 0) {
11690
$arg->value = new MethodCall($arg->value->var, 'never');
117-
$this->hasChanged = true;
118-
} elseif ($count === 1) {
91+
return $node;
92+
}
93+
94+
if ($count === 1) {
11995
$arg->value = new MethodCall($arg->value->var, 'once');
120-
$this->hasChanged = true;
121-
} elseif ($count > 1) {
96+
return $node;
97+
}
98+
99+
if ($count > 1) {
122100
$arg->value = new MethodCall($arg->value->var, 'exactly', [new Arg(new Int_($count))]);
123-
$this->hasChanged = true;
101+
return $node;
102+
}
103+
104+
return null;
105+
}
106+
107+
private function findAtMethodCall(MethodCall $methodCall): ?Arg
108+
{
109+
foreach ($methodCall->getArgs() as $arg) {
110+
$argExpr = $arg->value;
111+
if ($argExpr instanceof MethodCall && $this->isName($argExpr->name, 'at')) {
112+
return $arg;
113+
}
124114
}
115+
116+
if ($methodCall->var instanceof MethodCall) {
117+
$this->findAtMethodCall($methodCall->var);
118+
}
119+
120+
return null;
125121
}
126122
}

0 commit comments

Comments
 (0)