@@ -526,6 +526,20 @@ private static function getContainingClassFqn(Node $node)
526
526
return (string )$ classNode ->getNamespacedName ();
527
527
}
528
528
529
+ /**
530
+ * Returns the type of the class a node is contained in
531
+ * Returns null if the class is anonymous or the node is not contained in a class
532
+ *
533
+ * @param Node $node The node used to find the containing class
534
+ *
535
+ * @return Types\Object_|null
536
+ */
537
+ private function getContainingClassType (Node $ node )
538
+ {
539
+ $ classFqn = $ this ->getContainingClassFqn ($ node );
540
+ return $ classFqn ? new Types \Object_ (new Fqsen ('\\' . $ classFqn )) : null ;
541
+ }
542
+
529
543
/**
530
544
* Returns the assignment or parameter node where a variable was defined
531
545
*
@@ -1110,18 +1124,24 @@ public function getTypeFromNode($node)
1110
1124
&& $ returnTags [0 ]->getType () !== null
1111
1125
) {
1112
1126
// Use @return tag
1113
- return $ returnTags [0 ]->getType ();
1127
+ $ returnType = $ returnTags [0 ]->getType ();
1128
+ if ($ returnType instanceof Types \Self_) {
1129
+ $ selfType = $ this ->getContainingClassType ($ node );
1130
+ if ($ selfType ) {
1131
+ return $ selfType ;
1132
+ }
1133
+ }
1134
+ return $ returnType ;
1114
1135
}
1115
1136
if ($ node ->returnType !== null && !($ node ->returnType instanceof PhpParser \MissingToken)) {
1116
1137
// Use PHP7 return type hint
1117
1138
if ($ node ->returnType instanceof PhpParser \Token) {
1118
1139
// Resolve a string like "bool" to a type object
1119
1140
return $ this ->typeResolver ->resolve ($ node ->returnType ->getText ($ node ->getFileContents ()));
1120
1141
} elseif ($ node ->returnType ->getResolvedName () === 'self ' ) {
1121
- $ classNode = $ node ->getFirstAncestor (Node \Statement \ClassDeclaration::class);
1122
- if ($ classNode ) {
1123
- $ classFqn = (string )$ classNode ->getNamespacedName ();
1124
- return new Types \Object_ (new Fqsen ('\\' . $ classFqn ));
1142
+ $ selfType = $ this ->getContainingClassType ($ node );
1143
+ if ($ selfType !== null ) {
1144
+ return $ selfType ;
1125
1145
}
1126
1146
}
1127
1147
return new Types \Object_ (new Fqsen ('\\' . (string )$ node ->returnType ->getResolvedName ()));
0 commit comments