@@ -1141,7 +1141,23 @@ public function offsetGet(mixed $option, bool $triggerDeprecation = true): mixed
1141
1141
1142
1142
private function verifyTypes (string $ type , mixed $ value , ?array &$ invalidTypes = null , int $ level = 0 ): bool
1143
1143
{
1144
- if ($ this ->isEnclosedWithParenthesis ($ type )) {
1144
+ $ allowedTypes = $ this ->splitOutsideParenthesis ($ type );
1145
+ if (\count ($ allowedTypes ) > 1 ) {
1146
+ foreach ($ allowedTypes as $ allowedType ) {
1147
+ if ($ this ->verifyTypes ($ allowedType , $ value )) {
1148
+ return true ;
1149
+ }
1150
+ }
1151
+
1152
+ if (\is_array ($ invalidTypes ) && (!$ invalidTypes || $ level > 0 )) {
1153
+ $ invalidTypes [get_debug_type ($ value )] = true ;
1154
+ }
1155
+
1156
+ return false ;
1157
+ }
1158
+
1159
+ $ type = $ allowedTypes [0 ];
1160
+ if (str_starts_with ($ type , '( ' ) && str_ends_with ($ type , ') ' )) {
1145
1161
return $ this ->verifyTypes (substr ($ type , 1 , -1 ), $ value , $ invalidTypes , $ level );
1146
1162
}
1147
1163
@@ -1158,14 +1174,7 @@ private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes =
1158
1174
return $ valid ;
1159
1175
}
1160
1176
1161
- if (str_contains ($ type , '| ' )) {
1162
- $ allowedTypes = preg_split ('/\|(?![^\(]*\))/ ' , $ type );
1163
- foreach ($ allowedTypes as $ allowedType ) {
1164
- if ($ this ->verifyTypes ($ allowedType , $ value )) {
1165
- return true ;
1166
- }
1167
- }
1168
- } elseif (('null ' === $ type && null === $ value ) || (isset (self ::VALIDATION_FUNCTIONS [$ type ]) ? self ::VALIDATION_FUNCTIONS [$ type ]($ value ) : $ value instanceof $ type )) {
1177
+ if (('null ' === $ type && null === $ value ) || (isset (self ::VALIDATION_FUNCTIONS [$ type ]) ? self ::VALIDATION_FUNCTIONS [$ type ]($ value ) : $ value instanceof $ type )) {
1169
1178
return true ;
1170
1179
}
1171
1180
@@ -1176,24 +1185,38 @@ private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes =
1176
1185
return false ;
1177
1186
}
1178
1187
1179
- private function isEnclosedWithParenthesis (string $ string ): bool
1188
+ /**
1189
+ * @return list<string>
1190
+ */
1191
+ private function splitOutsideParenthesis (string $ type ): array
1180
1192
{
1181
- if (!str_starts_with ($ string , '( ' ) || !str_ends_with ($ string , ') ' )) {
1182
- return false ;
1183
- }
1193
+ $ parts = [];
1194
+ $ currentPart = '' ;
1195
+ $ parenthesisLevel = 0 ;
1196
+
1197
+ $ typeLength = strlen ($ type );
1198
+ for ($ i = 0 ; $ i < $ typeLength ; $ i ++) {
1199
+ $ char = $ type [$ i ];
1200
+
1201
+ if ('( ' === $ char ) {
1202
+ $ parenthesisLevel ++;
1203
+ } elseif (') ' === $ char ) {
1204
+ $ parenthesisLevel --;
1205
+ }
1184
1206
1185
- $ openedParenthesis = 1 ;
1186
- $ i = 1 ;
1187
- while (isset ($ string [$ i ]) && $ openedParenthesis > 0 ) {
1188
- if ('( ' === $ string [$ i ]) {
1189
- $ openedParenthesis ++;
1190
- } elseif (') ' === $ string [$ i ]) {
1191
- $ openedParenthesis --;
1207
+ if ('| ' === $ char && 0 === $ parenthesisLevel ) {
1208
+ $ parts [] = $ currentPart ;
1209
+ $ currentPart = '' ;
1210
+ } else {
1211
+ $ currentPart .= $ char ;
1192
1212
}
1193
- ++$ i ;
1194
1213
}
1195
1214
1196
- return $ i === \strlen ($ string );
1215
+ if ($ currentPart !== '' ) {
1216
+ $ parts [] = $ currentPart ;
1217
+ }
1218
+
1219
+ return $ parts ;
1197
1220
}
1198
1221
1199
1222
/**
0 commit comments