@@ -61,6 +61,13 @@ final class GenericValidator implements TypeValidatorInterface
6161 */
6262 private static array $ enumValueCache = [];
6363
64+ /**
65+ * Cache for resolved int-mask-of masks: [$cacheKey] => array{0: int, 1: bool}.
66+ *
67+ * @var array<string, array{0: int, 1: bool}>
68+ */
69+ private static array $ maskOfCache = [];
70+
6471 /**
6572 * Validates a value against a GenericTypeNode AST.
6673 */
@@ -280,6 +287,18 @@ private function validateIntMask(mixed $value, GenericTypeNode $node, string $co
280287 $ allowedMask |= $ constVal ;
281288 }
282289 }
290+ } elseif ($ typeNode instanceof IdentifierTypeNode) {
291+ $ constName = $ typeNode ->name ;
292+ if (str_contains ($ constName , ':: ' )) {
293+ [$ className , $ name ] = explode (':: ' , $ constName , 2 );
294+ $ constVal = $ this ->resolveConstantValue ($ className , $ name );
295+ } else {
296+ $ constVal = $ this ->resolveConstantValue ('' , $ constName );
297+ }
298+
299+ if (\is_int ($ constVal )) {
300+ $ allowedMask |= $ constVal ;
301+ }
283302 }
284303 }
285304
@@ -300,36 +319,104 @@ private function validateIntMaskOf(mixed $value, GenericTypeNode $node, string $
300319 $ allowedMask = 0 ;
301320 $ foundFlags = false ;
302321
322+ $ className = '' ;
323+ $ pattern = '' ;
324+
303325 if ($ targetType instanceof ConstTypeNode && $ targetType ->constExpr instanceof ConstFetchNode) {
304- $ constExpr = $ targetType ->constExpr ;
305- $ fqcn = $ constExpr ->className ;
306- $ pattern = $ constExpr ->name ;
326+ $ className = $ targetType ->constExpr ->className ;
327+ $ pattern = $ targetType ->constExpr ->name ;
328+ } elseif ($ targetType instanceof ConstTypeNode && $ targetType ->constExpr instanceof ConstExprStringNode) {
329+ $ className = '' ;
330+ $ pattern = $ targetType ->constExpr ->value ;
331+ } elseif ($ targetType instanceof IdentifierTypeNode) {
332+ if (str_contains ($ targetType ->name , ':: ' )) {
333+ [$ className , $ pattern ] = explode (':: ' , $ targetType ->name , 2 );
334+ } else {
335+ $ className = '' ;
336+ $ pattern = $ targetType ->name ;
337+ }
338+ } elseif ($ targetType instanceof UnionTypeNode) {
339+ foreach ($ targetType ->types as $ unionMember ) {
340+ if ($ unionMember instanceof ConstTypeNode) {
341+ $ expr = $ unionMember ->constExpr ;
342+ if ($ expr instanceof ConstExprIntegerNode) {
343+ $ allowedMask |= (int ) $ expr ->value ;
344+ $ foundFlags = true ;
345+ } elseif ($ expr instanceof ConstFetchNode) {
346+ $ val = $ this ->resolveConstantValue ($ expr ->className , $ expr ->name );
347+ if (\is_int ($ val )) {
348+ $ allowedMask |= $ val ;
349+ $ foundFlags = true ;
350+ }
351+ }
352+ } elseif ($ unionMember instanceof IdentifierTypeNode) {
353+ $ name = $ unionMember ->name ;
354+ if (str_contains ($ name , ':: ' )) {
355+ [$ cls , $ cName ] = explode (':: ' , $ name , 2 );
356+ $ val = $ this ->resolveConstantValue ($ cls , $ cName );
357+ } else {
358+ $ val = $ this ->resolveConstantValue ('' , $ name );
359+ }
360+ if (\is_int ($ val )) {
361+ $ allowedMask |= $ val ;
362+ $ foundFlags = true ;
363+ }
364+ }
365+ }
366+ }
307367
308- if ($ fqcn !== '' && ( class_exists ( $ fqcn ) || interface_exists ( $ fqcn )) ) {
309- $ refClass = new \ ReflectionClass ( $ fqcn ) ;
368+ if ($ pattern !== '' ) {
369+ $ cacheKey = $ className !== '' ? " $ className :: $ pattern " : $ pattern ;
310370
311- if (str_contains ($ pattern , '* ' )) {
312- $ regex = '/^ ' . str_replace ('\* ' , '.* ' , preg_quote ($ pattern , '/ ' )) . '$/i ' ;
313- foreach ($ refClass ->getConstants () as $ cName => $ cValue ) {
314- if (\is_int ($ cValue ) && preg_match ($ regex , $ cName ) === 1 ) {
315- $ allowedMask |= $ cValue ;
316- $ foundFlags = true ;
371+ if (isset (self ::$ maskOfCache [$ cacheKey ])) {
372+ [$ allowedMask , $ foundFlags ] = self ::$ maskOfCache [$ cacheKey ];
373+ } else {
374+ if ($ className !== '' ) {
375+ if (class_exists ($ className ) || interface_exists ($ className )) {
376+ $ refClass = new \ReflectionClass ($ className );
377+
378+ if (str_contains ($ pattern , '* ' )) {
379+ $ regex = '/^ ' . str_replace ('\* ' , '.* ' , preg_quote ($ pattern , '/ ' )) . '$/i ' ;
380+ foreach ($ refClass ->getConstants () as $ cName => $ cValue ) {
381+ if (\is_int ($ cValue ) && preg_match ($ regex , $ cName ) === 1 ) {
382+ $ allowedMask |= $ cValue ;
383+ $ foundFlags = true ;
384+ }
385+ }
386+ } else {
387+ $ cValue = $ this ->resolveConstantValue ($ className , $ pattern );
388+ if (\is_int ($ cValue )) {
389+ $ allowedMask |= $ cValue ;
390+ $ foundFlags = true ;
391+ } elseif (\is_array ($ cValue )) {
392+ foreach ($ cValue as $ item ) {
393+ if (\is_int ($ item )) {
394+ $ allowedMask |= $ item ;
395+ $ foundFlags = true ;
396+ }
397+ }
398+ }
317399 }
318400 }
319401 } else {
320- $ cValue = $ this ->resolveConstantValue ($ fqcn , $ pattern );
321- if (\is_int ($ cValue )) {
322- $ allowedMask |= $ cValue ;
323- $ foundFlags = true ;
324- } elseif (\is_array ($ cValue )) {
325- foreach ($ cValue as $ item ) {
326- if (\is_int ($ item )) {
327- $ allowedMask |= $ item ;
402+ if (str_contains ($ pattern , '* ' )) {
403+ $ regex = '/^ ' . str_replace ('\* ' , '.* ' , preg_quote ($ pattern , '/ ' )) . '$/i ' ;
404+ foreach (get_defined_constants () as $ cName => $ cValue ) {
405+ if (\is_int ($ cValue ) && preg_match ($ regex , $ cName ) === 1 ) {
406+ $ allowedMask |= $ cValue ;
328407 $ foundFlags = true ;
329408 }
330409 }
410+ } else {
411+ $ cValue = $ this ->resolveConstantValue ('' , $ pattern );
412+ if (\is_int ($ cValue )) {
413+ $ allowedMask |= $ cValue ;
414+ $ foundFlags = true ;
415+ }
331416 }
332417 }
418+
419+ self ::$ maskOfCache [$ cacheKey ] = [$ allowedMask , $ foundFlags ];
333420 }
334421 }
335422
0 commit comments