1818use TypePHP \Internal \Diagnostic \ErrorFactory ;
1919use TypePHP \Internal \Diagnostic \ErrorMessage ;
2020use TypePHP \Internal \Diagnostic \TypeFormatter ;
21+ use TypePHP \Internal \Docblock \DocblockExtractor ;
2122use TypePHP \Internal \Docblock \DocblockParser ;
2223use TypePHP \Internal \Generics \TemplateManager ;
2324use TypePHP \Internal \Generics \TemplateSubstitutor ;
2425use TypePHP \Internal \Resolver \HierarchyResolver ;
2526use TypePHP \Internal \Resolver \SpecialTypeResolver ;
2627use TypePHP \Internal \Util \ClassNameValidator ;
2728use TypePHP \Internal \Util \Config ;
29+ use TypePHP \Internal \Util \StubManager ;
2830use TypePHP \Internal \Validator \TypeValidatorRegistry ;
2931
3032/**
@@ -51,13 +53,21 @@ final class ParamChecker
5153 */
5254 private static array $ baseTypeCache = [];
5355
56+ /**
57+ * Cache for declared class template names by class FQCN.
58+ *
59+ * @var array<string, list<string>>
60+ */
61+ private static array $ classDeclaredTemplateNamesCache = [];
62+
5463 /**
5564 * Resets internal caches. Useful for test isolation.
5665 */
5766 public static function reset (): void
5867 {
5968 self ::$ effectiveFunctionCache = [];
6069 self ::$ noParamContractCache = [];
70+ self ::$ classDeclaredTemplateNamesCache = [];
6171 ClassNameValidator ::reset ();
6272 self ::$ baseTypeCache = [];
6373 }
@@ -803,6 +813,25 @@ private static function inferGenericObjectNode(
803813 return ;
804814 }
805815
816+ $ targetClassFqcn = SpecialTypeResolver ::resolveFqcn ($ typeNode ->type ->name , new \ReflectionClass ($ value ));
817+ $ targetTemplateNames = self ::$ classDeclaredTemplateNamesCache [$ targetClassFqcn ] ?? null ;
818+
819+ if ($ targetTemplateNames === null ) {
820+ $ targetTemplateNames = [];
821+ if (class_exists ($ targetClassFqcn , false ) || class_exists ($ targetClassFqcn ) || interface_exists ($ targetClassFqcn ) || trait_exists ($ targetClassFqcn )) {
822+ try {
823+ $ targetRef = new \ReflectionClass ($ targetClassFqcn );
824+ $ targetDoc = StubManager ::getClassDoc ($ targetClassFqcn ) ?? $ targetRef ->getDocComment ();
825+ if ($ targetDoc !== false && $ targetDoc !== null ) {
826+ $ targetPhpDocNode = DocblockExtractor ::parseDocString ($ targetDoc );
827+ $ targetTemplateNames = array_keys (DocblockExtractor ::extractTemplates ($ targetPhpDocNode ));
828+ }
829+ } catch (\Throwable $ e ) {
830+ }
831+ }
832+ self ::$ classDeclaredTemplateNamesCache [$ targetClassFqcn ] = $ targetTemplateNames ;
833+ }
834+
806835 $ instanceBoundTypes = array_values ($ boundOnInstance );
807836
808837 foreach ($ typeNode ->genericTypes as $ idx => $ gtNode ) {
@@ -812,7 +841,14 @@ private static function inferGenericObjectNode(
812841 $ targetObj = $ isClassLevel ? $ thisObj : null ;
813842
814843 if (! TemplateManager ::isBound ($ effectiveFunction , $ targetObj , $ tName )) {
815- $ inferredCandidate = $ instanceBoundTypes [$ idx ] ?? null ;
844+ $ targetTemplateName = $ targetTemplateNames [$ idx ] ?? null ;
845+ $ inferredCandidate = null ;
846+
847+ if ($ targetTemplateName !== null && isset ($ boundOnInstance [$ targetTemplateName ])) {
848+ $ inferredCandidate = $ boundOnInstance [$ targetTemplateName ];
849+ } elseif (isset ($ instanceBoundTypes [$ idx ])) {
850+ $ inferredCandidate = $ instanceBoundTypes [$ idx ];
851+ }
816852
817853 if ($ inferredCandidate !== null ) {
818854 $ templateTag = $ templates [$ tName ];
0 commit comments