15
15
use OpenCodeModeling \CodeAst \Code \DocBlock \DocBlock ;
16
16
use OpenCodeModeling \CodeAst \Code \MethodGenerator ;
17
17
use OpenCodeModeling \CodeAst \NodeVisitor \ClassMethod ;
18
+ use PhpParser \Comment \Doc ;
18
19
use PhpParser \Node ;
19
20
use PhpParser \NodeTraverser ;
20
21
use PhpParser \NodeVisitor ;
@@ -77,8 +78,22 @@ public static function fromNode(Node\Stmt\ClassMethod $node, bool $typed = true,
77
78
78
79
$ self = new self ();
79
80
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
+
80
95
$ self ->name = $ node ->name ->toString ();
81
- $ self ->returnType = $ node -> returnType ? $ node -> returnType -> toString () : null ;
96
+ $ self ->returnType = $ returnType ;
82
97
$ self ->visibility = $ node ->flags ;
83
98
$ self ->abstract = ($ node ->flags & MethodGenerator::FLAG_ABSTRACT ) > 0 ;
84
99
$ self ->final = ($ node ->flags & MethodGenerator::FLAG_FINAL ) > 0 ;
@@ -93,6 +108,20 @@ public static function fromNode(Node\Stmt\ClassMethod $node, bool $typed = true,
93
108
$ self ->body = $ printer ->prettyPrint ($ node ->stmts );
94
109
}
95
110
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
+
96
125
return $ self ;
97
126
}
98
127
@@ -122,13 +151,23 @@ public function setBody(string $body): self
122
151
return $ this ;
123
152
}
124
153
154
+ public function body (): string
155
+ {
156
+ return $ this ->body ;
157
+ }
158
+
125
159
public function setReturnType (?string $ returnType ): self
126
160
{
127
161
$ this ->returnType = $ returnType ;
128
162
129
163
return $ this ;
130
164
}
131
165
166
+ public function getReturnType (): ?string
167
+ {
168
+ return $ this ->returnType ;
169
+ }
170
+
132
171
public function getName (): string
133
172
{
134
173
return $ this ->name ;
@@ -175,6 +214,21 @@ public function setPublic(): self
175
214
return $ this ;
176
215
}
177
216
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
+
178
232
public function getDocBlockComment (): ?string
179
233
{
180
234
return $ this ->docBlockComment ;
0 commit comments