|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\AnnotatedCommand\Parser\Internal; |
|
3
|
|
|
|
|
4
|
|
|
use phpDocumentor\Reflection\DocBlock; |
|
5
|
|
|
use phpDocumentor\Reflection\DocBlock\Tag\ParamTag; |
|
6
|
|
|
use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag; |
|
7
|
|
|
use Consolidation\AnnotatedCommand\Parser\CommandInfo; |
|
8
|
|
|
use Consolidation\AnnotatedCommand\Parser\DefaultsWithDescriptions; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Given a class and method name, parse the annotations in the |
|
12
|
|
|
* DocBlock comment, and provide accessor methods for all of |
|
13
|
|
|
* the elements that are needed to create an annotated Command. |
|
14
|
|
|
*/ |
|
15
|
|
|
class CommandDocBlockParser2 extends AbstractCommandDocBlockParser |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Parse the docBlock comment for this command, and set the |
|
19
|
|
|
* fields of this class with the data thereby obtained. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function parse() |
|
22
|
|
|
{ |
|
23
|
|
|
$docblockComment = $this->reflection->getDocComment(); |
|
24
|
|
|
$phpdoc = new DocBlock($docblockComment); |
|
25
|
|
|
|
|
26
|
|
|
// First set the description (synopsis) and help. |
|
27
|
|
|
$this->commandInfo->setDescription((string)$phpdoc->getShortDescription()); |
|
|
|
|
|
|
28
|
|
|
$this->commandInfo->setHelp((string)$phpdoc->getLongDescription()); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
// Iterate over all of the tags, and process them as necessary. |
|
31
|
|
View Code Duplication |
foreach ($phpdoc->getTags() as $tag) { |
|
|
|
|
|
|
32
|
|
|
$processFn = [$this, 'processGenericTag']; |
|
33
|
|
|
if (array_key_exists($tag->getName(), $this->tagProcessors)) { |
|
34
|
|
|
$processFn = [$this, $this->tagProcessors[$tag->getName()]]; |
|
35
|
|
|
} |
|
36
|
|
|
$processFn($tag); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Save any tag that we do not explicitly recognize in the |
|
42
|
|
|
* 'otherAnnotations' map. |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function processGenericTag($tag) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->commandInfo->addOtherAnnotation($tag->getName(), $tag->getContent()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set the name of the command from a @command or @name annotation. |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function processCommandTag($tag) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->commandInfo->setName($tag->getContent()); |
|
55
|
|
|
// We also store the name in the 'other annotations' so that is is |
|
56
|
|
|
// possible to determine if the method had a @command annotation. |
|
57
|
|
|
$this->processGenericTag($tag); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The @description and @desc annotations may be used in |
|
62
|
|
|
* place of the synopsis (which we call 'description'). |
|
63
|
|
|
* This is discouraged. |
|
64
|
|
|
* |
|
65
|
|
|
* @deprecated |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function processAlternateDescriptionTag($tag) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->commandInfo->setDescription($tag->getContent()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Store the data from a @arg annotation in our argument descriptions. |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function processArgumentTag($tag) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->addOptionOrArgumentTag($tag, $this->commandInfo->arguments()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Store the data from a @param annotation in our argument descriptions. |
|
82
|
|
|
*/ |
|
83
|
|
View Code Duplication |
protected function processParamTag($tag) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
if (!$tag instanceof ParamTag) { |
|
|
|
|
|
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
$variableName = $tag->getVariableName(); |
|
89
|
|
|
$variableName = str_replace('$', '', $variableName); |
|
90
|
|
|
$description = static::removeLineBreaks($tag->getDescription()); |
|
91
|
|
|
if ($variableName == $this->commandInfo->optionParamName()) { |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
$this->commandInfo->arguments()->add($variableName, $description); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Store the data from a @return annotation in our argument descriptions. |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function processReturnTag($tag) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$tag instanceof ReturnTag) { |
|
|
|
|
|
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
$this->commandInfo->setReturnType($tag->getType()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Store the data from an @option annotation in our option descriptions. |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function processOptionTag($tag) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->addOptionOrArgumentTag($tag, $this->commandInfo->options()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
protected function addOptionOrArgumentTag($tag, DefaultsWithDescriptions $set) |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
if (!$this->pregMatchNameAndDescription($tag->getDescription(), $match)) { |
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
$variableName = $this->commandInfo->findMatchingOption($match['name']); |
|
122
|
|
|
$desc = $match['description']; |
|
123
|
|
|
$description = static::removeLineBreaks($desc); |
|
124
|
|
|
$set->add($variableName, $description); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Store the data from a @default annotation in our argument or option store, |
|
129
|
|
|
* as appropriate. |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
protected function processDefaultTag($tag) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
if (!$this->pregMatchNameAndDescription($tag->getDescription(), $match)) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
$variableName = $match['name']; |
|
137
|
|
|
$defaultValue = $this->interpretDefaultValue($match['description']); |
|
138
|
|
|
if ($this->commandInfo->arguments()->exists($variableName)) { |
|
139
|
|
|
$this->commandInfo->arguments()->setDefaultValue($variableName, $defaultValue); |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
$variableName = $this->commandInfo->findMatchingOption($variableName); |
|
143
|
|
|
if ($this->commandInfo->options()->exists($variableName)) { |
|
144
|
|
|
$this->commandInfo->options()->setDefaultValue($variableName, $defaultValue); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Process the comma-separated list of aliases |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function processAliases($tag) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->commandInfo->setAliases($tag->getDescription()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Store the data from a @usage annotation in our example usage list. |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
protected function processUsageTag($tag) |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
$lines = explode("\n", $tag->getContent()); |
|
162
|
|
|
$usage = array_shift($lines); |
|
163
|
|
|
$description = static::removeLineBreaks(implode("\n", $lines)); |
|
164
|
|
|
|
|
165
|
|
|
$this->commandInfo->setExampleUsage($usage, $description); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.