Total Complexity | 108 |
Total Lines | 717 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
40 | class Controller extends \yii\base\Controller |
||
41 | { |
||
42 | /** |
||
43 | * @deprecated since 2.0.13. Use [[ExitCode::OK]] instead. |
||
44 | */ |
||
45 | const EXIT_CODE_NORMAL = 0; |
||
46 | /** |
||
47 | * @deprecated since 2.0.13. Use [[ExitCode::UNSPECIFIED_ERROR]] instead. |
||
48 | */ |
||
49 | const EXIT_CODE_ERROR = 1; |
||
50 | |||
51 | /** |
||
52 | * @var bool whether to run the command interactively. |
||
53 | */ |
||
54 | public $interactive = true; |
||
55 | /** |
||
56 | * @var bool|null whether to enable ANSI color in the output. |
||
57 | * If not set, ANSI color will only be enabled for terminals that support it. |
||
58 | */ |
||
59 | public $color; |
||
60 | /** |
||
61 | * @var bool whether to display help information about current command. |
||
62 | * @since 2.0.10 |
||
63 | */ |
||
64 | public $help = false; |
||
65 | /** |
||
66 | * @var bool|null if true - script finish with `ExitCode::OK` in case of exception. |
||
67 | * false - `ExitCode::UNSPECIFIED_ERROR`. |
||
68 | * Default: `YII_ENV_TEST` |
||
69 | * @since 2.0.36 |
||
70 | */ |
||
71 | public $silentExitOnException; |
||
72 | |||
73 | /** |
||
74 | * @var array the options passed during execution. |
||
75 | */ |
||
76 | private $_passedOptions = []; |
||
77 | |||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function 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) |
||
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) && is_string($value)) { |
||
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 (PHP_VERSION_ID >= 80000) { |
||
217 | $isArray = ($type = $param->getType()) instanceof \ReflectionNamedType && $type->getName() === 'array'; |
||
218 | } else { |
||
219 | $isArray = $param->isArray(); |
||
220 | } |
||
221 | if ($isArray) { |
||
222 | $params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]); |
||
223 | } |
||
224 | $args[] = $actionParams[$key] = $params[$key]; |
||
225 | unset($params[$key]); |
||
226 | } elseif ( |
||
227 | PHP_VERSION_ID >= 70100 |
||
228 | && ($type = $param->getType()) !== null |
||
229 | && $type instanceof \ReflectionNamedType |
||
230 | && !$type->isBuiltin() |
||
231 | ) { |
||
232 | try { |
||
233 | $this->bindInjectedParams($type, $name, $args, $requestedParams); |
||
234 | } catch (\yii\base\Exception $e) { |
||
235 | throw new Exception($e->getMessage()); |
||
236 | } |
||
237 | } elseif ($param->isDefaultValueAvailable()) { |
||
238 | $args[] = $actionParams[$i] = $param->getDefaultValue(); |
||
239 | } else { |
||
240 | $missing[] = $name; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | if (!empty($missing)) { |
||
245 | throw new Exception(Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', $missing)])); |
||
246 | } |
||
247 | |||
248 | // We use a different array here, specifically one that doesn't contain service instances but descriptions instead. |
||
249 | if (\Yii::$app->requestedParams === null) { |
||
250 | \Yii::$app->requestedParams = array_merge($actionParams, $requestedParams); |
||
251 | } |
||
252 | |||
253 | return array_merge($args, $params); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Formats a string with ANSI codes. |
||
258 | * |
||
259 | * You may pass additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
260 | * |
||
261 | * Example: |
||
262 | * |
||
263 | * ``` |
||
264 | * echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
265 | * ``` |
||
266 | * |
||
267 | * @param string $string the string to be formatted |
||
268 | * @return string |
||
269 | */ |
||
270 | public function ansiFormat($string) |
||
271 | { |
||
272 | if ($this->isColorEnabled()) { |
||
273 | $args = func_get_args(); |
||
274 | array_shift($args); |
||
275 | $string = Console::ansiFormat($string, $args); |
||
276 | } |
||
277 | |||
278 | return $string; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Prints a string to STDOUT. |
||
283 | * |
||
284 | * You may optionally format the string with ANSI codes by |
||
285 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
286 | * |
||
287 | * Example: |
||
288 | * |
||
289 | * ``` |
||
290 | * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
291 | * ``` |
||
292 | * |
||
293 | * @param string $string the string to print |
||
294 | * @param int ...$args additional parameters to decorate the output |
||
295 | * @return int|bool Number of bytes printed or false on error |
||
296 | */ |
||
297 | public function stdout($string) |
||
298 | { |
||
299 | if ($this->isColorEnabled()) { |
||
300 | $args = func_get_args(); |
||
301 | array_shift($args); |
||
302 | $string = Console::ansiFormat($string, $args); |
||
303 | } |
||
304 | |||
305 | return Console::stdout($string); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Prints a string to STDERR. |
||
310 | * |
||
311 | * You may optionally format the string with ANSI codes by |
||
312 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
313 | * |
||
314 | * Example: |
||
315 | * |
||
316 | * ``` |
||
317 | * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
318 | * ``` |
||
319 | * |
||
320 | * @param string $string the string to print |
||
321 | * @param int ...$args additional parameters to decorate the output |
||
322 | * @return int|bool Number of bytes printed or false on error |
||
323 | */ |
||
324 | public function stderr($string) |
||
325 | { |
||
326 | if ($this->isColorEnabled(\STDERR)) { |
||
327 | $args = func_get_args(); |
||
328 | array_shift($args); |
||
329 | $string = Console::ansiFormat($string, $args); |
||
330 | } |
||
331 | |||
332 | return fwrite(\STDERR, $string); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Prompts the user for input and validates it. |
||
337 | * |
||
338 | * @param string $text prompt string |
||
339 | * @param array $options the options to validate the input: |
||
340 | * |
||
341 | * - required: whether it is required or not |
||
342 | * - default: default value if no input is inserted by the user |
||
343 | * - pattern: regular expression pattern to validate user input |
||
344 | * - validator: a callable function to validate input. The function must accept two parameters: |
||
345 | * - $input: the user input to validate |
||
346 | * - $error: the error value passed by reference if validation failed. |
||
347 | * |
||
348 | * An example of how to use the prompt method with a validator function. |
||
349 | * |
||
350 | * ```php |
||
351 | * $code = $this->prompt('Enter 4-Chars-Pin', ['required' => true, 'validator' => function($input, &$error) { |
||
352 | * if (strlen($input) !== 4) { |
||
353 | * $error = 'The Pin must be exactly 4 chars!'; |
||
354 | * return false; |
||
355 | * } |
||
356 | * return true; |
||
357 | * }]); |
||
358 | * ``` |
||
359 | * |
||
360 | * @return string the user input |
||
361 | */ |
||
362 | public function prompt($text, $options = []) |
||
363 | { |
||
364 | if ($this->interactive) { |
||
365 | return Console::prompt($text, $options); |
||
366 | } |
||
367 | |||
368 | return isset($options['default']) ? $options['default'] : ''; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Asks user to confirm by typing y or n. |
||
373 | * |
||
374 | * A typical usage looks like the following: |
||
375 | * |
||
376 | * ```php |
||
377 | * if ($this->confirm("Are you sure?")) { |
||
378 | * echo "user typed yes\n"; |
||
379 | * } else { |
||
380 | * echo "user typed no\n"; |
||
381 | * } |
||
382 | * ``` |
||
383 | * |
||
384 | * @param string $message to echo out before waiting for user input |
||
385 | * @param bool $default this value is returned if no selection is made. |
||
386 | * @return bool whether user confirmed. |
||
387 | * Will return true if [[interactive]] is false. |
||
388 | */ |
||
389 | public function confirm($message, $default = false) |
||
390 | { |
||
391 | if ($this->interactive) { |
||
392 | return Console::confirm($message, $default); |
||
393 | } |
||
394 | |||
395 | return true; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Gives the user an option to choose from. Giving '?' as an input will show |
||
400 | * a list of options to choose from and their explanations. |
||
401 | * |
||
402 | * @param string $prompt the prompt message |
||
403 | * @param array $options Key-value array of options to choose from |
||
404 | * |
||
405 | * @return string An option character the user chose |
||
406 | */ |
||
407 | public function select($prompt, $options = []) |
||
408 | { |
||
409 | return Console::select($prompt, $options); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Returns the names of valid options for the action (id) |
||
414 | * An option requires the existence of a public member variable whose |
||
415 | * name is the option name. |
||
416 | * Child classes may override this method to specify possible options. |
||
417 | * |
||
418 | * Note that the values setting via options are not available |
||
419 | * until [[beforeAction()]] is being called. |
||
420 | * |
||
421 | * @param string $actionID the action id of the current request |
||
422 | * @return string[] the names of the options valid for the action |
||
423 | */ |
||
424 | public function options($actionID) |
||
|
|||
425 | { |
||
426 | // $actionId might be used in subclasses to provide options specific to action id |
||
427 | return ['color', 'interactive', 'help', 'silentExitOnException']; |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * Returns option alias names. |
||
432 | * Child classes may override this method to specify alias options. |
||
433 | * |
||
434 | * @return array the options alias names valid for the action |
||
435 | * where the keys is alias name for option and value is option name. |
||
436 | * |
||
437 | * @since 2.0.8 |
||
438 | * @see options() |
||
439 | */ |
||
440 | public function optionAliases() |
||
441 | { |
||
442 | return [ |
||
443 | 'h' => 'help', |
||
444 | ]; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Returns properties corresponding to the options for the action id |
||
449 | * Child classes may override this method to specify possible properties. |
||
450 | * |
||
451 | * @param string $actionID the action id of the current request |
||
452 | * @return array properties corresponding to the options for the action |
||
453 | */ |
||
454 | public function getOptionValues($actionID) |
||
455 | { |
||
456 | // $actionId might be used in subclasses to provide properties specific to action id |
||
457 | $properties = []; |
||
458 | foreach ($this->options($this->action->id) as $property) { |
||
459 | $properties[$property] = $this->$property; |
||
460 | } |
||
461 | |||
462 | return $properties; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Returns the names of valid options passed during execution. |
||
467 | * |
||
468 | * @return array the names of the options passed during execution |
||
469 | */ |
||
470 | public function getPassedOptions() |
||
471 | { |
||
472 | return $this->_passedOptions; |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Returns the properties corresponding to the passed options. |
||
477 | * |
||
478 | * @return array the properties corresponding to the passed options |
||
479 | */ |
||
480 | public function getPassedOptionValues() |
||
481 | { |
||
482 | $properties = []; |
||
483 | foreach ($this->_passedOptions as $property) { |
||
484 | $properties[$property] = $this->$property; |
||
485 | } |
||
486 | |||
487 | return $properties; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Returns one-line short summary describing this controller. |
||
492 | * |
||
493 | * You may override this method to return customized summary. |
||
494 | * The default implementation returns first line from the PHPDoc comment. |
||
495 | * |
||
496 | * @return string |
||
497 | */ |
||
498 | public function getHelpSummary() |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Returns help information for this controller. |
||
505 | * |
||
506 | * You may override this method to return customized help. |
||
507 | * The default implementation returns help information retrieved from the PHPDoc comment. |
||
508 | * @return string |
||
509 | */ |
||
510 | public function getHelp() |
||
511 | { |
||
512 | return $this->parseDocCommentDetail(new \ReflectionClass($this)); |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Returns a one-line short summary describing the specified action. |
||
517 | * @param Action $action action to get summary for |
||
518 | * @return string a one-line short summary describing the specified action. |
||
519 | */ |
||
520 | public function getActionHelpSummary($action) |
||
521 | { |
||
522 | if ($action === null) { |
||
523 | return $this->ansiFormat(Yii::t('yii', 'Action not found.'), Console::FG_RED); |
||
524 | } |
||
525 | |||
526 | return $this->parseDocCommentSummary($this->getActionMethodReflection($action)); |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * Returns the detailed help information for the specified action. |
||
531 | * @param Action $action action to get help for |
||
532 | * @return string the detailed help information for the specified action. |
||
533 | */ |
||
534 | public function getActionHelp($action) |
||
535 | { |
||
536 | return $this->parseDocCommentDetail($this->getActionMethodReflection($action)); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Returns the help information for the anonymous arguments for the action. |
||
541 | * |
||
542 | * The returned value should be an array. The keys are the argument names, and the values are |
||
543 | * the corresponding help information. Each value must be an array of the following structure: |
||
544 | * |
||
545 | * - required: bool, whether this argument is required |
||
546 | * - type: string|null, the PHP type(s) of this argument |
||
547 | * - default: mixed, the default value of this argument |
||
548 | * - comment: string, the description of this argument |
||
549 | * |
||
550 | * The default implementation will return the help information extracted from the Reflection or |
||
551 | * DocBlock of the parameters corresponding to the action method. |
||
552 | * |
||
553 | * @param Action $action the action instance |
||
554 | * @return array the help information of the action arguments |
||
555 | */ |
||
556 | public function getActionArgsHelp($action) |
||
557 | { |
||
558 | $method = $this->getActionMethodReflection($action); |
||
559 | |||
560 | $tags = $this->parseDocCommentTags($method); |
||
561 | $tags['param'] = isset($tags['param']) ? (array) $tags['param'] : []; |
||
562 | $phpDocParams = []; |
||
563 | foreach ($tags['param'] as $i => $tag) { |
||
564 | if (preg_match('/^(?<type>\S+)(\s+\$(?<name>\w+))?(?<comment>.*)/us', $tag, $matches) === 1) { |
||
565 | $key = empty($matches['name']) ? $i : $matches['name']; |
||
566 | $phpDocParams[$key] = ['type' => $matches['type'], 'comment' => $matches['comment']]; |
||
567 | } |
||
568 | } |
||
569 | unset($tags); |
||
570 | |||
571 | $args = []; |
||
572 | |||
573 | /** @var \ReflectionParameter $parameter */ |
||
574 | foreach ($method->getParameters() as $i => $parameter) { |
||
575 | $type = null; |
||
576 | $comment = ''; |
||
577 | if (PHP_MAJOR_VERSION > 5 && $parameter->hasType()) { |
||
578 | $reflectionType = $parameter->getType(); |
||
579 | if (PHP_VERSION_ID >= 70100) { |
||
580 | $types = method_exists($reflectionType, 'getTypes') ? $reflectionType->getTypes() : [$reflectionType]; |
||
581 | foreach ($types as $key => $reflectionType) { |
||
582 | $types[$key] = $reflectionType->getName(); |
||
583 | } |
||
584 | $type = implode('|', $types); |
||
585 | } else { |
||
586 | $type = (string) $reflectionType; |
||
587 | } |
||
588 | } |
||
589 | // find PhpDoc tag by property name or position |
||
590 | $key = isset($phpDocParams[$parameter->name]) ? $parameter->name : (isset($phpDocParams[$i]) ? $i : null); |
||
591 | if ($key !== null) { |
||
592 | $comment = $phpDocParams[$key]['comment']; |
||
593 | if ($type === null && !empty($phpDocParams[$key]['type'])) { |
||
594 | $type = $phpDocParams[$key]['type']; |
||
595 | } |
||
596 | } |
||
597 | // if type still not detected, then using type of default value |
||
598 | if ($type === null && $parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() !== null) { |
||
599 | $type = gettype($parameter->getDefaultValue()); |
||
600 | } |
||
601 | |||
602 | $args[$parameter->name] = [ |
||
603 | 'required' => !$parameter->isOptional(), |
||
604 | 'type' => $type, |
||
605 | 'default' => $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null, |
||
606 | 'comment' => $comment, |
||
607 | ]; |
||
608 | } |
||
609 | |||
610 | return $args; |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Returns the help information for the options for the action. |
||
615 | * |
||
616 | * The returned value should be an array. The keys are the option names, and the values are |
||
617 | * the corresponding help information. Each value must be an array of the following structure: |
||
618 | * |
||
619 | * - type: string, the PHP type of this argument. |
||
620 | * - default: string, the default value of this argument |
||
621 | * - comment: string, the comment of this argument |
||
622 | * |
||
623 | * The default implementation will return the help information extracted from the doc-comment of |
||
624 | * the properties corresponding to the action options. |
||
625 | * |
||
626 | * @param Action $action |
||
627 | * @return array the help information of the action options |
||
628 | */ |
||
629 | public function getActionOptionsHelp($action) |
||
630 | { |
||
631 | $optionNames = $this->options($action->id); |
||
632 | if (empty($optionNames)) { |
||
633 | return []; |
||
634 | } |
||
635 | |||
636 | $class = new \ReflectionClass($this); |
||
637 | $options = []; |
||
638 | foreach ($class->getProperties() as $property) { |
||
639 | $name = $property->getName(); |
||
640 | if (!in_array($name, $optionNames, true)) { |
||
641 | continue; |
||
642 | } |
||
643 | $defaultValue = $property->getValue($this); |
||
644 | $tags = $this->parseDocCommentTags($property); |
||
645 | |||
646 | // Display camelCase options in kebab-case |
||
647 | $name = Inflector::camel2id($name, '-', true); |
||
648 | |||
649 | if (isset($tags['var']) || isset($tags['property'])) { |
||
650 | $doc = isset($tags['var']) ? $tags['var'] : $tags['property']; |
||
651 | if (is_array($doc)) { |
||
652 | $doc = reset($doc); |
||
653 | } |
||
654 | if (preg_match('/^(\S+)(.*)/s', $doc, $matches)) { |
||
655 | $type = $matches[1]; |
||
656 | $comment = $matches[2]; |
||
657 | } else { |
||
658 | $type = null; |
||
659 | $comment = $doc; |
||
660 | } |
||
661 | $options[$name] = [ |
||
662 | 'type' => $type, |
||
663 | 'default' => $defaultValue, |
||
664 | 'comment' => $comment, |
||
665 | ]; |
||
666 | } else { |
||
667 | $options[$name] = [ |
||
668 | 'type' => null, |
||
669 | 'default' => $defaultValue, |
||
670 | 'comment' => '', |
||
671 | ]; |
||
672 | } |
||
673 | } |
||
674 | |||
675 | return $options; |
||
676 | } |
||
677 | |||
678 | private $_reflections = []; |
||
679 | |||
680 | /** |
||
681 | * @param Action $action |
||
682 | * @return \ReflectionMethod |
||
683 | */ |
||
684 | protected function getActionMethodReflection($action) |
||
685 | { |
||
686 | if (!isset($this->_reflections[$action->id])) { |
||
687 | if ($action instanceof InlineAction) { |
||
688 | $this->_reflections[$action->id] = new \ReflectionMethod($this, $action->actionMethod); |
||
689 | } else { |
||
690 | $this->_reflections[$action->id] = new \ReflectionMethod($action, 'run'); |
||
691 | } |
||
692 | } |
||
693 | |||
694 | return $this->_reflections[$action->id]; |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * Parses the comment block into tags. |
||
699 | * @param \Reflector $reflection the comment block |
||
700 | * @return array the parsed tags |
||
701 | */ |
||
702 | protected function parseDocCommentTags($reflection) |
||
703 | { |
||
704 | $comment = $reflection->getDocComment(); |
||
705 | $comment = "@description \n" . strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))), "\r", ''); |
||
706 | $parts = preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY); |
||
707 | $tags = []; |
||
708 | foreach ($parts as $part) { |
||
709 | if (preg_match('/^(\w+)(.*)/ms', trim($part), $matches)) { |
||
710 | $name = $matches[1]; |
||
711 | if (!isset($tags[$name])) { |
||
712 | $tags[$name] = trim($matches[2]); |
||
713 | } elseif (is_array($tags[$name])) { |
||
714 | $tags[$name][] = trim($matches[2]); |
||
715 | } else { |
||
716 | $tags[$name] = [$tags[$name], trim($matches[2])]; |
||
717 | } |
||
718 | } |
||
719 | } |
||
720 | |||
721 | return $tags; |
||
722 | } |
||
723 | |||
724 | /** |
||
725 | * Returns the first line of docblock. |
||
726 | * |
||
727 | * @param \Reflector $reflection |
||
728 | * @return string |
||
729 | */ |
||
730 | protected function parseDocCommentSummary($reflection) |
||
731 | { |
||
732 | $docLines = preg_split('~\R~u', $reflection->getDocComment()); |
||
733 | if (isset($docLines[1])) { |
||
734 | return trim($docLines[1], "\t *"); |
||
735 | } |
||
736 | |||
737 | return ''; |
||
738 | } |
||
739 | |||
740 | /** |
||
741 | * Returns full description from the docblock. |
||
742 | * |
||
743 | * @param \Reflector $reflection |
||
744 | * @return string |
||
745 | */ |
||
746 | protected function parseDocCommentDetail($reflection) |
||
757 | } |
||
758 | } |
||
759 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.