|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Arg; |
9 | 9 | use PhpParser\Node\Expr\MethodCall; |
10 | | -use PhpParser\Node\Identifier; |
11 | 10 | use PhpParser\Node\Scalar\Int_; |
12 | 11 | use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
13 | 12 | use Rector\Rector\AbstractRector; |
|
19 | 18 | */ |
20 | 19 | final class ReplaceAtMethodWithDesiredMatcherRector extends AbstractRector |
21 | 20 | { |
22 | | - private bool $hasChanged = false; |
23 | | - |
24 | 21 | public function __construct( |
25 | 22 | private readonly TestsNodeAnalyzer $testsNodeAnalyzer |
26 | 23 | ) { |
@@ -57,70 +54,69 @@ public function getNodeTypes(): array |
57 | 54 | } |
58 | 55 |
|
59 | 56 | /** |
60 | | - * @param MethodCall $node |
| 57 | + * @param MethodCall $node |
61 | 58 | */ |
62 | 59 | public function refactor(Node $node): null|MethodCall |
63 | 60 | { |
64 | | - $this->hasChanged = false; |
65 | | - |
66 | 61 | if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
67 | 62 | return null; |
68 | 63 | } |
69 | 64 |
|
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; |
90 | 67 | } |
91 | 68 |
|
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; |
94 | 72 | } |
95 | 73 |
|
96 | | - return null; |
97 | | - } |
98 | | - |
99 | | - private function replaceWithDesiredMatcher(Arg $arg): void |
100 | | - { |
101 | 74 | if (! $arg->value instanceof MethodCall) { |
102 | | - return; |
| 75 | + return null; |
103 | 76 | } |
104 | 77 |
|
| 78 | + $count = null; |
105 | 79 | foreach ($arg->value->getArgs() as $item) { |
106 | 80 | if ($item->value instanceof Int_) { |
107 | 81 | $count = $item->value->value; |
108 | 82 | } |
109 | 83 | } |
110 | 84 |
|
111 | 85 | if (! isset($count)) { |
112 | | - return; |
| 86 | + return null; |
113 | 87 | } |
114 | 88 |
|
115 | 89 | if ($count === 0) { |
116 | 90 | $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) { |
119 | 95 | $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) { |
122 | 100 | $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 | + } |
124 | 114 | } |
| 115 | + |
| 116 | + if ($methodCall->var instanceof MethodCall) { |
| 117 | + $this->findAtMethodCall($methodCall->var); |
| 118 | + } |
| 119 | + |
| 120 | + return null; |
125 | 121 | } |
126 | 122 | } |
0 commit comments