1313use PHPStan \PhpDocParser \Ast \Type \IdentifierTypeNode ;
1414use PHPStan \PhpDocParser \Ast \Type \TypeNode ;
1515use TypePHP \Internal \ClassNameValidator ;
16+ use TypePHP \Internal \Config ;
1617use TypePHP \Internal \ErrorFactory ;
1718use TypePHP \Internal \ErrorMessage ;
1819use TypePHP \Internal \RuntimeTypeChecker ;
@@ -361,7 +362,6 @@ private function validateClassString(mixed $value, GenericTypeNode $node, string
361362
362363 /**
363364 * Validates sequential list structures (e.g. list<string> or non-empty-list<int>).
364- * Uses Beartype-style O(1) hybrid sampling for large lists (> 64 items).
365365 */
366366 private function validateList (mixed $ value , GenericTypeNode $ node , string $ context , TypeValidatorRegistry $ registry ): ?ErrorMessage
367367 {
@@ -381,8 +381,7 @@ private function validateList(mixed $value, GenericTypeNode $node, string $conte
381381 if ($ valueTypeNode !== null && $ count > 0 ) {
382382 $ isComplexObjectGeneric = ($ valueTypeNode instanceof GenericTypeNode && ! \in_array (strtolower ($ valueTypeNode ->type ->name ), ['class-string ' , 'list ' , 'array ' , 'iterable ' ], strict: true ));
383383
384- // O(1) Beartype Hybrid Sampling for large lists (> 64 items)
385- if ($ count > self ::HYBRID_SAMPLE_THRESHOLD ) {
384+ if ($ count > self ::HYBRID_SAMPLE_THRESHOLD && Config::isArrayValidationHybrid ()) {
386385 $ sampleIndices = [0 , $ count - 1 ];
387386 $ samplesToTake = min (3 , $ count - 2 );
388387 for ($ i = 0 ; $ i < $ samplesToTake ; $ i ++) {
@@ -403,7 +402,6 @@ private function validateList(mixed $value, GenericTypeNode $node, string $conte
403402 return null ;
404403 }
405404
406- // Full O(n) scan for small/medium lists (<= 64 items)
407405 foreach ($ value as $ k => $ v ) {
408406 $ err = $ isComplexObjectGeneric
409407 ? $ this ->validateObjectGeneric ($ v , $ valueTypeNode , '' )
@@ -420,7 +418,6 @@ private function validateList(mixed $value, GenericTypeNode $node, string $conte
420418
421419 /**
422420 * Validates key-value array structures (e.g. array<string, int>).
423- * Uses Beartype-style O(1) hybrid sampling for large maps (> 64 items).
424421 */
425422 private function validateArray (mixed $ value , GenericTypeNode $ node , string $ context , TypeValidatorRegistry $ registry ): ?ErrorMessage
426423 {
@@ -448,8 +445,7 @@ private function validateArray(mixed $value, GenericTypeNode $node, string $cont
448445 if ($ typesCount === 1 ) {
449446 $ valTypeNode = $ node ->genericTypes [0 ];
450447 $ isComplexObjectGeneric = ($ valTypeNode instanceof GenericTypeNode && ! \in_array (strtolower ($ valTypeNode ->type ->name ), ['class-string ' , 'list ' , 'array ' , 'iterable ' ], strict: true ));
451-
452- if ($ count > self ::HYBRID_SAMPLE_THRESHOLD ) {
448+ if ($ count > self ::HYBRID_SAMPLE_THRESHOLD && Config::isArrayValidationHybrid ()) {
453449 $ keys = array_keys ($ value );
454450 $ sampleKeys = [$ keys [0 ], $ keys [$ count - 1 ]];
455451 $ samplesToTake = min (3 , $ count - 2 );
@@ -485,7 +481,7 @@ private function validateArray(mixed $value, GenericTypeNode $node, string $cont
485481 $ valTypeNode = $ node ->genericTypes [1 ];
486482 $ isComplexObjectGeneric = ($ valTypeNode instanceof GenericTypeNode && ! \in_array (strtolower ($ valTypeNode ->type ->name ), ['class-string ' , 'list ' , 'array ' , 'iterable ' ], strict: true ));
487483
488- if ($ count > self ::HYBRID_SAMPLE_THRESHOLD ) {
484+ if ($ count > self ::HYBRID_SAMPLE_THRESHOLD && Config:: isArrayValidationHybrid () ) {
489485 $ keys = array_keys ($ value );
490486 $ sampleKeys = [$ keys [0 ], $ keys [$ count - 1 ]];
491487 $ samplesToTake = min (3 , $ count - 2 );
@@ -547,4 +543,4 @@ private function validateObjectGeneric(mixed $value, GenericTypeNode $node, stri
547543
548544 return RuntimeTypeChecker::bindInstanceFromNode ($ value , $ node , $ context );
549545 }
550- }
546+ }
0 commit comments