@@ -224,6 +224,47 @@ private function getVisibility(array $tokens, $idx)
224
224
}
225
225
}
226
226
227
+ /**
228
+ * Get the docblock for this token
229
+ *
230
+ * This method will fetch the docblock belonging to the current token. The
231
+ * docblock must be placed on the line directly above the token to be
232
+ * recognized.
233
+ *
234
+ * @return string|null Returns the docblock as a string if found
235
+ */
236
+ private function getDocblock (array $ tokens , $ idx ) {
237
+ $ currentLineNumber = $ this ->tline ($ tokens [$ idx ]);
238
+ $ prevLineNumber = $ currentLineNumber - 1 ;
239
+
240
+ for ($ i = $ idx - 1 ; $ i ; $ i --) {
241
+ if (!isset ($ tokens [$ i ])) {
242
+ return ;
243
+ }
244
+
245
+ $ token = $ tokens [$ i ];
246
+ $ tconst = $ this ->tconst ($ token );
247
+
248
+ if ($ tconst === T_FUNCTION || $ tconst === T_CLASS || $ tconst === T_TRAIT ) {
249
+ // Some other trait, class or function, no docblock can be
250
+ // used for the current token
251
+ break ;
252
+ }
253
+
254
+ $ line = $ this ->tline ($ token );
255
+
256
+ if ($ line == $ currentLineNumber || ($ line == $ prevLineNumber && $ tconst === T_WHITESPACE )) {
257
+ continue ;
258
+ }
259
+
260
+ if ($ line < $ currentLineNumber && $ tconst === T_DOC_COMMENT ) {
261
+ break ;
262
+ }
263
+
264
+ return (string )$ token ;
265
+ }
266
+ }
267
+
227
268
public function tokenize () {
228
269
$ sourceCode = file_get_contents ($ this ->filename );
229
270
$ tokens = token_get_all ($ sourceCode );
@@ -272,7 +313,7 @@ public function tokenize() {
272
313
'parent ' => $ this ->getParent ($ tokens , $ i ),
273
314
'interfaces ' => $ this ->getInterfaces ($ tokens , $ i ),
274
315
'keywords ' => $ this ->getKeywords ($ tokens , $ i ),
275
- 'docblock ' => $ token ->getDocblock (),
316
+ 'docblock ' => $ this ->getDocblock ($ tokens , $ i ),
276
317
'startLine ' => $ this ->tline ($ token ),
277
318
'endLine ' => $ token ->getEndLine (),
278
319
'package ' => $ token ->getPackage (),
@@ -293,7 +334,7 @@ public function tokenize() {
293
334
case 'PHP_Token_FUNCTION ' :
294
335
$ tname = $ this ->tname ($ tokens , $ idx );
295
336
$ tmp = array (
296
- 'docblock ' => $ token ->getDocblock (),
337
+ 'docblock ' => $ this ->getDocblock ($ tokens , $ i ),
297
338
'keywords ' => $ this ->getKeywords ($ tokens , $ i ),
298
339
'visibility ' => $ this ->getVisibility ($ tokens , $ i ),
299
340
'signature ' => $ token ->getSignature (),
0 commit comments