|
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr; |
| 10 | +use PhpParser\Node\Expr\FuncCall; |
9 | 11 | use PhpParser\Node\Expr\MethodCall; |
10 | 12 | use PhpParser\Node\Expr\Ternary; |
11 | 13 | use PhpParser\Node\Scalar\String_; |
|
19 | 21 | */ |
20 | 22 | final class DowngradeIsEnumRector extends AbstractRector |
21 | 23 | { |
| 24 | + private const string IS_IF_TERNARY = 'is_if_ternary'; |
| 25 | + |
22 | 26 | public function getRuleDefinition(): RuleDefinition |
23 | 27 | { |
24 | 28 | return new RuleDefinition('Downgrades isEnum() on class reflection', [ |
@@ -52,18 +56,33 @@ public function run(ReflectionClass $reflectionClass) |
52 | 56 | */ |
53 | 57 | public function getNodeTypes(): array |
54 | 58 | { |
55 | | - return [MethodCall::class]; |
| 59 | + return [Ternary::class, MethodCall::class]; |
56 | 60 | } |
57 | 61 |
|
58 | 62 | /** |
59 | | - * @param MethodCall $node |
| 63 | + * @param Ternary|MethodCall $node |
60 | 64 | */ |
61 | 65 | public function refactor(Node $node): ?Node |
62 | 66 | { |
| 67 | + if ($node instanceof Ternary) { |
| 68 | + if ($node->if instanceof Expr |
| 69 | + && $node->cond instanceof FuncCall |
| 70 | + && $this->isName($node->cond, 'method_exists')) { |
| 71 | + $node->if->setAttribute(self::IS_IF_TERNARY, true); |
| 72 | + } |
| 73 | + |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
63 | 77 | if (! $this->isName($node->name, 'isEnum')) { |
64 | 78 | return null; |
65 | 79 | } |
66 | 80 |
|
| 81 | + // already wrapped by this rule on a previous pass |
| 82 | + if ($node->getAttribute(self::IS_IF_TERNARY) === true) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
67 | 86 | if (! $this->isObjectType($node->var, new ObjectType('ReflectionClass'))) { |
68 | 87 | return null; |
69 | 88 | } |
|
0 commit comments