@@ -297,7 +297,8 @@ private function buildArrayType(
297
297
$ i = 0 ;
298
298
foreach ($ captureGroups as $ captureGroup ) {
299
299
$ isTrailingOptional = $ i >= $ countGroups - $ trailingOptionals ;
300
- $ groupValueType = $ this ->createGroupValueType ($ captureGroup , $ wasMatched , $ flags , $ isTrailingOptional , $ matchesAll );
300
+ $ isLastGroup = $ i === $ countGroups - 1 ;
301
+ $ groupValueType = $ this ->createGroupValueType ($ captureGroup , $ wasMatched , $ flags , $ isTrailingOptional , $ isLastGroup , $ matchesAll );
301
302
$ optional = $ this ->isGroupOptional ($ captureGroup , $ wasMatched , $ flags , $ isTrailingOptional , $ matchesAll );
302
303
303
304
if ($ captureGroup ->isNamed ()) {
@@ -390,11 +391,11 @@ private function isGroupOptional(RegexCapturingGroup $captureGroup, TrinaryLogic
390
391
return $ optional ;
391
392
}
392
393
393
- private function createGroupValueType (RegexCapturingGroup $ captureGroup , TrinaryLogic $ wasMatched , int $ flags , bool $ isTrailingOptional , bool $ matchesAll ): Type
394
+ private function createGroupValueType (RegexCapturingGroup $ captureGroup , TrinaryLogic $ wasMatched , int $ flags , bool $ isTrailingOptional , bool $ isLastGroup , bool $ matchesAll ): Type
394
395
{
395
- $ groupValueType = $ this ->getValueType ($ captureGroup ->getType (), $ flags , $ matchesAll );
396
-
397
396
if ($ matchesAll ) {
397
+ $ groupValueType = $ this ->getValueType ($ captureGroup ->getType (), $ flags , $ matchesAll );
398
+
398
399
if (!$ isTrailingOptional && $ this ->containsUnmatchedAsNull ($ flags , $ matchesAll ) && !$ captureGroup ->isOptional ()) {
399
400
$ groupValueType = TypeCombinator::removeNull ($ groupValueType );
400
401
}
@@ -411,16 +412,22 @@ private function createGroupValueType(RegexCapturingGroup $captureGroup, Trinary
411
412
return $ groupValueType ;
412
413
}
413
414
415
+ if (!$ isLastGroup && !$ this ->containsUnmatchedAsNull ($ flags , $ matchesAll ) && $ captureGroup ->isOptional ()) {
416
+ $ groupValueType = $ this ->getValueType (
417
+ TypeCombinator::union ($ captureGroup ->getType (), new ConstantStringType ('' )),
418
+ $ flags ,
419
+ $ matchesAll ,
420
+ );
421
+ } else {
422
+ $ groupValueType = $ this ->getValueType ($ captureGroup ->getType (), $ flags , $ matchesAll );
423
+ }
424
+
414
425
if ($ wasMatched ->yes ()) {
415
426
if (!$ isTrailingOptional && $ this ->containsUnmatchedAsNull ($ flags , $ matchesAll ) && !$ captureGroup ->isOptional ()) {
416
427
$ groupValueType = TypeCombinator::removeNull ($ groupValueType );
417
428
}
418
429
}
419
430
420
- if (!$ isTrailingOptional && !$ this ->containsUnmatchedAsNull ($ flags , $ matchesAll ) && $ captureGroup ->isOptional ()) {
421
- $ groupValueType = TypeCombinator::union ($ groupValueType , new ConstantStringType ('' ));
422
- }
423
-
424
431
return $ groupValueType ;
425
432
}
426
433
0 commit comments