@@ -165,7 +165,7 @@ public static function checkParams(
165165 return self ::validateSimpleParams ($ contract ['types ' ], $ vars , $ effectiveFunction , $ registry );
166166 }
167167
168- self ::prepareGenericBindings ($ effectiveFunction , $ methodTemplates , $ thisObj , $ classTemplates );
168+ self ::prepareGenericBindings ($ effectiveFunction , $ methodTemplates , $ thisObj , $ classTemplates, $ thisOrClass );
169169
170170 /** @var array<string, TemplateTagValueNode> $allTemplates */
171171 $ allTemplates = [...$ classTemplates , ...$ methodTemplates ];
@@ -175,7 +175,7 @@ public static function checkParams(
175175 }
176176
177177 $ boundTemplates = (\count ($ allTemplates ) > 0 )
178- ? TemplateManager ::getBoundTemplates ($ effectiveFunction , $ thisObj , $ allTemplates )
178+ ? TemplateManager ::getBoundTemplates ($ effectiveFunction , $ thisOrClass , $ allTemplates )
179179 : [];
180180 $ declaredTemplates = $ allTemplates ;
181181
@@ -192,7 +192,8 @@ public static function checkParams(
192192 $ boundTemplates ,
193193 $ declaredTemplates ,
194194 $ registry ,
195- $ classTemplates
195+ $ classTemplates ,
196+ $ thisOrClass
196197 );
197198 }
198199
@@ -240,15 +241,21 @@ private static function prepareGenericBindings(
240241 string $ effectiveFunction ,
241242 array $ methodTemplates ,
242243 ?object $ thisObj ,
243- array $ classTemplates
244+ array $ classTemplates ,
245+ object |string |null $ thisOrClass = null
244246 ): void {
245247 if (\count ($ methodTemplates ) > 0 ) {
246248 TemplateManager ::clearCallBindings ($ effectiveFunction , $ methodTemplates );
247249 }
248250
249- if ($ thisObj !== null && \count ($ classTemplates ) > 0 && ! TemplateManager :: hasInstanceBindings ( $ thisObj ) && str_contains ($ effectiveFunction , ':: ' )) {
251+ if (\count ($ classTemplates ) > 0 && str_contains ($ effectiveFunction , ':: ' )) {
250252 $ declaringClass = explode (':: ' , $ effectiveFunction , 2 )[0 ];
251- TemplateManager ::resolveInheritedTemplates ($ thisObj , $ declaringClass );
253+ if ($ thisObj !== null && ! TemplateManager ::hasInstanceBindings ($ thisObj )) {
254+ TemplateManager ::resolveInheritedTemplates ($ thisObj , $ declaringClass );
255+ } elseif ($ thisObj === null ) {
256+ $ targetClass = \is_string ($ thisOrClass ) && $ thisOrClass !== '' ? $ thisOrClass : $ declaringClass ;
257+ TemplateManager ::getClassInheritedBindings ($ targetClass );
258+ }
252259 }
253260 }
254261
@@ -288,13 +295,10 @@ private static function resolveBaseTypes(
288295 * @param array<string, TypeNode> $contractTypes
289296 * @param array<string, TypeNode> $baseTypes
290297 * @param array<string, mixed> $vars
291- * @param string $effectiveFunction
292- * @param object|null $thisObj
293298 * @param array<string, TemplateTagValueNode> $allTemplates
294299 * @param array<string, TypeNode> $aliases
295300 * @param array<string, TypeNode> $boundTemplates
296301 * @param array<string, TemplateTagValueNode> $declaredTemplates
297- * @param TypeValidatorRegistry $registry
298302 * @param array<string, TemplateTagValueNode> $classTemplates
299303 */
300304 private static function validateAllParameters (
@@ -308,15 +312,16 @@ private static function validateAllParameters(
308312 array $ boundTemplates ,
309313 array $ declaredTemplates ,
310314 TypeValidatorRegistry $ registry ,
311- array $ classTemplates
315+ array $ classTemplates ,
316+ object |string |null $ thisOrClass = null
312317 ): ?ErrorMessage {
313318 foreach ($ contractTypes as $ paramName => $ _ ) {
314319 if (! isset ($ vars [$ paramName ]) && ! \array_key_exists ($ paramName , $ vars )) {
315320 continue ;
316321 }
317322
318323 $ currentBoundTemplates = (\count ($ allTemplates ) > 0 )
319- ? TemplateManager ::getBoundTemplates ($ effectiveFunction , $ thisObj , $ allTemplates )
324+ ? TemplateManager ::getBoundTemplates ($ effectiveFunction , $ thisOrClass , $ allTemplates )
320325 : $ boundTemplates ;
321326
322327 $ err = self ::validateSingleParam (
@@ -331,7 +336,8 @@ private static function validateAllParameters(
331336 $ declaredTemplates ,
332337 $ registry ,
333338 $ classTemplates ,
334- $ vars
339+ $ vars ,
340+ $ thisOrClass
335341 );
336342
337343 if ($ err !== null ) {
@@ -440,7 +446,7 @@ private static function handleMagicCall(
440446 }
441447
442448 /**
443- * Pre-infers generic template parameters from closure typehints and array arguments.
449+ * Pre-infers generic template parameters from closure typehints, array arguments, and generic object arguments.
444450 *
445451 * @param array<string, TypeNode> $types
446452 * @param array<string, mixed> $vars
@@ -468,8 +474,9 @@ private static function preInferGenericTemplates(
468474 self ::inferTemplatesFromClosures ($ types , $ vars , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates );
469475 }
470476
471- if (\count ($ types ) > 1 ) {
477+ if (\count ($ types ) > 0 ) {
472478 self ::inferTemplatesFromArrays ($ types , $ vars , $ effectiveFunction , $ thisObj , $ templates );
479+ self ::inferTemplatesFromGenericObjects ($ types , $ vars , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates );
473480 }
474481 }
475482
@@ -743,6 +750,107 @@ private static function bindTemplateIfUnbound(
743750 }
744751 }
745752
753+ /**
754+ * Pre-infers generic template parameters from generic object arguments (e.g. PBox<T> or array<K, PBox<T>>).
755+ *
756+ * @param array<string, TypeNode> $types
757+ * @param array<string, mixed> $vars
758+ * @param array<string, TemplateTagValueNode> $templates
759+ * @param array<string, TemplateTagValueNode> $classTemplates
760+ */
761+ private static function inferTemplatesFromGenericObjects (
762+ array $ types ,
763+ array $ vars ,
764+ string $ effectiveFunction ,
765+ ?object $ thisObj ,
766+ array $ templates ,
767+ array $ classTemplates = []
768+ ): void {
769+ foreach ($ types as $ paramName => $ typeNode ) {
770+ if (! isset ($ vars [$ paramName ])) {
771+ continue ;
772+ }
773+
774+ $ value = $ vars [$ paramName ];
775+ self ::inferGenericObjectNode ($ typeNode , $ value , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates );
776+ }
777+ }
778+
779+ /**
780+ * @param array<string, TemplateTagValueNode> $templates
781+ * @param array<string, TemplateTagValueNode> $classTemplates
782+ */
783+ private static function inferGenericObjectNode (
784+ TypeNode $ typeNode ,
785+ mixed $ value ,
786+ string $ effectiveFunction ,
787+ ?object $ thisObj ,
788+ array $ templates ,
789+ array $ classTemplates = []
790+ ): void {
791+ if ($ typeNode instanceof NullableTypeNode) {
792+ $ typeNode = $ typeNode ->type ;
793+ }
794+
795+ if ($ typeNode instanceof GenericTypeNode && \is_object ($ value )) {
796+ $ baseName = strtolower ($ typeNode ->type ->name );
797+ if (\in_array ($ baseName , ['array ' , 'list ' , 'iterable ' , 'traversable ' , 'non-empty-array ' , 'non-empty-list ' ], true )) {
798+ return ;
799+ }
800+
801+ $ boundOnInstance = TemplateManager ::getBoundTemplatesForInstance ($ value );
802+ if ($ boundOnInstance === []) {
803+ return ;
804+ }
805+
806+ $ instanceBoundTypes = array_values ($ boundOnInstance );
807+
808+ foreach ($ typeNode ->genericTypes as $ idx => $ gtNode ) {
809+ if ($ gtNode instanceof IdentifierTypeNode && isset ($ templates [$ gtNode ->name ])) {
810+ $ tName = $ gtNode ->name ;
811+ $ isClassLevel = ! TemplateManager ::isMethodTemplate ($ effectiveFunction , $ tName ) && isset ($ classTemplates [$ tName ]);
812+ $ targetObj = $ isClassLevel ? $ thisObj : null ;
813+
814+ if (! TemplateManager ::isBound ($ effectiveFunction , $ targetObj , $ tName )) {
815+ $ inferredCandidate = $ instanceBoundTypes [$ idx ] ?? null ;
816+
817+ if ($ inferredCandidate !== null ) {
818+ $ templateTag = $ templates [$ tName ];
819+ $ satisfiesBound = true ;
820+
821+ if ($ templateTag ->bound !== null ) {
822+ $ resolvedBound = SpecialTypeResolver ::resolve ($ templateTag ->bound , $ effectiveFunction , $ thisObj );
823+ $ satisfiesBound = TemplateManager ::checkVariance ($ inferredCandidate , $ resolvedBound , GenericTypeNode::VARIANCE_COVARIANT );
824+ }
825+
826+ if ($ satisfiesBound ) {
827+ TemplateManager ::bindTemplate ($ effectiveFunction , $ targetObj , $ tName , $ inferredCandidate );
828+ }
829+ }
830+ }
831+ }
832+ }
833+ } elseif (\is_array ($ value )) {
834+ $ innerType = null ;
835+ if ($ typeNode instanceof ArrayTypeNode) {
836+ $ innerType = $ typeNode ->type ;
837+ } elseif ($ typeNode instanceof GenericTypeNode) {
838+ $ baseName = strtolower ($ typeNode ->type ->name );
839+ if (\in_array ($ baseName , ['array ' , 'list ' , 'iterable ' , 'traversable ' , 'non-empty-array ' , 'non-empty-list ' ], true )) {
840+ $ innerType = $ typeNode ->genericTypes [1 ] ?? $ typeNode ->genericTypes [0 ] ?? null ;
841+ }
842+ }
843+
844+ if ($ innerType !== null ) {
845+ foreach ($ value as $ item ) {
846+ if (\is_object ($ item )) {
847+ self ::inferGenericObjectNode ($ innerType , $ item , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates );
848+ }
849+ }
850+ }
851+ }
852+ }
853+
746854 /**
747855 * Unified single-parameter validation pipeline.
748856 *
@@ -766,7 +874,8 @@ private static function validateSingleParam(
766874 array $ declaredTemplates ,
767875 TypeValidatorRegistry $ registry ,
768876 array $ classTemplates = [],
769- array $ vars = []
877+ array $ vars = [],
878+ object |string |null $ thisOrClass = null
770879 ): ?ErrorMessage {
771880 if (
772881 $ typeNode instanceof ConditionalTypeForParameterNode ||
@@ -800,7 +909,7 @@ private static function validateSingleParam(
800909 }
801910
802911 if (self ::getTemplateName ($ typeNode , $ templates ) !== null ) {
803- return self ::resolveTemplateParam ($ typeNode , $ val , $ paramName , $ effectiveFunction , $ thisObj , $ templates , $ registry , $ classTemplates );
912+ return self ::resolveTemplateParam ($ typeNode , $ val , $ paramName , $ effectiveFunction , $ thisObj , $ templates , $ registry , $ classTemplates, $ thisOrClass );
804913 }
805914
806915 return $ registry ->validate ($ val , $ typeNode , $ effectiveFunction . '(): Argument $ ' . $ paramName );
@@ -1055,7 +1164,8 @@ private static function resolveTemplateParam(
10551164 ?object $ thisObj ,
10561165 array $ templates ,
10571166 TypeValidatorRegistry $ registry ,
1058- array $ classTemplates = []
1167+ array $ classTemplates = [],
1168+ object |string |null $ thisOrClass = null
10591169 ): ?ErrorMessage {
10601170 $ templateName = self ::getTemplateName ($ typeNode , $ templates );
10611171 if ($ templateName === null || ! isset ($ templates [$ templateName ])) {
@@ -1066,24 +1176,24 @@ private static function resolveTemplateParam(
10661176 $ isVariadic = $ typeNode instanceof ArrayTypeNode;
10671177 $ isNullable = ($ typeNode instanceof NullableTypeNode) || ($ typeNode instanceof UnionTypeNode && self ::typeContainsNull ($ typeNode ));
10681178 $ isClassLevelTemplate = ! TemplateManager ::isMethodTemplate ($ function , $ templateName ) && isset ($ classTemplates [$ templateName ]);
1069- $ targetObj = $ isClassLevelTemplate ? $ thisObj : null ;
1179+ $ targetObjOrClass = $ isClassLevelTemplate ? ( $ thisObj ?? $ thisOrClass ) : null ;
10701180 $ allowsNullInBound = ($ templateNode ->bound !== null && self ::typeContainsNull ($ templateNode ->bound ));
10711181
10721182 if ($ isNullable && $ val === null ) {
1073- if ($ allowsNullInBound && ! TemplateManager ::isBound ($ function , $ targetObj , $ templateName )) {
1074- TemplateManager ::bindTemplate ($ function , $ targetObj , $ templateName , new IdentifierTypeNode ('null ' ));
1183+ if ($ allowsNullInBound && ! TemplateManager ::isBound ($ function , $ targetObjOrClass , $ templateName )) {
1184+ TemplateManager ::bindTemplate ($ function , $ thisObj , $ templateName , new IdentifierTypeNode ('null ' ));
10751185 }
10761186
10771187 return null ;
10781188 }
10791189
1080- if (! TemplateManager ::isBound ($ function , $ targetObj , $ templateName )) {
1190+ if (! TemplateManager ::isBound ($ function , $ targetObjOrClass , $ templateName )) {
10811191 return self ::bindInitialTemplate (
10821192 $ val ,
10831193 $ paramName ,
10841194 $ function ,
10851195 $ thisObj ,
1086- $ targetObj ,
1196+ $ thisObj ,
10871197 $ templateName ,
10881198 $ templateNode ,
10891199 $ isVariadic ,
@@ -1097,7 +1207,7 @@ private static function resolveTemplateParam(
10971207 $ paramName ,
10981208 $ function ,
10991209 $ thisObj ,
1100- $ targetObj ,
1210+ $ targetObjOrClass ,
11011211 $ templateName ,
11021212 $ templateNode ,
11031213 $ isVariadic ,
@@ -1154,7 +1264,7 @@ private static function validateBoundTemplate(
11541264 string $ paramName ,
11551265 string $ function ,
11561266 ?object $ thisObj ,
1157- ? object $ targetObj ,
1267+ object | string | null $ targetObj ,
11581268 string $ templateName ,
11591269 TemplateTagValueNode $ templateNode ,
11601270 bool $ isVariadic ,
@@ -1166,9 +1276,11 @@ private static function validateBoundTemplate(
11661276 return null ;
11671277 }
11681278
1279+ $ targetObjOnly = \is_object ($ targetObj ) ? $ targetObj : null ;
1280+
11691281 if ($ expectedTypeNode instanceof IdentifierTypeNode && $ expectedTypeNode ->name === $ templateName ) {
11701282 $ inferredType = TemplateManager ::inferTypeFromValue ($ val );
1171- TemplateManager ::bindTemplate ($ function , $ targetObj , $ templateName , $ inferredType );
1283+ TemplateManager ::bindTemplate ($ function , $ targetObjOnly , $ templateName , $ inferredType );
11721284
11731285 return null ;
11741286 }
@@ -1179,7 +1291,7 @@ private static function validateBoundTemplate(
11791291 $ paramName ,
11801292 $ function ,
11811293 $ thisObj ,
1182- $ targetObj ,
1294+ $ targetObjOnly ,
11831295 $ templateName ,
11841296 $ templateNode ,
11851297 $ expectedTypeNode ,
@@ -1199,7 +1311,7 @@ private static function validateBoundTemplate(
11991311 $ isClassLevelTemplate ,
12001312 $ function ,
12011313 $ thisObj ,
1202- $ targetObj ,
1314+ $ targetObjOnly ,
12031315 $ templateName ,
12041316 $ context ,
12051317 $ err ,
0 commit comments