Repository navigation
Expand file tree
/
Copy pathWithConsecutiveRector.php
More file actions
269 lines (223 loc) · 8.05 KB
/
Copy pathWithConsecutiveRector.php
File metadata and controls
269 lines (223 loc) · 8.05 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?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\Class_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\Rector\StmtsAwareInterface\WithConsecutiveRector\WithConsecutiveRectorTest
*/
final class WithConsecutiveRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Refactor deprecated withConsecutive() to willReturnCallback() structure', [
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;
}
$withConsecutiveMethodCall = $this->findWithConsecutiveMethodCall($node);
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];
}
public function provideMinPhpVersion(): int
{
/**
* This rule just work for phpunit 10,
* And as php 8.1 is the min version supported by phpunit 10, then we decided to let this version as minimum.
*
* You can see more detail in this issue: https://github.com/rectorphp/rector-phpunit/issues/272
*/
return PhpVersion::PHP_81;
}
/**
* @template T of Node
* @param Node|Node[] $node
* @param class-string<T> $type
* @return T[]
*/
public function findInstancesOfScoped(Node|array $node, string $type): array
{
/** @var T[] $foundNodes */
$foundNodes = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$node,
static function (Node $subNode) use ($type, &$foundNodes): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($subNode instanceof $type) {
$foundNodes[] = $subNode;
return null;
}
return null;
}
);
return $foundNodes;
}
private function createClosure(MethodCall $expectsMethodCall): Closure
{
$closure = new Closure();
$matcherVariable = new Variable('matcher');
$closure->uses[] = new ClosureUse($matcherVariable);
$usedVariables = $this->resolveUniqueUsedVariables($expectsMethodCall);
foreach ($usedVariables as $usedVariable) {
$closure->uses[] = new ClosureUse($usedVariable);
}
$match = $this->createMatch($matcherVariable, $expectsMethodCall);
$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;
}
private function findWithConsecutiveMethodCall(Expression $expression): ?MethodCall
{
if (! $expression->expr instanceof MethodCall) {
return null;
}
/** @var MethodCall|null $withConsecutiveMethodCall */
$withConsecutiveMethodCall = $this->betterNodeFinder->findFirst($expression->expr, function (Node $node): bool {
if (! $node instanceof MethodCall) {
return false;
}
return $this->isName($node->name, 'withConsecutive');
});
return $withConsecutiveMethodCall;
}
private function createMatch(Variable $matcherVariable, MethodCall $expectsMethodCall): Match_
{
$numberOfInvocationsMethodCall = new MethodCall($matcherVariable, new Identifier('numberOfInvocations'));
$matchArms = [];
foreach ($expectsMethodCall->getArgs() as $key => $arg) {
$matchArms[] = new MatchArm([new LNumber($key + 1)], $arg->value);
}
return new Match_($numberOfInvocationsMethodCall, $matchArms);
}
/**
* @return Variable[]
*/
private function resolveUniqueUsedVariables(MethodCall $expectsMethodCall): array
{
/** @var Variable[] $usedVariables */
$usedVariables = $this->findInstancesOfScoped($expectsMethodCall->getArgs(), Variable::class);
$uniqueUsedVariables = [];
foreach ($usedVariables as $usedVariable) {
if ($this->isName($usedVariable, 'this')) {
continue;
}
$usedVariableName = $this->getName($usedVariable);
$uniqueUsedVariables[$usedVariableName] = $usedVariable;
}
return $uniqueUsedVariables;
}
}