1515use OpenCodeModeling \CodeAst \Code \DocBlock \DocBlock ;
1616use OpenCodeModeling \CodeAst \Code \MethodGenerator ;
1717use OpenCodeModeling \CodeAst \NodeVisitor \ClassMethod ;
18+ use PhpParser \Comment \Doc ;
1819use PhpParser \Node ;
1920use PhpParser \NodeTraverser ;
2021use PhpParser \NodeVisitor ;
@@ -77,8 +78,22 @@ public static function fromNode(Node\Stmt\ClassMethod $node, bool $typed = true,
7778
7879 $ self = new self ();
7980
81+ $ returnType = null ;
82+
83+ switch (true ) {
84+ case $ node ->returnType instanceof Node \Name:
85+ case $ node ->returnType instanceof Node \Identifier:
86+ $ returnType = $ node ->returnType ->toString ();
87+ break ;
88+ case $ node ->returnType instanceof Node \NullableType:
89+ $ returnType = '? ' . $ node ->returnType ->type ->toString ();
90+ break ;
91+ default :
92+ break ;
93+ }
94+
8095 $ self ->name = $ node ->name ->toString ();
81- $ self ->returnType = $ node -> returnType ? $ node -> returnType -> toString () : null ;
96+ $ self ->returnType = $ returnType ;
8297 $ self ->visibility = $ node ->flags ;
8398 $ self ->abstract = ($ node ->flags & MethodGenerator::FLAG_ABSTRACT ) > 0 ;
8499 $ self ->final = ($ node ->flags & MethodGenerator::FLAG_FINAL ) > 0 ;
@@ -93,6 +108,20 @@ public static function fromNode(Node\Stmt\ClassMethod $node, bool $typed = true,
93108 $ self ->body = $ printer ->prettyPrint ($ node ->stmts );
94109 }
95110
111+ $ comments = $ node ->getAttribute ('comments ' );
112+
113+ if ($ comments !== null
114+ && $ comments [0 ] instanceof Doc
115+ ) {
116+ $ comments = \explode ("\n" , $ comments [0 ]->getReformattedText ());
117+
118+ foreach ($ comments as $ comment ) {
119+ if (0 === \strpos ($ comment , ' * @return ' )) {
120+ $ self ->setReturnTypeDocBlockHint (\substr ($ comment , 11 ));
121+ }
122+ }
123+ }
124+
96125 return $ self ;
97126 }
98127
@@ -122,13 +151,23 @@ public function setBody(string $body): self
122151 return $ this ;
123152 }
124153
154+ public function body (): string
155+ {
156+ return $ this ->body ;
157+ }
158+
125159 public function setReturnType (?string $ returnType ): self
126160 {
127161 $ this ->returnType = $ returnType ;
128162
129163 return $ this ;
130164 }
131165
166+ public function getReturnType (): ?string
167+ {
168+ return $ this ->returnType ;
169+ }
170+
132171 public function getName (): string
133172 {
134173 return $ this ->name ;
@@ -175,6 +214,21 @@ public function setPublic(): self
175214 return $ this ;
176215 }
177216
217+ public function isPrivate (): bool
218+ {
219+ return (bool ) ($ this ->visibility & ClassConstGenerator::FLAG_PRIVATE );
220+ }
221+
222+ public function isProtected (): bool
223+ {
224+ return (bool ) ($ this ->visibility & ClassConstGenerator::FLAG_PROTECTED );
225+ }
226+
227+ public function isPublic (): bool
228+ {
229+ return (bool ) ($ this ->visibility & ClassConstGenerator::FLAG_PUBLIC );
230+ }
231+
178232 public function getDocBlockComment (): ?string
179233 {
180234 return $ this ->docBlockComment ;
0 commit comments