@@ -44,8 +44,9 @@ public function narrowToFullyQualifiedOrAliasedObjectType(
4444 Scope |null $ scope ,
4545 bool $ withPreslash = false
4646 ): TypeWithClassName |NonExistingObjectType |UnionType |MixedType |TemplateType {
47- $ className = ltrim ($ objectType ->getClassName (), '\\' );
48- if (str_starts_with ($ objectType ->getClassName (), '\\' )) {
47+ $ rawClassName = $ objectType ->getClassName ();
48+ $ className = ltrim ($ rawClassName , '\\' );
49+ if (str_starts_with ($ rawClassName , '\\' )) {
4950 return new FullyQualifiedObjectType ($ className );
5051 }
5152
@@ -87,27 +88,18 @@ public function narrowToFullyQualifiedOrAliasedObjectType(
8788
8889 $ classReflection = $ scope ->getClassReflection ();
8990 if ($ classReflection instanceof ClassReflection) {
90- $ templateTags = $ classReflection ->getTemplateTags ();
9191 $ nameScope = $ this ->nameScopeFactory ->createNameScopeFromNodeWithoutTemplateTypes ($ node );
9292 $ templateTypeScope = $ nameScope ->getTemplateTypeScope ();
93-
94- if (! $ templateTypeScope instanceof TemplateTypeScope) {
95- // invalid type
96- return $ this ->resolveNamespacedNonExistingObjectType ($ namespaceName , $ className , $ withPreslash );
97- }
98-
99- $ currentTemplateTag = $ templateTags [$ className ] ?? null ;
100- if ($ currentTemplateTag === null ) {
101- // invalid type
102- return $ this ->resolveNamespacedNonExistingObjectType ($ namespaceName , $ className , $ withPreslash );
93+ $ currentTemplateTag = $ classReflection ->getTemplateTags ()[$ className ] ?? null ;
94+
95+ if ($ templateTypeScope instanceof TemplateTypeScope && $ currentTemplateTag !== null ) {
96+ return TemplateTypeFactory::create (
97+ $ templateTypeScope ,
98+ $ currentTemplateTag ->getName (),
99+ $ currentTemplateTag ->getBound (),
100+ $ currentTemplateTag ->getVariance ()
101+ );
103102 }
104-
105- return TemplateTypeFactory::create (
106- $ templateTypeScope ,
107- $ currentTemplateTag ->getName (),
108- $ currentTemplateTag ->getBound (),
109- $ currentTemplateTag ->getVariance ()
110- );
111103 }
112104 }
113105
@@ -120,15 +112,7 @@ private function resolveNamespacedNonExistingObjectType(
120112 string $ className ,
121113 bool $ withPreslash
122114 ): NonExistingObjectType {
123- if ($ namespacedName === null ) {
124- return new NonExistingObjectType ($ className );
125- }
126-
127- if ($ withPreslash ) {
128- return new NonExistingObjectType ($ className );
129- }
130-
131- if (str_contains ($ className , '\\' )) {
115+ if ($ namespacedName === null || $ withPreslash || str_contains ($ className , '\\' )) {
132116 return new NonExistingObjectType ($ className );
133117 }
134118
@@ -153,14 +137,12 @@ private function matchAliasedObjectType(ObjectType $objectType, array $uses): ?A
153137 continue ;
154138 }
155139
156- $ useName = $ prefix . $ useUse ->name ->toString ();
157140 $ alias = $ useUse ->alias ->toString ();
158141 $ fullyQualifiedName = $ prefix . $ useUse ->name ->toString ();
159142
160143 $ processAliasedObject = $ this ->processAliasedObject (
161144 $ alias ,
162145 $ className ,
163- $ useName ,
164146 $ fullyQualifiedName
165147 );
166148
@@ -176,7 +158,6 @@ private function matchAliasedObjectType(ObjectType $objectType, array $uses): ?A
176158 private function processAliasedObject (
177159 string $ alias ,
178160 string $ className ,
179- string $ useName ,
180161 string $ fullyQualifiedName
181162 ): ?AliasedObjectType {
182163 // A. is alias in use statement matching this class alias
@@ -185,7 +166,7 @@ private function processAliasedObject(
185166 }
186167
187168 // B. is aliased classes matching the class name
188- if ($ useName === $ className ) {
169+ if ($ fullyQualifiedName === $ className ) {
189170 return new AliasedObjectType ($ className , $ fullyQualifiedName );
190171 }
191172
0 commit comments