@@ -310,6 +310,68 @@ private function getEndLine(array $tokens, $idx)
310
310
return $ this ->tline ($ tokens [$ this ->getEndTokenId ($ tokens , $ idx )]);
311
311
}
312
312
313
+ /**
314
+ * @return array
315
+ */
316
+ private function getPackage (array $ tokens , $ idx )
317
+ {
318
+ $ token = $ tokens [$ idx ];
319
+ $ className = $ this ->tname ($ token );
320
+ $ docComment = $ this ->getDocblock ($ tokens , $ idx );
321
+
322
+ $ result = array (
323
+ 'namespace ' => '' ,
324
+ 'fullPackage ' => '' ,
325
+ 'category ' => '' ,
326
+ 'package ' => '' ,
327
+ 'subpackage ' => ''
328
+ );
329
+
330
+ for ($ i = $ idx ; $ i ; --$ i ) {
331
+ $ tconst = $ this ->tconst ($ tokens [$ i ]);
332
+ if ($ tconst === T_NAMESPACE ) {
333
+ $ result ['namespace ' ] = $ this ->tname ($ tokens [$ i ]);
334
+ break ;
335
+ }
336
+ }
337
+
338
+ if (preg_match ('/@category[\s]+([\.\w]+)/ ' , $ docComment , $ matches )) {
339
+ $ result ['category ' ] = $ matches [1 ];
340
+ }
341
+
342
+ if (preg_match ('/@package[\s]+([\.\w]+)/ ' , $ docComment , $ matches )) {
343
+ $ result ['package ' ] = $ matches [1 ];
344
+ $ result ['fullPackage ' ] = $ matches [1 ];
345
+ }
346
+
347
+ if (preg_match ('/@subpackage[\s]+([\.\w]+)/ ' , $ docComment , $ matches )) {
348
+ $ result ['subpackage ' ] = $ matches [1 ];
349
+ $ result ['fullPackage ' ] .= '. ' . $ matches [1 ];
350
+ }
351
+
352
+ if (empty ($ result ['fullPackage ' ])) {
353
+ $ result ['fullPackage ' ] = $ this ->arrayToName (
354
+ explode ('_ ' , str_replace ('\\' , '_ ' , $ className )),
355
+ '. '
356
+ );
357
+ }
358
+
359
+ return $ result ;
360
+ }
361
+
362
+ private function arrayToName (array $ parts , $ join = '\\' )
363
+ {
364
+ $ result = '' ;
365
+
366
+ if (count ($ parts ) > 1 ) {
367
+ array_pop ($ parts );
368
+
369
+ $ result = join ($ join , $ parts );
370
+ }
371
+
372
+ return $ result ;
373
+ }
374
+
313
375
public function tokenize () {
314
376
$ sourceCode = file_get_contents ($ this ->filename );
315
377
$ tokens = token_get_all ($ sourceCode );
@@ -363,7 +425,7 @@ public function tokenize() {
363
425
'docblock ' => $ this ->getDocblock ($ tokens , $ i ),
364
426
'startLine ' => $ this ->tline ($ token ),
365
427
'endLine ' => $ endLine ,
366
- 'package ' => $ token ->getPackage (),
428
+ 'package ' => $ this ->getPackage ($ tokens , $ i ),
367
429
'file ' => $ this ->filename
368
430
);
369
431
0 commit comments