@@ -21,18 +21,40 @@ class PhpDoc
2121 /**
2222 * Parses the comment block into tags.
2323 * @param string $comment The comment block text
24- * @param array $ignored
24+ * @param array $options
25+ * - 'allow' // only allowed tags
26+ * - 'ignore' // ignored tags
27+ * - 'default' => 'description', // default tag name, first line text will attach to it.
2528 * @return array The parsed tags
2629 */
27- public static function getTags (string $ comment , array $ ignored = [' param ' , ' return ' ]): array
30+ public static function getTags (string $ comment , array $ options = []): array
2831 {
29- $ comment = (string )\str_replace ("\r\n" , "\n" , \trim ($ comment , "/ \n" ));
30- $ comment = "@description \n" . \str_replace ("\r" , '' ,
32+ if (!$ comment = \trim ($ comment , "/ \n" )) {
33+ return [];
34+ }
35+
36+ $ options = \array_merge ([
37+ 'allow ' => [], // only allowed tags
38+ 'ignore ' => ['param ' , 'return ' ], // ignore tags
39+ 'default ' => 'description ' , // default tag name, first line text will attach to it.
40+ ], $ options );
41+
42+ $ allow = (array )$ options ['allow ' ];
43+ $ ignored = (array )$ options ['ignore ' ];
44+
45+ // always allow default tag
46+ if ($ default = (string )$ options ['default ' ]) {
47+ $ allow [] = $ default ;
48+ }
49+
50+ $ comment = \str_replace ("\r\n" , "\n" , $ comment );
51+ $ comment = "@ {$ default } \n" .
52+ \str_replace ("\r" , '' ,
3153 \trim (\preg_replace ('/^\s*\**( |\t)?/m ' , '' , $ comment ))
3254 );
3355
34- $ tags = [];
35- $ parts = \preg_split ('/^\s*@/m ' , $ comment , -1 , PREG_SPLIT_NO_EMPTY );
56+ $ tags = [];
57+ $ parts = \preg_split ('/^\s*@/m ' , $ comment , -1 , \ PREG_SPLIT_NO_EMPTY );
3658
3759 foreach ($ parts as $ part ) {
3860 if (\preg_match ('/^(\w+)(.*)/ms ' , \trim ($ part ), $ matches )) {
@@ -41,6 +63,10 @@ public static function getTags(string $comment, array $ignored = ['param', 'retu
4163 continue ;
4264 }
4365
66+ if ($ allow && !\in_array ($ name , $ allow , true )) {
67+ continue ;
68+ }
69+
4470 if (!isset ($ tags [$ name ])) {
4571 $ tags [$ name ] = \trim ($ matches [2 ]);
4672 } elseif (\is_array ($ tags [$ name ])) {
@@ -56,7 +82,6 @@ public static function getTags(string $comment, array $ignored = ['param', 'retu
5682
5783 /**
5884 * Returns the first line of docBlock.
59- *
6085 * @param string $comment
6186 * @return string
6287 */
@@ -74,15 +99,14 @@ public static function firstLine(string $comment): string
7499 /**
75100 * Returns full description from the doc-block.
76101 * If have multi line text, will return multi line.
77- *
78102 * @param string $comment
79103 * @return string
80104 */
81105 public static function description (string $ comment ): string
82106 {
83- $ comment = ( string ) \str_replace ("\r" , '' , \trim (\preg_replace ('/^\s*\**( |\t)?/m ' , '' , trim ($ comment , '/ ' ))));
107+ $ comment = \str_replace ("\r" , '' , \trim (\preg_replace ('/^\s*\**( |\t)?/m ' , '' , trim ($ comment , '/ ' ))));
84108
85- if (\preg_match ('/^\s*@\w+/m ' , $ comment , $ matches , PREG_OFFSET_CAPTURE )) {
109+ if (\preg_match ('/^\s*@\w+/m ' , $ comment , $ matches , \ PREG_OFFSET_CAPTURE )) {
86110 $ comment = \trim (\substr ($ comment , 0 , $ matches [0 ][1 ]));
87111 }
88112
0 commit comments