@@ -526,6 +526,20 @@ private static function getContainingClassFqn(Node $node)
526526 return (string )$ classNode ->getNamespacedName ();
527527 }
528528
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+
529543 /**
530544 * Returns the assignment or parameter node where a variable was defined
531545 *
@@ -1110,18 +1124,24 @@ public function getTypeFromNode($node)
11101124 && $ returnTags [0 ]->getType () !== null
11111125 ) {
11121126 // 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 ;
11141135 }
11151136 if ($ node ->returnType !== null && !($ node ->returnType instanceof PhpParser \MissingToken)) {
11161137 // Use PHP7 return type hint
11171138 if ($ node ->returnType instanceof PhpParser \Token) {
11181139 // Resolve a string like "bool" to a type object
11191140 return $ this ->typeResolver ->resolve ($ node ->returnType ->getText ($ node ->getFileContents ()));
11201141 } 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 ;
11251145 }
11261146 }
11271147 return new Types \Object_ (new Fqsen ('\\' . (string )$ node ->returnType ->getResolvedName ()));
0 commit comments