This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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; |
||
0 ignored issues
–
show
|
|||
36 | $this->reflection = $reflection; |
||
0 ignored issues
–
show
The property
reflection does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
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)) { |
||
0 ignored issues
–
show
|
|||
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'])) { |
||
0 ignored issues
–
show
|
|||
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) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
115 | { |
||
116 | if (!$tag->hasVariable($matches)) { |
||
0 ignored issues
–
show
|
|||
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) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
129 | { |
||
130 | if (!$tag->hasVariable($matches)) { |
||
0 ignored issues
–
show
|
|||
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) |
||
0 ignored issues
–
show
|
|||
137 | { |
||
138 | $variableName = $this->commandInfo->findMatchingOption($name); |
||
139 | $description = static::removeLineBreaks($description); |
||
140 | list($description, $defaultValue) = $this->splitOutDefault($description); |
||
141 | $set->add($variableName, $description); |
||
142 | if ($defaultValue !== null) { |
||
143 | $set->setDefaultValue($variableName, $defaultValue); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | protected function splitOutDefault($description) |
||
148 | { |
||
149 | if (!preg_match('#(.*)(Default: *)(.*)#', trim($description), $matches)) { |
||
150 | return [$description, null]; |
||
151 | } |
||
152 | |||
153 | return [trim($matches[1]), $this->interpretDefaultValue(trim($matches[3]))]; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Store the data from a @default annotation in our argument or option store, |
||
158 | * as appropriate. |
||
159 | */ |
||
160 | protected function processDefaultTag($tag) |
||
161 | { |
||
162 | if (!$tag->hasVariable($matches)) { |
||
0 ignored issues
–
show
|
|||
163 | throw new \Exception('Could not determine parameter name for default value from tag ' . (string)$tag); |
||
164 | } |
||
165 | $variableName = $matches['variable']; |
||
166 | $defaultValue = $this->interpretDefaultValue($matches['description']); |
||
167 | if ($this->commandInfo->arguments()->exists($variableName)) { |
||
168 | $this->commandInfo->arguments()->setDefaultValue($variableName, $defaultValue); |
||
169 | return; |
||
170 | } |
||
171 | $variableName = $this->commandInfo->findMatchingOption($variableName); |
||
172 | if ($this->commandInfo->options()->exists($variableName)) { |
||
173 | $this->commandInfo->options()->setDefaultValue($variableName, $defaultValue); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Store the data from a @usage annotation in our example usage list. |
||
179 | */ |
||
180 | protected function processUsageTag($tag) |
||
181 | { |
||
182 | $lines = explode("\n", $tag->getContent()); |
||
183 | $usage = trim(array_shift($lines)); |
||
184 | $description = static::removeLineBreaks(implode("\n", array_map(function ($line) { |
||
185 | return trim($line); |
||
186 | }, $lines))); |
||
187 | |||
188 | $this->commandInfo->setExampleUsage($usage, $description); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Process the comma-separated list of aliases |
||
193 | */ |
||
194 | protected function processAliases($tag) |
||
195 | { |
||
196 | $this->commandInfo->setAliases((string)$tag->getContent()); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Store the data from a @return annotation in our argument descriptions. |
||
201 | */ |
||
202 | protected function processReturnTag($tag) |
||
203 | { |
||
204 | // The return type might be a variable -- '$this'. It will |
||
205 | // usually be a type, like RowsOfFields, or \Namespace\RowsOfFields. |
||
206 | if (!$tag->hasVariableAndDescription($matches)) { |
||
0 ignored issues
–
show
|
|||
207 | throw new \Exception('Could not determine return type from tag ' . (string)$tag); |
||
208 | } |
||
209 | // Look at namespace and `use` statments to make returnType a fqdn |
||
210 | $returnType = $matches['variable']; |
||
211 | $returnType = $this->findFullyQualifiedClass($returnType); |
||
212 | $this->commandInfo->setReturnType($returnType); |
||
213 | } |
||
214 | |||
215 | protected function findFullyQualifiedClass($className) |
||
216 | { |
||
217 | if (strpos($className, '\\') !== false) { |
||
218 | return $className; |
||
219 | } |
||
220 | |||
221 | return $this->fqcnCache->qualify($this->reflection->getFileName(), $className); |
||
222 | } |
||
223 | |||
224 | private function parseDocBlock($doc) |
||
225 | { |
||
226 | // Remove the leading /** and the trailing */ |
||
227 | $doc = preg_replace('#^\s*/\*+\s*#', '', $doc); |
||
228 | $doc = preg_replace('#\s*\*+/\s*#', '', $doc); |
||
229 | |||
230 | // Nothing left? Exit. |
||
231 | if (empty($doc)) { |
||
232 | return; |
||
233 | } |
||
234 | |||
235 | $tagFactory = new TagFactory(); |
||
236 | $lines = []; |
||
237 | |||
238 | foreach (explode("\n", $doc) as $row) { |
||
239 | // Remove trailing whitespace and leading space + '*'s |
||
240 | $row = rtrim($row); |
||
241 | $row = preg_replace('#^[ \t]*\**#', '', $row); |
||
242 | |||
243 | if (!$tagFactory->parseLine($row)) { |
||
244 | $lines[] = $row; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | $this->processDescriptionAndHelp($lines); |
||
249 | $this->processAllTags($tagFactory->getTags()); |
||
250 | } |
||
251 | |||
252 | protected function processDescriptionAndHelp($lines) |
||
253 | { |
||
254 | // Trim all of the lines individually. |
||
255 | $lines = |
||
256 | array_map( |
||
257 | function ($line) { |
||
258 | return trim($line); |
||
259 | }, |
||
260 | $lines |
||
261 | ); |
||
262 | |||
263 | // Everything up to the first blank line goes in the description. |
||
264 | $description = array_shift($lines); |
||
265 | while ($this->nextLineIsNotEmpty($lines)) { |
||
266 | $description .= ' ' . array_shift($lines); |
||
267 | } |
||
268 | |||
269 | // Everything else goes in the help. |
||
270 | $help = trim(implode("\n", $lines)); |
||
271 | |||
272 | $this->commandInfo->setDescription($description); |
||
273 | $this->commandInfo->setHelp($help); |
||
274 | } |
||
275 | |||
276 | protected function nextLineIsNotEmpty($lines) |
||
277 | { |
||
278 | if (empty($lines)) { |
||
279 | return false; |
||
280 | } |
||
281 | |||
282 | $nextLine = trim($lines[0]); |
||
283 | return !empty($nextLine); |
||
284 | } |
||
285 | |||
286 | protected function processAllTags($tags) |
||
287 | { |
||
288 | // Iterate over all of the tags, and process them as necessary. |
||
289 | foreach ($tags as $tag) { |
||
290 | $processFn = [$this, 'processGenericTag']; |
||
291 | if (array_key_exists($tag->getTag(), $this->tagProcessors)) { |
||
292 | $processFn = [$this, $this->tagProcessors[$tag->getTag()]]; |
||
293 | } |
||
294 | $processFn($tag); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | protected function lastParameterName() |
||
299 | { |
||
300 | $params = $this->commandInfo->getParameters(); |
||
301 | $param = end($params); |
||
302 | if (!$param) { |
||
303 | return ''; |
||
304 | } |
||
305 | return $param->name; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Return the name of the last parameter if it holds the options. |
||
310 | */ |
||
311 | public function optionParamName() |
||
312 | { |
||
313 | // Remember the name of the last parameter, if it holds the options. |
||
314 | // We will use this information to ignore @param annotations for the options. |
||
315 | if (!isset($this->optionParamName)) { |
||
316 | $this->optionParamName = ''; |
||
0 ignored issues
–
show
The property
optionParamName does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
317 | $options = $this->commandInfo->options(); |
||
318 | if (!$options->isEmpty()) { |
||
319 | $this->optionParamName = $this->lastParameterName(); |
||
320 | } |
||
321 | } |
||
322 | |||
323 | return $this->optionParamName; |
||
324 | } |
||
325 | |||
326 | protected function interpretDefaultValue($defaultValue) |
||
327 | { |
||
328 | $defaults = [ |
||
329 | 'null' => null, |
||
330 | 'true' => true, |
||
331 | 'false' => false, |
||
332 | "''" => '', |
||
333 | '[]' => [], |
||
334 | ]; |
||
335 | foreach ($defaults as $defaultName => $defaultTypedValue) { |
||
336 | if ($defaultValue == $defaultName) { |
||
337 | return $defaultTypedValue; |
||
338 | } |
||
339 | } |
||
340 | return $defaultValue; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
345 | * convert the data into the last of these forms. |
||
346 | */ |
||
347 | protected static function convertListToCommaSeparated($text) |
||
348 | { |
||
349 | return preg_replace('#[ \t\n\r,]+#', ',', $text); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Take a multiline description and convert it into a single |
||
354 | * long unbroken line. |
||
355 | */ |
||
356 | protected static function removeLineBreaks($text) |
||
357 | { |
||
358 | return trim(preg_replace('#[ \t\n\r]+#', ' ', $text)); |
||
359 | } |
||
360 | } |
||
361 |
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: