@@ -123,19 +123,18 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
123
123
$ trailingOptionals ++;
124
124
}
125
125
126
- $ onlyOptionalTopLevelGroup = $ this ->getOnlyOptionalTopLevelGroup ($ groupList );
126
+ $ onlyOptionalTopLevelGroupIndex = $ this ->getOnlyOptionalTopLevelGroupIndex ($ groupList );
127
127
$ onlyTopLevelAlternation = $ this ->getOnlyTopLevelAlternation ($ groupList );
128
128
$ flags ??= 0 ;
129
129
130
130
if (
131
131
!$ matchesAll
132
132
&& $ wasMatched ->yes ()
133
- && $ onlyOptionalTopLevelGroup !== null
133
+ && $ onlyOptionalTopLevelGroupIndex !== null
134
134
) {
135
135
// if only one top level capturing optional group exists
136
136
// we build a more precise tagged union of a empty-match and a match with the group
137
-
138
- $ onlyOptionalTopLevelGroup ->forceNonOptional ();
137
+ $ groupList [$ onlyOptionalTopLevelGroupIndex ] = $ groupList [$ onlyOptionalTopLevelGroupIndex ]->forceNonOptional ();
139
138
140
139
$ combiType = $ this ->buildArrayType (
141
140
$ groupList ,
@@ -154,12 +153,10 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
154
153
);
155
154
}
156
155
157
- $ onlyOptionalTopLevelGroup ->clearOverrides ();
158
-
159
156
return $ combiType ;
160
157
} elseif (
161
158
!$ matchesAll
162
- && $ onlyOptionalTopLevelGroup === null
159
+ && $ onlyOptionalTopLevelGroupIndex === null
163
160
&& $ onlyTopLevelAlternation !== null
164
161
&& !$ wasMatched ->no ()
165
162
) {
@@ -174,13 +171,14 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
174
171
foreach ($ comboList as $ groupId => $ group ) {
175
172
if (in_array ($ groupId , $ groupCombo , true )) {
176
173
$ isOptionalAlternation = $ group ->inOptionalAlternation ();
177
- $ group ->forceNonOptional ();
174
+ $ forcedGroup = $ group ->forceNonOptional ();
178
175
$ beforeCurrentCombo = false ;
176
+ $ comboList [$ groupId ] = $ forcedGroup ;
179
177
} elseif ($ beforeCurrentCombo && !$ group ->resetsGroupCounter ()) {
180
- $ group ->forceNonOptional ();
181
- $ group ->forceType (
178
+ $ forcedGroup = $ group ->forceNonOptional ()->forceType (
182
179
$ this ->containsUnmatchedAsNull ($ flags , $ matchesAll ) ? new NullType () : new ConstantStringType ('' ),
183
180
);
181
+ $ comboList [$ groupId ] = $ forcedGroup ;
184
182
} elseif (
185
183
$ group ->getAlternationId () === $ onlyTopLevelAlternation ->getId ()
186
184
&& !$ this ->containsUnmatchedAsNull ($ flags , $ matchesAll )
@@ -199,11 +197,6 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
199
197
);
200
198
201
199
$ combiTypes [] = $ combiType ;
202
-
203
- foreach ($ groupCombo as $ groupId ) {
204
- $ group = $ comboList [$ groupId ];
205
- $ group ->clearOverrides ();
206
- }
207
200
}
208
201
209
202
if (
@@ -235,10 +228,10 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
235
228
/**
236
229
* @param array<int, RegexCapturingGroup> $captureGroups
237
230
*/
238
- private function getOnlyOptionalTopLevelGroup (array $ captureGroups ): ?RegexCapturingGroup
231
+ private function getOnlyOptionalTopLevelGroupIndex (array $ captureGroups ): ?int
239
232
{
240
- $ group = null ;
241
- foreach ($ captureGroups as $ captureGroup ) {
233
+ $ groupIndex = null ;
234
+ foreach ($ captureGroups as $ i => $ captureGroup ) {
242
235
if (!$ captureGroup ->isTopLevel ()) {
243
236
continue ;
244
237
}
@@ -247,14 +240,14 @@ private function getOnlyOptionalTopLevelGroup(array $captureGroups): ?RegexCaptu
247
240
return null ;
248
241
}
249
242
250
- if ($ group !== null ) {
243
+ if ($ groupIndex !== null ) {
251
244
return null ;
252
245
}
253
246
254
- $ group = $ captureGroup ;
247
+ $ groupIndex = $ i ;
255
248
}
256
249
257
- return $ group ;
250
+ return $ groupIndex ;
258
251
}
259
252
260
253
/**
0 commit comments