forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocParser.php
More file actions
197 lines (165 loc) · 4.01 KB
/
DocParser.php
File metadata and controls
197 lines (165 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace WP_CLI;
use Mustangostang\Spyc;
/**
* Parse command attributes from its PHPdoc.
* Used to determine execution characteristics (arguments, etc.).
*/
class DocParser {
/**
* PHPdoc command for the command.
*
* @var string
*/
protected $doc_comment;
/**
* @param string $doc_comment
*/
public function __construct( $doc_comment ) {
/* Make sure we have a known line ending in document */
$doc_comment = str_replace( "\r\n", "\n", $doc_comment );
$this->doc_comment = self::remove_decorations( $doc_comment );
}
/**
* Remove unused cruft from PHPdoc comment.
*
* @param string $comment PHPdoc comment.
* @return string
*/
private static function remove_decorations( $comment ) {
$comment = preg_replace( '|^/\*\*[\r\n]+|', '', $comment );
$comment = preg_replace( '|\n[\t ]*\*/$|', '', $comment );
$comment = preg_replace( '|^[\t ]*\* ?|m', '', $comment );
return $comment;
}
/**
* Get the command's short description (e.g. summary).
*
* @return string
*/
public function get_shortdesc() {
if ( ! preg_match( '|^([^@][^\n]+)\n*|', $this->doc_comment, $matches ) ) {
return '';
}
return $matches[1];
}
/**
* Get the command's full description
*
* @return string
*/
public function get_longdesc() {
$shortdesc = $this->get_shortdesc();
if ( ! $shortdesc ) {
return '';
}
$longdesc = substr( $this->doc_comment, strlen( $shortdesc ) );
$lines = [];
foreach ( explode( "\n", $longdesc ) as $line ) {
if ( 0 === strpos( $line, '@' ) ) {
break;
}
$lines[] = $line;
}
return trim( implode( "\n", $lines ) );
}
/**
* Get the value for a given tag (e.g. "@alias" or "@subcommand")
*
* @param string $name Name for the tag, without '@'
* @return string
*/
public function get_tag( $name ) {
if ( preg_match( '|^@' . $name . '\s+([a-z-_0-9]+)|m', $this->doc_comment, $matches ) ) {
return $matches[1];
}
return '';
}
/**
* Get the command's synopsis.
*
* @return string
*/
public function get_synopsis() {
if ( ! preg_match( '|^@synopsis\s+(.+)|m', $this->doc_comment, $matches ) ) {
return '';
}
return $matches[1];
}
/**
* Get the description for a given argument.
*
* @param string $name Argument's doc name.
* @return string
*/
public function get_arg_desc( $name ) {
if ( preg_match( "/\[?<{$name}>.+\n: (.+?)(\n|$)/", $this->doc_comment, $matches ) ) {
return $matches[1];
}
return '';
}
/**
* Get the arguments for a given argument.
*
* @param string $name Argument's doc name.
* @return mixed|null
*/
public function get_arg_args( $name ) {
return $this->get_arg_or_param_args( "/^\[?<{$name}>.*/" );
}
/**
* Get the description for a given parameter.
*
* @param string $key Parameter's key.
* @return string
*/
public function get_param_desc( $key ) {
if ( preg_match( "/\[?--{$key}=.+\n: (.+?)(\n|$)/", $this->doc_comment, $matches ) ) {
return $matches[1];
}
return '';
}
/**
* Get the arguments for a given parameter.
*
* @param string $key Parameter's key.
* @return mixed|null
*/
public function get_param_args( $key ) {
return $this->get_arg_or_param_args( "/^\[?--{$key}=.*/" );
}
/**
* Get the args for an arg or param
*
* @param string $regex Pattern to match against
* @return array|null Interpreted YAML document, or null.
*/
private function get_arg_or_param_args( $regex ) {
$bits = explode( "\n", $this->doc_comment );
$within_arg = false;
$within_doc = false;
$document = [];
foreach ( $bits as $bit ) {
if ( preg_match( $regex, $bit ) ) {
$within_arg = true;
}
if ( $within_arg && $within_doc && '---' === $bit ) {
$within_doc = false;
}
if ( $within_arg && ! $within_doc && '---' === $bit ) {
$within_doc = true;
}
if ( $within_doc ) {
$document[] = $bit;
}
if ( $within_arg && '' === $bit ) {
$within_arg = false;
break;
}
}
if ( $document ) {
return Spyc::YAMLLoadString( implode( "\n", $document ) );
}
return null;
}
}