1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\AnnotatedCommand\Parser\Internal; |
3
|
|
|
|
4
|
|
|
use Consolidation\AnnotatedCommand\Parser\CommandInfo; |
5
|
|
|
use Consolidation\AnnotatedCommand\Parser\DefaultsWithDescriptions; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Given a class and method name, parse the annotations in the |
9
|
|
|
* DocBlock comment, and provide accessor methods for all of |
10
|
|
|
* the elements that are needed to create an annotated Command. |
11
|
|
|
*/ |
12
|
|
|
class BespokeDocBlockParser |
13
|
|
|
{ |
14
|
|
|
protected $fqcnCache; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $tagProcessors = [ |
20
|
|
|
'command' => 'processCommandTag', |
21
|
|
|
'name' => 'processCommandTag', |
22
|
|
|
'arg' => 'processArgumentTag', |
23
|
|
|
'param' => 'processParamTag', |
24
|
|
|
'return' => 'processReturnTag', |
25
|
|
|
'option' => 'processOptionTag', |
26
|
|
|
'default' => 'processDefaultTag', |
27
|
|
|
'aliases' => 'processAliases', |
28
|
|
|
'usage' => 'processUsageTag', |
29
|
|
|
'description' => 'processAlternateDescriptionTag', |
30
|
|
|
'desc' => 'processAlternateDescriptionTag', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
public function __construct(CommandInfo $commandInfo, \ReflectionMethod $reflection, $fqcnCache = null) |
34
|
|
|
{ |
35
|
|
|
$this->commandInfo = $commandInfo; |
|
|
|
|
36
|
|
|
$this->reflection = $reflection; |
|
|
|
|
37
|
|
|
$this->fqcnCache = $fqcnCache ?: new FullyQualifiedClassCache(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Parse the docBlock comment for this command, and set the |
42
|
|
|
* fields of this class with the data thereby obtained. |
43
|
|
|
*/ |
44
|
|
|
public function parse() |
45
|
|
|
{ |
46
|
|
|
$doc = $this->reflection->getDocComment(); |
47
|
|
|
$this->parseDocBlock($doc); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Save any tag that we do not explicitly recognize in the |
52
|
|
|
* 'otherAnnotations' map. |
53
|
|
|
*/ |
54
|
|
|
protected function processGenericTag($tag) |
55
|
|
|
{ |
56
|
|
|
$this->commandInfo->addAnnotation($tag->getTag(), $tag->getContent()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set the name of the command from a @command or @name annotation. |
61
|
|
|
*/ |
62
|
|
|
protected function processCommandTag($tag) |
63
|
|
|
{ |
64
|
|
|
if (!$tag->hasWordAndDescription($matches)) { |
|
|
|
|
65
|
|
|
throw new \Exception('Could not determine command name from tag ' . (string)$tag); |
66
|
|
|
} |
67
|
|
|
$commandName = $matches['word']; |
68
|
|
|
$this->commandInfo->setName($commandName); |
69
|
|
|
// We also store the name in the 'other annotations' so that is is |
70
|
|
|
// possible to determine if the method had a @command annotation. |
71
|
|
|
$this->commandInfo->addAnnotation($tag->getTag(), $commandName); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The @description and @desc annotations may be used in |
76
|
|
|
* place of the synopsis (which we call 'description'). |
77
|
|
|
* This is discouraged. |
78
|
|
|
* |
79
|
|
|
* @deprecated |
80
|
|
|
*/ |
81
|
|
|
protected function processAlternateDescriptionTag($tag) |
82
|
|
|
{ |
83
|
|
|
$this->commandInfo->setDescription($tag->getContent()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Store the data from a @param annotation in our argument descriptions. |
88
|
|
|
*/ |
89
|
|
|
protected function processParamTag($tag) |
90
|
|
|
{ |
91
|
|
|
if ($tag->hasTypeVariableAndDescription($matches)) { |
92
|
|
|
if ($this->ignoredParamType($matches['type'])) { |
|
|
|
|
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return $this->processArgumentTag($tag); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function ignoredParamType($paramType) |
100
|
|
|
{ |
101
|
|
|
// TODO: We should really only allow a couple of types here, |
102
|
|
|
// e.g. 'string', 'array', 'bool'. Blacklist things we do not |
103
|
|
|
// want for now to avoid breaking commands with weird types. |
104
|
|
|
// Fix in the next major version. |
105
|
|
|
// |
106
|
|
|
// This works: |
107
|
|
|
// return !in_array($paramType, ['string', 'array', 'integer', 'bool']); |
108
|
|
|
return preg_match('#(InputInterface|OutputInterface)$#', $paramType); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Store the data from a @arg annotation in our argument descriptions. |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
protected function processArgumentTag($tag) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
if (!$tag->hasVariable($matches)) { |
|
|
|
|
117
|
|
|
throw new \Exception('Could not determine argument name from tag ' . (string)$tag); |
118
|
|
|
} |
119
|
|
|
if ($matches['variable'] == $this->optionParamName()) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
$this->addOptionOrArgumentTag($tag, $this->commandInfo->arguments(), $matches['variable'], $matches['description']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Store the data from an @option annotation in our option descriptions. |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
protected function processOptionTag($tag) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
if (!$tag->hasVariable($matches)) { |
|
|
|
|
131
|
|
|
throw new \Exception('Could not determine option name from tag ' . (string)$tag); |
132
|
|
|
} |
133
|
|
|
$this->addOptionOrArgumentTag($tag, $this->commandInfo->options(), $matches['variable'], $matches['description']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function addOptionOrArgumentTag($tag, DefaultsWithDescriptions $set, $name, $description) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$variableName = $this->commandInfo->findMatchingOption($name); |
139
|
|
|
$description = static::removeLineBreaks($description); |
140
|
|
|
$set->add($variableName, $description); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Store the data from a @default annotation in our argument or option store, |
145
|
|
|
* as appropriate. |
146
|
|
|
*/ |
147
|
|
|
protected function processDefaultTag($tag) |
148
|
|
|
{ |
149
|
|
|
if (!$tag->hasVariable($matches)) { |
|
|
|
|
150
|
|
|
throw new \Exception('Could not determine parameter name for default value from tag ' . (string)$tag); |
151
|
|
|
} |
152
|
|
|
$variableName = $matches['variable']; |
153
|
|
|
$defaultValue = $this->interpretDefaultValue($matches['description']); |
154
|
|
|
if ($this->commandInfo->arguments()->exists($variableName)) { |
155
|
|
|
$this->commandInfo->arguments()->setDefaultValue($variableName, $defaultValue); |
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
$variableName = $this->commandInfo->findMatchingOption($variableName); |
159
|
|
|
if ($this->commandInfo->options()->exists($variableName)) { |
160
|
|
|
$this->commandInfo->options()->setDefaultValue($variableName, $defaultValue); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Store the data from a @usage annotation in our example usage list. |
166
|
|
|
*/ |
167
|
|
|
protected function processUsageTag($tag) |
168
|
|
|
{ |
169
|
|
|
$lines = explode("\n", $tag->getContent()); |
170
|
|
|
$usage = trim(array_shift($lines)); |
171
|
|
|
$description = static::removeLineBreaks(implode("\n", array_map(function ($line) { |
172
|
|
|
return trim($line); |
173
|
|
|
}, $lines))); |
174
|
|
|
|
175
|
|
|
$this->commandInfo->setExampleUsage($usage, $description); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Process the comma-separated list of aliases |
180
|
|
|
*/ |
181
|
|
|
protected function processAliases($tag) |
182
|
|
|
{ |
183
|
|
|
$this->commandInfo->setAliases((string)$tag->getContent()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Store the data from a @return annotation in our argument descriptions. |
188
|
|
|
*/ |
189
|
|
|
protected function processReturnTag($tag) |
190
|
|
|
{ |
191
|
|
|
// The return type might be a variable -- '$this'. It will |
192
|
|
|
// usually be a type, like RowsOfFields, or \Namespace\RowsOfFields. |
193
|
|
|
if (!$tag->hasVariableAndDescription($matches)) { |
|
|
|
|
194
|
|
|
throw new \Exception('Could not determine return type from tag ' . (string)$tag); |
195
|
|
|
} |
196
|
|
|
// Look at namespace and `use` statments to make returnType a fqdn |
197
|
|
|
$returnType = $matches['variable']; |
198
|
|
|
$returnType = $this->findFullyQualifiedClass($returnType); |
199
|
|
|
$this->commandInfo->setReturnType($returnType); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
protected function findFullyQualifiedClass($className) |
203
|
|
|
{ |
204
|
|
|
if (strpos($className, '\\') !== false) { |
205
|
|
|
return $className; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this->fqcnCache->qualify($this->reflection->getFileName(), $className); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
private function parseDocBlock($doc) |
212
|
|
|
{ |
213
|
|
|
// Remove the leading /** and the trailing */ |
214
|
|
|
$doc = preg_replace('#^\s*/\*+\s*#', '', $doc); |
215
|
|
|
$doc = preg_replace('#\s*\*+/\s*#', '', $doc); |
216
|
|
|
|
217
|
|
|
// Nothing left? Exit. |
218
|
|
|
if (empty($doc)) { |
219
|
|
|
return; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$tagFactory = new TagFactory(); |
223
|
|
|
$lines = []; |
224
|
|
|
|
225
|
|
|
foreach (explode("\n", $doc) as $row) { |
226
|
|
|
// Remove trailing whitespace and leading space + '*'s |
227
|
|
|
$row = rtrim($row); |
228
|
|
|
$row = preg_replace('#^[ \t]*\**#', '', $row); |
229
|
|
|
|
230
|
|
|
if (!$tagFactory->parseLine($row)) { |
231
|
|
|
$lines[] = $row; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$this->processDescriptionAndHelp($lines); |
236
|
|
|
$this->processAllTags($tagFactory->getTags()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
protected function processDescriptionAndHelp($lines) |
240
|
|
|
{ |
241
|
|
|
// Trim all of the lines individually. |
242
|
|
|
$lines = |
243
|
|
|
array_map( |
244
|
|
|
function ($line) { |
245
|
|
|
return trim($line); |
246
|
|
|
}, |
247
|
|
|
$lines |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
// Everything up to the first blank line goes in the description. |
251
|
|
|
$description = array_shift($lines); |
252
|
|
|
while ($this->nextLineIsNotEmpty($lines)) { |
253
|
|
|
$description .= ' ' . array_shift($lines); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// Everything else goes in the help. |
257
|
|
|
$help = trim(implode("\n", $lines)); |
258
|
|
|
|
259
|
|
|
$this->commandInfo->setDescription($description); |
260
|
|
|
$this->commandInfo->setHelp($help); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
protected function nextLineIsNotEmpty($lines) |
264
|
|
|
{ |
265
|
|
|
if (empty($lines)) { |
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$nextLine = trim($lines[0]); |
270
|
|
|
return !empty($nextLine); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
protected function processAllTags($tags) |
274
|
|
|
{ |
275
|
|
|
// Iterate over all of the tags, and process them as necessary. |
276
|
|
|
foreach ($tags as $tag) { |
277
|
|
|
$processFn = [$this, 'processGenericTag']; |
278
|
|
|
if (array_key_exists($tag->getTag(), $this->tagProcessors)) { |
279
|
|
|
$processFn = [$this, $this->tagProcessors[$tag->getTag()]]; |
280
|
|
|
} |
281
|
|
|
$processFn($tag); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
protected function lastParameterName() |
286
|
|
|
{ |
287
|
|
|
$params = $this->commandInfo->getParameters(); |
288
|
|
|
$param = end($params); |
289
|
|
|
if (!$param) { |
290
|
|
|
return ''; |
291
|
|
|
} |
292
|
|
|
return $param->name; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Return the name of the last parameter if it holds the options. |
297
|
|
|
*/ |
298
|
|
|
public function optionParamName() |
299
|
|
|
{ |
300
|
|
|
// Remember the name of the last parameter, if it holds the options. |
301
|
|
|
// We will use this information to ignore @param annotations for the options. |
302
|
|
|
if (!isset($this->optionParamName)) { |
303
|
|
|
$this->optionParamName = ''; |
|
|
|
|
304
|
|
|
$options = $this->commandInfo->options(); |
305
|
|
|
if (!$options->isEmpty()) { |
306
|
|
|
$this->optionParamName = $this->lastParameterName(); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $this->optionParamName; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
protected function interpretDefaultValue($defaultValue) |
314
|
|
|
{ |
315
|
|
|
$defaults = [ |
316
|
|
|
'null' => null, |
317
|
|
|
'true' => true, |
318
|
|
|
'false' => false, |
319
|
|
|
"''" => '', |
320
|
|
|
'[]' => [], |
321
|
|
|
]; |
322
|
|
|
foreach ($defaults as $defaultName => $defaultTypedValue) { |
323
|
|
|
if ($defaultValue == $defaultName) { |
324
|
|
|
return $defaultTypedValue; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
return $defaultValue; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
332
|
|
|
* convert the data into the last of these forms. |
333
|
|
|
*/ |
334
|
|
|
protected static function convertListToCommaSeparated($text) |
335
|
|
|
{ |
336
|
|
|
return preg_replace('#[ \t\n\r,]+#', ',', $text); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Take a multiline description and convert it into a single |
341
|
|
|
* long unbroken line. |
342
|
|
|
*/ |
343
|
|
|
protected static function removeLineBreaks($text) |
344
|
|
|
{ |
345
|
|
|
return trim(preg_replace('#[ \t\n\r]+#', ' ', $text)); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: