Total Complexity | 91 |
Total Lines | 688 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class Controller extends \yii\base\Controller |
||
44 | { |
||
45 | /** |
||
46 | * @deprecated since 2.0.13. Use [[ExitCode::OK]] instead. |
||
47 | */ |
||
48 | const EXIT_CODE_NORMAL = 0; |
||
49 | /** |
||
50 | * @deprecated since 2.0.13. Use [[ExitCode::UNSPECIFIED_ERROR]] instead. |
||
51 | */ |
||
52 | const EXIT_CODE_ERROR = 1; |
||
53 | |||
54 | /** |
||
55 | * @var bool whether to run the command interactively. |
||
56 | */ |
||
57 | public $interactive = true; |
||
58 | /** |
||
59 | * @var bool whether to enable ANSI color in the output. |
||
60 | * If not set, ANSI color will only be enabled for terminals that support it. |
||
61 | */ |
||
62 | public $color; |
||
63 | /** |
||
64 | * @var bool whether to display help information about current command. |
||
65 | * @since 2.0.10 |
||
66 | */ |
||
67 | public $help; |
||
68 | /** |
||
69 | * @var bool if TRUE - script finish with `ExitCode::OK` in case of exception. |
||
70 | * FALSE - `ExitCode::UNSPECIFIED_ERROR`. |
||
71 | * Default: `YII_ENV_TEST` |
||
72 | * @since 2.0.36 |
||
73 | */ |
||
74 | public $silentExitOnException; |
||
75 | |||
76 | /** |
||
77 | * @var array the options passed during execution. |
||
78 | */ |
||
79 | private $_passedOptions = []; |
||
80 | |||
81 | |||
82 | public function beforeAction($action) |
||
83 | { |
||
84 | $silentExit = $this->silentExitOnException !== null ? $this->silentExitOnException : YII_ENV_TEST; |
||
85 | Yii::$app->errorHandler->silentExitOnException = $silentExit; |
||
86 | |||
87 | return parent::beforeAction($action); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Returns a value indicating whether ANSI color is enabled. |
||
92 | * |
||
93 | * ANSI color is enabled only if [[color]] is set true or is not set |
||
94 | * and the terminal supports ANSI color. |
||
95 | * |
||
96 | * @param resource $stream the stream to check. |
||
97 | * @return bool Whether to enable ANSI style in output. |
||
98 | */ |
||
99 | public function isColorEnabled($stream = \STDOUT) |
||
100 | { |
||
101 | return $this->color === null ? Console::streamSupportsAnsiColors($stream) : $this->color; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Runs an action with the specified action ID and parameters. |
||
106 | * If the action ID is empty, the method will use [[defaultAction]]. |
||
107 | * @param string $id the ID of the action to be executed. |
||
108 | * @param array $params the parameters (name-value pairs) to be passed to the action. |
||
109 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
110 | * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. |
||
111 | * @throws Exception if there are unknown options or missing arguments |
||
112 | * @see createAction |
||
113 | */ |
||
114 | public function runAction($id, $params = []) |
||
115 | { |
||
116 | if (!empty($params)) { |
||
117 | // populate options here so that they are available in beforeAction(). |
||
118 | $options = $this->options($id === '' ? $this->defaultAction : $id); |
||
119 | if (isset($params['_aliases'])) { |
||
120 | $optionAliases = $this->optionAliases(); |
||
121 | foreach ($params['_aliases'] as $name => $value) { |
||
122 | if (array_key_exists($name, $optionAliases)) { |
||
123 | $params[$optionAliases[$name]] = $value; |
||
124 | } else { |
||
125 | $message = Yii::t('yii', 'Unknown alias: -{name}', ['name' => $name]); |
||
126 | if (!empty($optionAliases)) { |
||
127 | $aliasesAvailable = []; |
||
128 | foreach ($optionAliases as $alias => $option) { |
||
129 | $aliasesAvailable[] = '-' . $alias . ' (--' . $option . ')'; |
||
130 | } |
||
131 | |||
132 | $message .= '. ' . Yii::t('yii', 'Aliases available: {aliases}', [ |
||
133 | 'aliases' => implode(', ', $aliasesAvailable) |
||
134 | ]); |
||
135 | } |
||
136 | throw new Exception($message); |
||
137 | } |
||
138 | } |
||
139 | unset($params['_aliases']); |
||
140 | } |
||
141 | foreach ($params as $name => $value) { |
||
142 | // Allow camelCase options to be entered in kebab-case |
||
143 | if (!in_array($name, $options, true) && strpos($name, '-') !== false) { |
||
144 | $kebabName = $name; |
||
145 | $altName = lcfirst(Inflector::id2camel($kebabName)); |
||
146 | if (in_array($altName, $options, true)) { |
||
147 | $name = $altName; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | if (in_array($name, $options, true)) { |
||
152 | $default = $this->$name; |
||
153 | if (is_array($default)) { |
||
154 | $this->$name = preg_split('/\s*,\s*(?![^()]*\))/', $value); |
||
155 | } elseif ($default !== null) { |
||
156 | settype($value, gettype($default)); |
||
157 | $this->$name = $value; |
||
158 | } else { |
||
159 | $this->$name = $value; |
||
160 | } |
||
161 | $this->_passedOptions[] = $name; |
||
162 | unset($params[$name]); |
||
163 | if (isset($kebabName)) { |
||
164 | unset($params[$kebabName]); |
||
165 | } |
||
166 | } elseif (!is_int($name)) { |
||
167 | $message = Yii::t('yii', 'Unknown option: --{name}', ['name' => $name]); |
||
168 | if (!empty($options)) { |
||
169 | $message .= '. ' . Yii::t('yii', 'Options available: {options}', ['options' => '--' . implode(', --', $options)]); |
||
170 | } |
||
171 | |||
172 | throw new Exception($message); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | if ($this->help) { |
||
177 | $route = $this->getUniqueId() . '/' . $id; |
||
178 | return Yii::$app->runAction('help', [$route]); |
||
179 | } |
||
180 | |||
181 | return parent::runAction($id, $params); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Binds the parameters to the action. |
||
186 | * This method is invoked by [[Action]] when it begins to run with the given parameters. |
||
187 | * This method will first bind the parameters with the [[options()|options]] |
||
188 | * available to the action. It then validates the given arguments. |
||
189 | * @param Action $action the action to be bound with parameters |
||
190 | * @param array $params the parameters to be bound to the action |
||
191 | * @return array the valid parameters that the action can run with. |
||
192 | * @throws Exception if there are unknown options or missing arguments |
||
193 | */ |
||
194 | public function bindActionParams($action, $params) |
||
195 | { |
||
196 | if ($action instanceof InlineAction) { |
||
197 | $method = new \ReflectionMethod($this, $action->actionMethod); |
||
198 | } else { |
||
199 | $method = new \ReflectionMethod($action, 'run'); |
||
200 | } |
||
201 | |||
202 | $args = []; |
||
203 | $missing = []; |
||
204 | $actionParams = []; |
||
205 | $requestedParams = []; |
||
206 | foreach ($method->getParameters() as $i => $param) { |
||
207 | $name = $param->getName(); |
||
208 | $key = null; |
||
209 | if (array_key_exists($i, $params)) { |
||
210 | $key = $i; |
||
211 | } elseif (array_key_exists($name, $params)) { |
||
212 | $key = $name; |
||
213 | } |
||
214 | |||
215 | if ($key !== null) { |
||
216 | if ($param->isArray()) { |
||
217 | $params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]); |
||
218 | } |
||
219 | $args[] = $actionParams[$key] = $params[$key]; |
||
220 | unset($params[$key]); |
||
221 | } elseif (PHP_VERSION_ID >= 70100 && ($type = $param->getType()) !== null && !$type->isBuiltin()) { |
||
222 | try { |
||
223 | $this->bindInjectedParams($type, $name, $args, $requestedParams); |
||
224 | } catch (\yii\base\Exception $e) { |
||
225 | throw new Exception($e->getMessage()); |
||
226 | } |
||
227 | } elseif ($param->isDefaultValueAvailable()) { |
||
228 | $args[] = $actionParams[$i] = $param->getDefaultValue(); |
||
229 | } else { |
||
230 | $missing[] = $name; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | if (!empty($missing)) { |
||
235 | throw new Exception(Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', $missing)])); |
||
236 | } |
||
237 | |||
238 | // We use a different array here, specifically one that doesn't contain service instances but descriptions instead. |
||
239 | if (\Yii::$app->requestedParams === null) { |
||
240 | \Yii::$app->requestedParams = array_merge($actionParams, $requestedParams); |
||
241 | } |
||
242 | |||
243 | return $args; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Formats a string with ANSI codes. |
||
248 | * |
||
249 | * You may pass additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
250 | * |
||
251 | * Example: |
||
252 | * |
||
253 | * ``` |
||
254 | * echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
255 | * ``` |
||
256 | * |
||
257 | * @param string $string the string to be formatted |
||
258 | * @return string |
||
259 | */ |
||
260 | public function ansiFormat($string) |
||
261 | { |
||
262 | if ($this->isColorEnabled()) { |
||
263 | $args = func_get_args(); |
||
264 | array_shift($args); |
||
265 | $string = Console::ansiFormat($string, $args); |
||
266 | } |
||
267 | |||
268 | return $string; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Prints a string to STDOUT. |
||
273 | * |
||
274 | * You may optionally format the string with ANSI codes by |
||
275 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
276 | * |
||
277 | * Example: |
||
278 | * |
||
279 | * ``` |
||
280 | * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
281 | * ``` |
||
282 | * |
||
283 | * @param string $string the string to print |
||
284 | * @param int ...$args additional parameters to decorate the output |
||
285 | * @return int|bool Number of bytes printed or false on error |
||
286 | */ |
||
287 | public function stdout($string) |
||
288 | { |
||
289 | if ($this->isColorEnabled()) { |
||
290 | $args = func_get_args(); |
||
291 | array_shift($args); |
||
292 | $string = Console::ansiFormat($string, $args); |
||
293 | } |
||
294 | |||
295 | return Console::stdout($string); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Prints a string to STDERR. |
||
300 | * |
||
301 | * You may optionally format the string with ANSI codes by |
||
302 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
303 | * |
||
304 | * Example: |
||
305 | * |
||
306 | * ``` |
||
307 | * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
308 | * ``` |
||
309 | * |
||
310 | * @param string $string the string to print |
||
311 | * @return int|bool Number of bytes printed or false on error |
||
312 | */ |
||
313 | public function stderr($string) |
||
314 | { |
||
315 | if ($this->isColorEnabled(\STDERR)) { |
||
316 | $args = func_get_args(); |
||
317 | array_shift($args); |
||
318 | $string = Console::ansiFormat($string, $args); |
||
319 | } |
||
320 | |||
321 | return fwrite(\STDERR, $string); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Prompts the user for input and validates it. |
||
326 | * |
||
327 | * @param string $text prompt string |
||
328 | * @param array $options the options to validate the input: |
||
329 | * |
||
330 | * - required: whether it is required or not |
||
331 | * - default: default value if no input is inserted by the user |
||
332 | * - pattern: regular expression pattern to validate user input |
||
333 | * - validator: a callable function to validate input. The function must accept two parameters: |
||
334 | * - $input: the user input to validate |
||
335 | * - $error: the error value passed by reference if validation failed. |
||
336 | * |
||
337 | * An example of how to use the prompt method with a validator function. |
||
338 | * |
||
339 | * ```php |
||
340 | * $code = $this->prompt('Enter 4-Chars-Pin', ['required' => true, 'validator' => function($input, &$error) { |
||
341 | * if (strlen($input) !== 4) { |
||
342 | * $error = 'The Pin must be exactly 4 chars!'; |
||
343 | * return false; |
||
344 | * } |
||
345 | * return true; |
||
346 | * }]); |
||
347 | * ``` |
||
348 | * |
||
349 | * @return string the user input |
||
350 | */ |
||
351 | public function prompt($text, $options = []) |
||
352 | { |
||
353 | if ($this->interactive) { |
||
354 | return Console::prompt($text, $options); |
||
355 | } |
||
356 | |||
357 | return isset($options['default']) ? $options['default'] : ''; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Asks user to confirm by typing y or n. |
||
362 | * |
||
363 | * A typical usage looks like the following: |
||
364 | * |
||
365 | * ```php |
||
366 | * if ($this->confirm("Are you sure?")) { |
||
367 | * echo "user typed yes\n"; |
||
368 | * } else { |
||
369 | * echo "user typed no\n"; |
||
370 | * } |
||
371 | * ``` |
||
372 | * |
||
373 | * @param string $message to echo out before waiting for user input |
||
374 | * @param bool $default this value is returned if no selection is made. |
||
375 | * @return bool whether user confirmed. |
||
376 | * Will return true if [[interactive]] is false. |
||
377 | */ |
||
378 | public function confirm($message, $default = false) |
||
379 | { |
||
380 | if ($this->interactive) { |
||
381 | return Console::confirm($message, $default); |
||
382 | } |
||
383 | |||
384 | return true; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Gives the user an option to choose from. Giving '?' as an input will show |
||
389 | * a list of options to choose from and their explanations. |
||
390 | * |
||
391 | * @param string $prompt the prompt message |
||
392 | * @param array $options Key-value array of options to choose from |
||
393 | * |
||
394 | * @return string An option character the user chose |
||
395 | */ |
||
396 | public function select($prompt, $options = []) |
||
397 | { |
||
398 | return Console::select($prompt, $options); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Returns the names of valid options for the action (id) |
||
403 | * An option requires the existence of a public member variable whose |
||
404 | * name is the option name. |
||
405 | * Child classes may override this method to specify possible options. |
||
406 | * |
||
407 | * Note that the values setting via options are not available |
||
408 | * until [[beforeAction()]] is being called. |
||
409 | * |
||
410 | * @param string $actionID the action id of the current request |
||
411 | * @return string[] the names of the options valid for the action |
||
412 | */ |
||
413 | public function options($actionID) |
||
|
|||
414 | { |
||
415 | // $actionId might be used in subclasses to provide options specific to action id |
||
416 | return ['color', 'interactive', 'help', 'silentExitOnException']; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Returns option alias names. |
||
421 | * Child classes may override this method to specify alias options. |
||
422 | * |
||
423 | * @return array the options alias names valid for the action |
||
424 | * where the keys is alias name for option and value is option name. |
||
425 | * |
||
426 | * @since 2.0.8 |
||
427 | * @see options() |
||
428 | */ |
||
429 | public function optionAliases() |
||
430 | { |
||
431 | return [ |
||
432 | 'h' => 'help', |
||
433 | ]; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Returns properties corresponding to the options for the action id |
||
438 | * Child classes may override this method to specify possible properties. |
||
439 | * |
||
440 | * @param string $actionID the action id of the current request |
||
441 | * @return array properties corresponding to the options for the action |
||
442 | */ |
||
443 | public function getOptionValues($actionID) |
||
444 | { |
||
445 | // $actionId might be used in subclasses to provide properties specific to action id |
||
446 | $properties = []; |
||
447 | foreach ($this->options($this->action->id) as $property) { |
||
448 | $properties[$property] = $this->$property; |
||
449 | } |
||
450 | |||
451 | return $properties; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Returns the names of valid options passed during execution. |
||
456 | * |
||
457 | * @return array the names of the options passed during execution |
||
458 | */ |
||
459 | public function getPassedOptions() |
||
460 | { |
||
461 | return $this->_passedOptions; |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Returns the properties corresponding to the passed options. |
||
466 | * |
||
467 | * @return array the properties corresponding to the passed options |
||
468 | */ |
||
469 | public function getPassedOptionValues() |
||
470 | { |
||
471 | $properties = []; |
||
472 | foreach ($this->_passedOptions as $property) { |
||
473 | $properties[$property] = $this->$property; |
||
474 | } |
||
475 | |||
476 | return $properties; |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Returns one-line short summary describing this controller. |
||
481 | * |
||
482 | * You may override this method to return customized summary. |
||
483 | * The default implementation returns first line from the PHPDoc comment. |
||
484 | * |
||
485 | * @return string |
||
486 | */ |
||
487 | public function getHelpSummary() |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Returns help information for this controller. |
||
494 | * |
||
495 | * You may override this method to return customized help. |
||
496 | * The default implementation returns help information retrieved from the PHPDoc comment. |
||
497 | * @return string |
||
498 | */ |
||
499 | public function getHelp() |
||
500 | { |
||
501 | return $this->parseDocCommentDetail(new \ReflectionClass($this)); |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Returns a one-line short summary describing the specified action. |
||
506 | * @param Action $action action to get summary for |
||
507 | * @return string a one-line short summary describing the specified action. |
||
508 | */ |
||
509 | public function getActionHelpSummary($action) |
||
510 | { |
||
511 | if ($action === null) { |
||
512 | return $this->ansiFormat(Yii::t('yii', 'Action not found.'), Console::FG_RED); |
||
513 | } |
||
514 | |||
515 | return $this->parseDocCommentSummary($this->getActionMethodReflection($action)); |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Returns the detailed help information for the specified action. |
||
520 | * @param Action $action action to get help for |
||
521 | * @return string the detailed help information for the specified action. |
||
522 | */ |
||
523 | public function getActionHelp($action) |
||
524 | { |
||
525 | return $this->parseDocCommentDetail($this->getActionMethodReflection($action)); |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Returns the help information for the anonymous arguments for the action. |
||
530 | * |
||
531 | * The returned value should be an array. The keys are the argument names, and the values are |
||
532 | * the corresponding help information. Each value must be an array of the following structure: |
||
533 | * |
||
534 | * - required: boolean, whether this argument is required. |
||
535 | * - type: string, the PHP type of this argument. |
||
536 | * - default: string, the default value of this argument |
||
537 | * - comment: string, the comment of this argument |
||
538 | * |
||
539 | * The default implementation will return the help information extracted from the doc-comment of |
||
540 | * the parameters corresponding to the action method. |
||
541 | * |
||
542 | * @param Action $action |
||
543 | * @return array the help information of the action arguments |
||
544 | */ |
||
545 | public function getActionArgsHelp($action) |
||
546 | { |
||
547 | $method = $this->getActionMethodReflection($action); |
||
548 | $tags = $this->parseDocCommentTags($method); |
||
549 | $params = isset($tags['param']) ? (array) $tags['param'] : []; |
||
550 | |||
551 | $args = []; |
||
552 | |||
553 | /** @var \ReflectionParameter $reflection */ |
||
554 | foreach ($method->getParameters() as $i => $reflection) { |
||
555 | if ($reflection->getClass() !== null) { |
||
556 | continue; |
||
557 | } |
||
558 | $name = $reflection->getName(); |
||
559 | $tag = isset($params[$i]) ? $params[$i] : ''; |
||
560 | if (preg_match('/^(\S+)\s+(\$\w+\s+)?(.*)/s', $tag, $matches)) { |
||
561 | $type = $matches[1]; |
||
562 | $comment = $matches[3]; |
||
563 | } else { |
||
564 | $type = null; |
||
565 | $comment = $tag; |
||
566 | } |
||
567 | if ($reflection->isDefaultValueAvailable()) { |
||
568 | $args[$name] = [ |
||
569 | 'required' => false, |
||
570 | 'type' => $type, |
||
571 | 'default' => $reflection->getDefaultValue(), |
||
572 | 'comment' => $comment, |
||
573 | ]; |
||
574 | } else { |
||
575 | $args[$name] = [ |
||
576 | 'required' => true, |
||
577 | 'type' => $type, |
||
578 | 'default' => null, |
||
579 | 'comment' => $comment, |
||
580 | ]; |
||
581 | } |
||
582 | } |
||
583 | |||
584 | return $args; |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Returns the help information for the options for the action. |
||
589 | * |
||
590 | * The returned value should be an array. The keys are the option names, and the values are |
||
591 | * the corresponding help information. Each value must be an array of the following structure: |
||
592 | * |
||
593 | * - type: string, the PHP type of this argument. |
||
594 | * - default: string, the default value of this argument |
||
595 | * - comment: string, the comment of this argument |
||
596 | * |
||
597 | * The default implementation will return the help information extracted from the doc-comment of |
||
598 | * the properties corresponding to the action options. |
||
599 | * |
||
600 | * @param Action $action |
||
601 | * @return array the help information of the action options |
||
602 | */ |
||
603 | public function getActionOptionsHelp($action) |
||
604 | { |
||
605 | $optionNames = $this->options($action->id); |
||
606 | if (empty($optionNames)) { |
||
607 | return []; |
||
608 | } |
||
609 | |||
610 | $class = new \ReflectionClass($this); |
||
611 | $options = []; |
||
612 | foreach ($class->getProperties() as $property) { |
||
613 | $name = $property->getName(); |
||
614 | if (!in_array($name, $optionNames, true)) { |
||
615 | continue; |
||
616 | } |
||
617 | $defaultValue = $property->getValue($this); |
||
618 | $tags = $this->parseDocCommentTags($property); |
||
619 | |||
620 | // Display camelCase options in kebab-case |
||
621 | $name = Inflector::camel2id($name, '-', true); |
||
622 | |||
623 | if (isset($tags['var']) || isset($tags['property'])) { |
||
624 | $doc = isset($tags['var']) ? $tags['var'] : $tags['property']; |
||
625 | if (is_array($doc)) { |
||
626 | $doc = reset($doc); |
||
627 | } |
||
628 | if (preg_match('/^(\S+)(.*)/s', $doc, $matches)) { |
||
629 | $type = $matches[1]; |
||
630 | $comment = $matches[2]; |
||
631 | } else { |
||
632 | $type = null; |
||
633 | $comment = $doc; |
||
634 | } |
||
635 | $options[$name] = [ |
||
636 | 'type' => $type, |
||
637 | 'default' => $defaultValue, |
||
638 | 'comment' => $comment, |
||
639 | ]; |
||
640 | } else { |
||
641 | $options[$name] = [ |
||
642 | 'type' => null, |
||
643 | 'default' => $defaultValue, |
||
644 | 'comment' => '', |
||
645 | ]; |
||
646 | } |
||
647 | } |
||
648 | |||
649 | return $options; |
||
650 | } |
||
651 | |||
652 | private $_reflections = []; |
||
653 | |||
654 | /** |
||
655 | * @param Action $action |
||
656 | * @return \ReflectionMethod |
||
657 | */ |
||
658 | protected function getActionMethodReflection($action) |
||
659 | { |
||
660 | if (!isset($this->_reflections[$action->id])) { |
||
661 | if ($action instanceof InlineAction) { |
||
662 | $this->_reflections[$action->id] = new \ReflectionMethod($this, $action->actionMethod); |
||
663 | } else { |
||
664 | $this->_reflections[$action->id] = new \ReflectionMethod($action, 'run'); |
||
665 | } |
||
666 | } |
||
667 | |||
668 | return $this->_reflections[$action->id]; |
||
669 | } |
||
670 | |||
671 | /** |
||
672 | * Parses the comment block into tags. |
||
673 | * @param \Reflector $reflection the comment block |
||
674 | * @return array the parsed tags |
||
675 | */ |
||
676 | protected function parseDocCommentTags($reflection) |
||
677 | { |
||
678 | $comment = $reflection->getDocComment(); |
||
679 | $comment = "@description \n" . strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))), "\r", ''); |
||
680 | $parts = preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY); |
||
681 | $tags = []; |
||
682 | foreach ($parts as $part) { |
||
683 | if (preg_match('/^(\w+)(.*)/ms', trim($part), $matches)) { |
||
684 | $name = $matches[1]; |
||
685 | if (!isset($tags[$name])) { |
||
686 | $tags[$name] = trim($matches[2]); |
||
687 | } elseif (is_array($tags[$name])) { |
||
688 | $tags[$name][] = trim($matches[2]); |
||
689 | } else { |
||
690 | $tags[$name] = [$tags[$name], trim($matches[2])]; |
||
691 | } |
||
692 | } |
||
693 | } |
||
694 | |||
695 | return $tags; |
||
696 | } |
||
697 | |||
698 | /** |
||
699 | * Returns the first line of docblock. |
||
700 | * |
||
701 | * @param \Reflector $reflection |
||
702 | * @return string |
||
703 | */ |
||
704 | protected function parseDocCommentSummary($reflection) |
||
705 | { |
||
706 | $docLines = preg_split('~\R~u', $reflection->getDocComment()); |
||
707 | if (isset($docLines[1])) { |
||
708 | return trim($docLines[1], "\t *"); |
||
709 | } |
||
710 | |||
711 | return ''; |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * Returns full description from the docblock. |
||
716 | * |
||
717 | * @param \Reflector $reflection |
||
718 | * @return string |
||
719 | */ |
||
720 | protected function parseDocCommentDetail($reflection) |
||
731 | } |
||
732 | } |
||
733 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.