@@ -21,18 +21,40 @@ class PhpDoc
21
21
/**
22
22
* Parses the comment block into tags.
23
23
* @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.
25
28
* @return array The parsed tags
26
29
*/
27
- public static function getTags (string $ comment , array $ ignored = [' param ' , ' return ' ]): array
30
+ public static function getTags (string $ comment , array $ options = []): array
28
31
{
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" , '' ,
31
53
\trim (\preg_replace ('/^\s*\**( |\t)?/m ' , '' , $ comment ))
32
54
);
33
55
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 );
36
58
37
59
foreach ($ parts as $ part ) {
38
60
if (\preg_match ('/^(\w+)(.*)/ms ' , \trim ($ part ), $ matches )) {
@@ -41,6 +63,10 @@ public static function getTags(string $comment, array $ignored = ['param', 'retu
41
63
continue ;
42
64
}
43
65
66
+ if ($ allow && !\in_array ($ name , $ allow , true )) {
67
+ continue ;
68
+ }
69
+
44
70
if (!isset ($ tags [$ name ])) {
45
71
$ tags [$ name ] = \trim ($ matches [2 ]);
46
72
} elseif (\is_array ($ tags [$ name ])) {
@@ -56,7 +82,6 @@ public static function getTags(string $comment, array $ignored = ['param', 'retu
56
82
57
83
/**
58
84
* Returns the first line of docBlock.
59
- *
60
85
* @param string $comment
61
86
* @return string
62
87
*/
@@ -74,15 +99,14 @@ public static function firstLine(string $comment): string
74
99
/**
75
100
* Returns full description from the doc-block.
76
101
* If have multi line text, will return multi line.
77
- *
78
102
* @param string $comment
79
103
* @return string
80
104
*/
81
105
public static function description (string $ comment ): string
82
106
{
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 , '/ ' ))));
84
108
85
- if (\preg_match ('/^\s*@\w+/m ' , $ comment , $ matches , PREG_OFFSET_CAPTURE )) {
109
+ if (\preg_match ('/^\s*@\w+/m ' , $ comment , $ matches , \ PREG_OFFSET_CAPTURE )) {
86
110
$ comment = \trim (\substr ($ comment , 0 , $ matches [0 ][1 ]));
87
111
}
88
112
0 commit comments