|
4 | 4 |
|
5 | 5 | namespace Rector\DowngradePhp72\NodeManipulator; |
6 | 6 |
|
7 | | -use PhpParser\Node; |
8 | 7 | use PhpParser\Node\Expr; |
9 | 8 | use PhpParser\Node\Expr\BinaryOp\BitwiseOr; |
10 | 9 | use PhpParser\Node\Expr\ConstFetch; |
11 | 10 | use PhpParser\Node\Scalar\Int_; |
12 | 11 | use Rector\Enum\JsonConstant; |
13 | 12 | use Rector\NodeNameResolver\NodeNameResolver; |
14 | | -use Rector\NodeTypeResolver\Node\AttributeKey; |
15 | 13 |
|
16 | 14 | final readonly class JsonConstCleaner |
17 | 15 | { |
@@ -47,35 +45,56 @@ private function cleanByConstFetch(ConstFetch $constFetch, array $constants): ?I |
47 | 45 | /** |
48 | 46 | * @param array<JsonConstant::*> $constants |
49 | 47 | */ |
50 | | - private function cleanByBitwiseOr(BitwiseOr $bitwiseOr, array $constants): null|Expr|Int_ |
| 48 | + private function cleanByBitwiseOr(BitwiseOr $bitwiseOr, array $constants): ?Expr |
51 | 49 | { |
52 | | - $isLeftTransformed = $this->isTransformed($bitwiseOr->left, $constants); |
53 | | - $isRightTransformed = $this->isTransformed($bitwiseOr->right, $constants); |
| 50 | + $keptOperands = []; |
| 51 | + $hasChanged = false; |
54 | 52 |
|
55 | | - if (! $isLeftTransformed && ! $isRightTransformed) { |
| 53 | + foreach ($this->flattenOperands($bitwiseOr) as $operand) { |
| 54 | + if ($operand instanceof ConstFetch && $this->nodeNameResolver->isNames($operand, $constants)) { |
| 55 | + $hasChanged = true; |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + $keptOperands[] = $operand; |
| 60 | + } |
| 61 | + |
| 62 | + if (! $hasChanged) { |
56 | 63 | return null; |
57 | 64 | } |
58 | 65 |
|
59 | | - if (! $isLeftTransformed) { |
60 | | - return $bitwiseOr->left; |
| 66 | + if ($keptOperands === []) { |
| 67 | + return new Int_(0); |
61 | 68 | } |
62 | 69 |
|
63 | | - if (! $isRightTransformed) { |
64 | | - return $bitwiseOr->right; |
| 70 | + $result = array_shift($keptOperands); |
| 71 | + foreach ($keptOperands as $keptOperand) { |
| 72 | + $result = new BitwiseOr($result, $keptOperand); |
65 | 73 | } |
66 | 74 |
|
67 | | - return new Int_(0); |
| 75 | + return $result; |
68 | 76 | } |
69 | 77 |
|
70 | 78 | /** |
71 | | - * @param string[] $constants |
| 79 | + * Flatten a left-associative bitwise-or chain into its operands. Structural, so dropping a |
| 80 | + * target constant does not depend on the ORIGINAL_NODE attribute, which is not reliable on |
| 81 | + * re-decorated nodes. |
| 82 | + * |
| 83 | + * @return Expr[] |
72 | 84 | */ |
73 | | - private function isTransformed(Expr $expr, array $constants): bool |
| 85 | + private function flattenOperands(BitwiseOr $bitwiseOr): array |
74 | 86 | { |
75 | | - if ($expr instanceof ConstFetch && $this->nodeNameResolver->isNames($expr, $constants)) { |
76 | | - return true; |
| 87 | + $operands = []; |
| 88 | + |
| 89 | + foreach ([$bitwiseOr->left, $bitwiseOr->right] as $operand) { |
| 90 | + if ($operand instanceof BitwiseOr) { |
| 91 | + $operands = [...$operands, ...$this->flattenOperands($operand)]; |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $operands[] = $operand; |
77 | 96 | } |
78 | 97 |
|
79 | | - return ! $expr->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node; |
| 98 | + return $operands; |
80 | 99 | } |
81 | 100 | } |
0 commit comments