forked from consolidation/robo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskInfo.php
235 lines (195 loc) · 7.29 KB
/
TaskInfo.php
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace Robo;
class TaskInfo
{
const PARAM_IS_REQUIRED = '__param_is_required__';
protected static $annotationRegex = '/@%s(?:[ \t]+(.*?))?[ \t]*\r?$/m';
/**
* @var \ReflectionMethod
*/
protected $reflection;
/**
* @var array
*/
protected $parsedDocBlock;
public function __construct($className, $methodName)
{
$this->reflection = new \ReflectionMethod($className, $methodName);
}
public function getDescription()
{
$desc = $this->getAnnotation('description');
if (!$desc) {
$desc = $this->getAnnotation('desc');
}
if (!$desc) {
$parsed = $this->parseDocBlock();
$desc = $parsed['description'];
}
return $desc;
}
public function getName()
{
$name = $this->getAnnotation('name');
if (!$name) {
$name = $this->reflection->getName();
}
$name = $this->convertName($name);
return $name;
}
public function getArguments()
{
$args = [];
$params = $this->reflection->getParameters();
foreach ($params as $key => $param) {
// last array value is option, not argument
if (($key == count($params)-1) and $param->isDefaultValueAvailable()) {
if ($this->isAssoc($param->getDefaultValue())) break;
}
// arrays are array arguments
if ($param->isArray()) {
if ($param->isDefaultValueAvailable()) {
if (!$this->isAssoc($param->getDefaultValue())) $args[$param->getName()] = $param->getDefaultValue();
} else {
$args[$param->getName()] = [];
}
continue;
}
// default values are optional arguments
$val = $param->isDefaultValueAvailable()
? $param->getDefaultValue()
: self::PARAM_IS_REQUIRED;
$args[$param->getName()] = $val;
}
return $args;
}
public function getOptions()
{
$params = $this->reflection->getParameters();
if (empty($params)) return [];
$param = end($params);
if (!$param->isDefaultValueAvailable()) return [];
if (!$this->isAssoc($param->getDefaultValue())) return [];
return $param->getDefaultValue();
}
public function getHelp()
{
$parsed = $this->parseDocBlock();
return $parsed['help'];
}
public function getArgumentDescription($name)
{
$parsed = $this->parseDocBlock();
if (array_key_exists($name, $parsed['param'])) {
return $parsed['param'][$name];
}
return '';
}
public function getOptionDescription($name)
{
$parsed = $this->parseDocBlock();
if (array_key_exists($name, $parsed['option'])) {
return $parsed['option'][$name];
}
return '';
}
protected function isAssoc($arr)
{
if (!is_array($arr)) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
protected function getAnnotation($annotation)
{
$docBlock = $this->reflection->getDocComment();
$matched = array();
$res = preg_match(sprintf(self::$annotationRegex, $annotation), $docBlock, $matched);
if (!$res) return null;
return $matched[1];
}
private function convertName($camel)
{
$splitter="-";
$camel=preg_replace('/(?!^)[[:upper:]][[:lower:]]/', '$0', preg_replace('/(?!^)[[:upper:]]+/', $splitter.'$0', $camel));
$camel = preg_replace("/$splitter/", ':', $camel, 1);
return strtolower($camel);
}
private function parseDocBlock()
{
if (!$this->parsedDocBlock) {
$parsed = [
'description' => [],
'help' => [],
'param' => [],
'option' => [],
];
$tag = '@(?P<tag>[^ \t]+)[ \t]+';
$name = '\\$(?P<name>[^ \t]+)[ \t]+';
$type = '(?P<type>[^ \t]+)[ \t]+';
$description = '(?P<description>.*)';
$isTag = '/^\*[* \t]+@/';
$option = "/{$tag}{$name}{$description}/";
$argument1 = "/{$tag}{$type}{$name}{$description}/";
$argument2 = "/{$tag}{$name}{$type}{$description}/";
$null = [];
$doc = $this->reflection->getDocComment();
if ($doc) {
$current =& $parsed['description'];
foreach (explode("\n", $doc) as $row) {
$row = trim($row);
if ($row == '/**' || $row == '*/') {
continue;
}
// @option definitions
if (stripos($row, '@option') !== false && preg_match($option, $row, $match)) {
$parsed[$match['tag']][$match['name']] = [$match['description']];
$current =& $parsed[$match['tag']][$match['name']];
}
// @param definitions where type is specified before the variable name
elseif (stripos($row, '@param') !== false && preg_match($argument1, $row, $match)) {
$parsed[$match['tag']][$match['name']] = [$match['description']];
$current =& $parsed[$match['tag']][$match['name']];
}
// @param definitions where type is specified after the variable name
elseif (stripos($row, '@param') !== false && preg_match($argument2, $row, $match)) {
$parsed[$match['tag']][$match['name']] = [$match['description']];
$current =& $parsed[$match['tag']][$match['name']];
}
// If no tag is defined is it treated as part of the last definition
elseif (!preg_match($isTag, $row)) {
$current[] = substr(trim($row, '*/'), 1);
if ($current === $parsed['description']) {
$current =& $parsed['help'];
}
}
// Anything else is discarded
else {
$current =& $null;
}
}
}
$parsed['description'] = $this->combineParsedComment($parsed['description']);
$parsed['help'] = trim($this->combineParsedComment($parsed['help'], true));
foreach ($parsed['param'] as &$param) {
$param = $this->combineParsedComment($param);
}
foreach ($parsed['option'] as &$option) {
$option = $this->combineParsedComment($option);
}
if (empty($parsed['description'])) {
$parsed['description'] = null;
}
if (empty($parsed['help'])) {
$parsed['help'] = null;
}
$this->parsedDocBlock = $parsed;
}
return $this->parsedDocBlock;
}
private function combineParsedComment(array $doc, $keepFormatting = false)
{
if ($keepFormatting) {
return implode(PHP_EOL, $doc);
}
return trim(implode(' ', array_filter(array_map('trim', $doc))));
}
}