forked from rectorphp/rector-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWithConsecutiveRector.php
180 lines (147 loc) · 4.91 KB
/
WithConsecutiveRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\Rector\StmtsAwareInterface;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\Rector\StmtsAwareInterface\WithConsecutiveRector\WithConsecutiveRectorTest
*/
final class WithConsecutiveRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Refactor "withConsecutive()" to ', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$this->personServiceMock->expects($this->exactly(2))
->method('prepare')
->withConsecutive(
[1, 2],
[3, 4],
);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$matcher = $this->exactly(2);
$this->personServiceMock->expects($matcher)
->method('prepare')
->willReturnCallback(function () use ($matcher) {
return match ($matcher->numberOfInvocations()) {
1 => [1, 2],
2 => [3, 4]
};
});
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param Expression $node
*/
public function refactor(Node $node)
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
if (! $node->expr instanceof MethodCall) {
return null;
}
/** @var MethodCall|null $withConsecutiveMethodCall */
$withConsecutiveMethodCall = $this->betterNodeFinder->findFirst($node->expr, function (Node $node): bool {
if (! $node instanceof MethodCall) {
return false;
}
return $this->isName($node->name, 'withConsecutive');
});
if (! $withConsecutiveMethodCall instanceof MethodCall) {
return null;
}
$expectsMethodCall = $this->matchAndRefactorExpectsMethodCall($node);
if (! $expectsMethodCall instanceof MethodCall) {
return null;
}
// 2. rename and replace withConsecutive()
$withConsecutiveMethodCall->name = new Identifier('willReturnCallback');
$withConsecutiveMethodCall->args = [new Arg($this->createClosure($withConsecutiveMethodCall))];
$matcherAssign = new Assign(new Variable('matcher'), $expectsMethodCall);
return [new Expression($matcherAssign), $node];
}
private function createClosure(MethodCall $expectsMethodCall): Closure
{
$closure = new Closure();
$matcherVariable = new Variable('matcher');
$closure->uses[] = new ClosureUse($matcherVariable);
$match = new Match_(new MethodCall($matcherVariable, new Identifier('numberOfInvocations')));
foreach ($expectsMethodCall->getArgs() as $key => $arg) {
$match->arms[] = new MatchArm([new LNumber($key + 1)], $arg->value);
}
$closure->stmts[] = new Return_($match);
return $closure;
}
/**
* Replace $this->expects(...)
*
* @param Expression<MethodCall> $expression
*/
private function matchAndRefactorExpectsMethodCall(Expression $expression): ?MethodCall
{
/** @var MethodCall|null $exactlyMethodCall */
$exactlyMethodCall = null;
$this->traverseNodesWithCallable($expression, function (Node $node) use (&$exactlyMethodCall): ?MethodCall {
if (! $node instanceof MethodCall) {
return null;
}
if (! $this->isName($node->name, 'expects')) {
return null;
}
$firstArg = $node->getArgs()[0];
if (! $firstArg->value instanceof MethodCall) {
return null;
}
$exactlyMethodCall = $firstArg->value;
$node->args = [new Arg(new Variable('matcher'))];
return $node;
});
return $exactlyMethodCall;
}
}