|
8 | 8 | use PhpParser\Node\Expr\Array_; |
9 | 9 | use PhpParser\Node\Expr\MethodCall; |
10 | 10 | use PhpParser\Node\Expr\StaticCall; |
11 | | -use PhpParser\Node\Expr\Variable; |
12 | 11 | use PhpParser\Node\InterpolatedStringPart; |
13 | 12 | use PhpParser\Node\Scalar\InterpolatedString; |
14 | 13 | use PhpParser\Node\Scalar\String_; |
@@ -99,50 +98,34 @@ private function processStringRule(String_ $string): Array_ |
99 | 98 |
|
100 | 99 | private function processInterpolatedStringRule(InterpolatedString $interpolatedString): Array_ |
101 | 100 | { |
102 | | - $intermediateRules = []; |
| 101 | + // Build rule parts by splitting on pipes while preserving variables |
| 102 | + $allRuleParts = []; |
| 103 | + $currentParts = []; |
103 | 104 |
|
104 | 105 | foreach ($interpolatedString->parts as $part) { |
105 | 106 | if ($part instanceof InterpolatedStringPart) { |
106 | | - $newParts = explode('|', $part->value); |
107 | | - $intermediateRules[] = new InterpolatedString(array_map(static fn ($part) => new InterpolatedStringPart($part), $newParts)); |
108 | | - } elseif ($part instanceof Variable) { |
109 | | - $intermediateRules[] = $part; |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - $finalRules = []; |
114 | | - foreach ($intermediateRules as $key => $rulePart) { |
115 | | - $nextRule = $intermediateRules[$key + 1] ?? null; |
116 | | - $prevRule = $intermediateRules[$key - 1] ?? null; |
117 | | - |
118 | | - if ($rulePart instanceof Variable) { |
119 | | - $finalRule = new InterpolatedString([$rulePart]); |
120 | | - |
121 | | - if ($prevRule instanceof InterpolatedString) { |
122 | | - $lastPart = array_pop($prevRule->parts); |
123 | | - if ($lastPart instanceof InterpolatedStringPart) { |
124 | | - array_unshift($finalRule->parts, $lastPart); |
| 107 | + $segments = explode('|', $part->value); |
| 108 | + foreach ($segments as $index => $segment) { |
| 109 | + // Complete current rule and start new one |
| 110 | + if ($index !== 0 && $currentParts !== []) { |
| 111 | + $allRuleParts[] = $currentParts; |
| 112 | + $currentParts = []; |
125 | 113 | } |
126 | | - $intermediateRules[$key - 1] = $prevRule; |
127 | | - } |
128 | 114 |
|
129 | | - if ($nextRule instanceof InterpolatedString) { |
130 | | - $firstPart = array_shift($nextRule->parts); |
131 | | - if ($firstPart instanceof InterpolatedStringPart) { |
132 | | - $finalRule->parts[] = $firstPart; |
| 115 | + if ($segment !== '') { |
| 116 | + $currentParts[] = new InterpolatedStringPart($segment); |
133 | 117 | } |
134 | | - $intermediateRules[$key + 1] = $nextRule; |
135 | 118 | } |
136 | | - |
137 | | - $finalRules[] = $finalRule; |
138 | 119 | } else { |
139 | | - $finalRules[] = $rulePart; |
| 120 | + $currentParts[] = $part; // Variables and other parts |
140 | 121 | } |
141 | 122 | } |
142 | 123 |
|
143 | | - $finalRules = array_filter($finalRules, fn (InterpolatedString $interpolatedString) => count($interpolatedString->parts) > 0); |
| 124 | + if ($currentParts !== []) { |
| 125 | + $allRuleParts[] = $currentParts; |
| 126 | + } |
144 | 127 |
|
145 | | - return new Array_(array_map(static fn (InterpolatedString $interpolatedString) => new ArrayItem($interpolatedString), $finalRules)); |
| 128 | + return new Array_(array_map(fn ($parts) => new ArrayItem(new InterpolatedString($parts)), $allRuleParts)); |
146 | 129 | } |
147 | 130 |
|
148 | 131 | private function refactorCall(StaticCall|MethodCall $node): StaticCall|MethodCall|null |
|
0 commit comments