|
3 | 3 | namespace Rector\PHPUnit\Rector;
|
4 | 4 |
|
5 | 5 | use PhpParser\Node;
|
6 |
| -use PhpParser\Node\Arg; |
7 | 6 | use PhpParser\Node\Expr\MethodCall;
|
| 7 | +use PhpParser\Node\Expr\StaticCall; |
8 | 8 | use PhpParser\Node\Identifier;
|
9 | 9 | use Rector\Rector\AbstractPHPUnitRector;
|
10 | 10 | use Rector\RectorDefinition\CodeSample;
|
@@ -42,46 +42,41 @@ public function getDefinition(): RectorDefinition
|
42 | 42 | */
|
43 | 43 | public function getNodeTypes(): array
|
44 | 44 | {
|
45 |
| - return [MethodCall::class]; |
| 45 | + return [MethodCall::class, StaticCall::class]; |
46 | 46 | }
|
47 | 47 |
|
48 | 48 | /**
|
49 |
| - * @param MethodCall $node |
| 49 | + * @param MethodCall|StaticCall $node |
50 | 50 | */
|
51 | 51 | public function refactor(Node $node): ?Node
|
52 | 52 | {
|
53 |
| - if (! $this->isInTestClass($node)) { |
| 53 | + if (! $this->isPHPUnitMethodNames($node, array_keys($this->oldToNewMethod))) { |
54 | 54 | return null;
|
55 | 55 | }
|
56 | 56 |
|
57 |
| - if (! $this->isNames($node, array_keys($this->oldToNewMethod))) { |
58 |
| - return null; |
59 |
| - } |
| 57 | + if (isset($node->args[1])) { |
| 58 | + /** @var Identifier $identifierNode */ |
| 59 | + $identifierNode = $node->name; |
| 60 | + $oldMethodName = $identifierNode->name; |
60 | 61 |
|
61 |
| - if (! isset($node->args[1])) { |
62 |
| - return null; |
63 |
| - } |
| 62 | + $call = $this->createPHPUnitCallWithName($node, $this->oldToNewMethod[$oldMethodName]); |
| 63 | + $call->args[] = $node->args[1]; |
| 64 | + $this->addNodeAfterNode($call, $node); |
64 | 65 |
|
65 |
| - /** @var Identifier $identifierNode */ |
66 |
| - $identifierNode = $node->name; |
67 |
| - $oldMethodName = $identifierNode->name; |
| 66 | + unset($node->args[1]); |
68 | 67 |
|
69 |
| - $this->addNewMethodCall($node, $this->oldToNewMethod[$oldMethodName], $node->args[1]); |
70 |
| - unset($node->args[1]); |
| 68 | + // add exception code method call |
| 69 | + if (isset($node->args[2])) { |
| 70 | + $call = $this->createPHPUnitCallWithName($node, 'expectExceptionCode'); |
| 71 | + $call->args[] = $node->args[2]; |
| 72 | + $this->addNodeAfterNode($call, $node); |
71 | 73 |
|
72 |
| - // add exception code method call |
73 |
| - if (isset($node->args[2])) { |
74 |
| - $this->addNewMethodCall($node, 'expectExceptionCode', $node->args[2]); |
75 |
| - unset($node->args[2]); |
| 74 | + unset($node->args[2]); |
| 75 | + } |
76 | 76 | }
|
77 | 77 |
|
78 |
| - return $node; |
79 |
| - } |
80 |
| - |
81 |
| - private function addNewMethodCall(MethodCall $methodCall, string $methodName, Arg $arg): void |
82 |
| - { |
83 |
| - $expectExceptionMessageMethodCall = $this->createMethodCall('this', $methodName, [$arg]); |
| 78 | + $node->name = new Identifier('expectException'); |
84 | 79 |
|
85 |
| - $this->addNodeAfterNode($expectExceptionMessageMethodCall, $methodCall); |
| 80 | + return $node; |
86 | 81 | }
|
87 | 82 | }
|
0 commit comments