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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 | const EXIT_CODE_NORMAL = 0; |
||
43 | const EXIT_CODE_ERROR = 1; |
||
44 | |||
45 | /** |
||
46 | * @var bool whether to run the command interactively. |
||
47 | */ |
||
48 | public $interactive = true; |
||
49 | /** |
||
50 | * @var bool whether to enable ANSI color in the output. |
||
51 | * If not set, ANSI color will only be enabled for terminals that support it. |
||
52 | */ |
||
53 | public $color; |
||
54 | /** |
||
55 | * @var bool whether to display help information about current command. |
||
56 | * @since 2.0.10 |
||
57 | */ |
||
58 | public $help; |
||
59 | |||
60 | /** |
||
61 | * @var array the options passed during execution. |
||
62 | */ |
||
63 | private $_passedOptions = []; |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Returns a value indicating whether ANSI color is enabled. |
||
68 | * |
||
69 | * ANSI color is enabled only if [[color]] is set true or is not set |
||
70 | * and the terminal supports ANSI color. |
||
71 | * |
||
72 | * @param resource $stream the stream to check. |
||
73 | * @return bool Whether to enable ANSI style in output. |
||
74 | */ |
||
75 | 4 | public function isColorEnabled($stream = \STDOUT) |
|
76 | { |
||
77 | 4 | return $this->color === null ? Console::streamSupportsAnsiColors($stream) : $this->color; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Runs an action with the specified action ID and parameters. |
||
82 | * If the action ID is empty, the method will use [[defaultAction]]. |
||
83 | * @param string $id the ID of the action to be executed. |
||
84 | * @param array $params the parameters (name-value pairs) to be passed to the action. |
||
85 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
86 | * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. |
||
87 | * @throws Exception if there are unknown options or missing arguments |
||
88 | * @see createAction |
||
89 | */ |
||
90 | 57 | public function runAction($id, $params = []) |
|
91 | { |
||
92 | 57 | if (!empty($params)) { |
|
93 | // populate options here so that they are available in beforeAction(). |
||
94 | 48 | $options = $this->options($id === '' ? $this->defaultAction : $id); |
|
95 | 48 | if (isset($params['_aliases'])) { |
|
96 | 1 | $optionAliases = $this->optionAliases(); |
|
97 | 1 | foreach ($params['_aliases'] as $name => $value) { |
|
98 | 1 | if (array_key_exists($name, $optionAliases)) { |
|
99 | 1 | $params[$optionAliases[$name]] = $value; |
|
100 | 1 | } else { |
|
101 | throw new Exception(Yii::t('yii', 'Unknown alias: -{name}', ['name' => $name])); |
||
102 | } |
||
103 | 1 | } |
|
104 | 1 | unset($params['_aliases']); |
|
105 | 1 | } |
|
106 | 48 | foreach ($params as $name => $value) { |
|
107 | 48 | if (in_array($name, $options, true)) { |
|
108 | 5 | $default = $this->$name; |
|
109 | 5 | if (is_array($default)) { |
|
110 | 5 | $this->$name = preg_split('/\s*,\s*(?![^()]*\))/', $value); |
|
111 | 5 | } elseif ($default !== null) { |
|
112 | 2 | settype($value, gettype($default)); |
|
113 | 2 | $this->$name = $value; |
|
114 | 2 | } else { |
|
115 | 1 | $this->$name = $value; |
|
116 | } |
||
117 | 5 | $this->_passedOptions[] = $name; |
|
118 | 5 | unset($params[$name]); |
|
119 | 48 | } elseif (!is_int($name)) { |
|
120 | throw new Exception(Yii::t('yii', 'Unknown option: --{name}', ['name' => $name])); |
||
121 | } |
||
122 | 48 | } |
|
123 | 48 | } |
|
124 | 57 | if ($this->help) { |
|
125 | 2 | $route = $this->getUniqueId() . '/' . $id; |
|
126 | 2 | return Yii::$app->runAction('help', [$route]); |
|
127 | } |
||
128 | 57 | return parent::runAction($id, $params); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Binds the parameters to the action. |
||
133 | * This method is invoked by [[Action]] when it begins to run with the given parameters. |
||
134 | * This method will first bind the parameters with the [[options()|options]] |
||
135 | * available to the action. It then validates the given arguments. |
||
136 | * @param Action $action the action to be bound with parameters |
||
137 | * @param array $params the parameters to be bound to the action |
||
138 | * @return array the valid parameters that the action can run with. |
||
139 | * @throws Exception if there are unknown options or missing arguments |
||
140 | */ |
||
141 | 63 | public function bindActionParams($action, $params) |
|
171 | |||
172 | /** |
||
173 | * Formats a string with ANSI codes |
||
174 | * |
||
175 | * You may pass additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
176 | * |
||
177 | * Example: |
||
178 | * |
||
179 | * ``` |
||
180 | * echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
181 | * ``` |
||
182 | * |
||
183 | * @param string $string the string to be formatted |
||
184 | * @return string |
||
185 | */ |
||
186 | 4 | public function ansiFormat($string) |
|
187 | { |
||
188 | 4 | if ($this->isColorEnabled()) { |
|
189 | 4 | $args = func_get_args(); |
|
190 | 4 | array_shift($args); |
|
191 | 4 | $string = Console::ansiFormat($string, $args); |
|
192 | 4 | } |
|
193 | 4 | return $string; |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Prints a string to STDOUT |
||
198 | * |
||
199 | * You may optionally format the string with ANSI codes by |
||
200 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
201 | * |
||
202 | * Example: |
||
203 | * |
||
204 | * ``` |
||
205 | * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
206 | * ``` |
||
207 | * |
||
208 | * @param string $string the string to print |
||
209 | * @return int|bool Number of bytes printed or false on error |
||
210 | */ |
||
211 | public function stdout($string) |
||
220 | |||
221 | /** |
||
222 | * Prints a string to STDERR |
||
223 | * |
||
224 | * You may optionally format the string with ANSI codes by |
||
225 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
226 | * |
||
227 | * Example: |
||
228 | * |
||
229 | * ``` |
||
230 | * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
231 | * ``` |
||
232 | * |
||
233 | * @param string $string the string to print |
||
234 | * @return int|bool Number of bytes printed or false on error |
||
235 | */ |
||
236 | public function stderr($string) |
||
245 | |||
246 | /** |
||
247 | * Prompts the user for input and validates it |
||
248 | * |
||
249 | * @param string $text prompt string |
||
250 | * @param array $options the options to validate the input: |
||
251 | * |
||
252 | * - required: whether it is required or not |
||
253 | * - default: default value if no input is inserted by the user |
||
254 | * - pattern: regular expression pattern to validate user input |
||
255 | * - validator: a callable function to validate input. The function must accept two parameters: |
||
256 | * - $input: the user input to validate |
||
257 | * - $error: the error value passed by reference if validation failed. |
||
258 | * |
||
259 | * An example of how to use the prompt method with a validator function. |
||
260 | * |
||
261 | * ```php |
||
262 | * $code = $this->prompt('Enter 4-Chars-Pin', ['required' => true, 'validator' => function($input, &$error) { |
||
263 | * if (strlen($input) !== 4) { |
||
264 | * $error = 'The Pin must be exactly 4 chars!'; |
||
265 | * return false; |
||
266 | * } |
||
267 | * return true; |
||
268 | * }); |
||
269 | * ``` |
||
270 | * |
||
271 | * @return string the user input |
||
272 | */ |
||
273 | public function prompt($text, $options = []) |
||
274 | { |
||
275 | if ($this->interactive) { |
||
276 | return Console::prompt($text, $options); |
||
277 | } |
||
278 | |||
279 | return isset($options['default']) ? $options['default'] : ''; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Asks user to confirm by typing y or n. |
||
284 | * |
||
285 | * @param string $message to echo out before waiting for user input |
||
286 | * @param bool $default this value is returned if no selection is made. |
||
287 | * @return bool whether user confirmed. |
||
288 | * Will return true if [[interactive]] is false. |
||
289 | */ |
||
290 | 34 | public function confirm($message, $default = false) |
|
291 | { |
||
292 | 34 | if ($this->interactive) { |
|
293 | return Console::confirm($message, $default); |
||
294 | } |
||
295 | |||
296 | 34 | return true; |
|
297 | } |
||
298 | |||
299 | /** |
||
300 | * Gives the user an option to choose from. Giving '?' as an input will show |
||
301 | * a list of options to choose from and their explanations. |
||
302 | * |
||
303 | * @param string $prompt the prompt message |
||
304 | * @param array $options Key-value array of options to choose from |
||
305 | * |
||
306 | * @return string An option character the user chose |
||
307 | */ |
||
308 | public function select($prompt, $options = []) |
||
312 | |||
313 | /** |
||
314 | * Returns the names of valid options for the action (id) |
||
315 | * An option requires the existence of a public member variable whose |
||
316 | * name is the option name. |
||
317 | * Child classes may override this method to specify possible options. |
||
318 | * |
||
319 | * Note that the values setting via options are not available |
||
320 | * until [[beforeAction()]] is being called. |
||
321 | * |
||
322 | * @param string $actionID the action id of the current request |
||
323 | * @return string[] the names of the options valid for the action |
||
324 | */ |
||
325 | 51 | public function options($actionID) |
|
330 | |||
331 | /** |
||
332 | * Returns option alias names. |
||
333 | * Child classes may override this method to specify alias options. |
||
334 | * |
||
335 | * @return array the options alias names valid for the action |
||
336 | * where the keys is alias name for option and value is option name. |
||
337 | * |
||
338 | * @since 2.0.8 |
||
339 | * @see options() |
||
340 | */ |
||
341 | 2 | public function optionAliases() |
|
342 | { |
||
343 | return [ |
||
344 | 'h' => 'help' |
||
345 | 2 | ]; |
|
346 | } |
||
347 | |||
348 | /** |
||
349 | * Returns properties corresponding to the options for the action id |
||
350 | * Child classes may override this method to specify possible properties. |
||
351 | * |
||
352 | * @param string $actionID the action id of the current request |
||
353 | * @return array properties corresponding to the options for the action |
||
354 | */ |
||
355 | 20 | public function getOptionValues($actionID) |
|
364 | |||
365 | /** |
||
366 | * Returns the names of valid options passed during execution. |
||
367 | * |
||
368 | * @return array the names of the options passed during execution |
||
369 | */ |
||
370 | public function getPassedOptions() |
||
374 | |||
375 | /** |
||
376 | * Returns the properties corresponding to the passed options |
||
377 | * |
||
378 | * @return array the properties corresponding to the passed options |
||
379 | */ |
||
380 | 18 | public function getPassedOptionValues() |
|
388 | |||
389 | /** |
||
390 | * Returns one-line short summary describing this controller. |
||
391 | * |
||
392 | * You may override this method to return customized summary. |
||
393 | * The default implementation returns first line from the PHPDoc comment. |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | 2 | public function getHelpSummary() |
|
398 | { |
||
399 | 2 | return $this->parseDocCommentSummary(new \ReflectionClass($this)); |
|
400 | } |
||
401 | |||
402 | /** |
||
403 | * Returns help information for this controller. |
||
404 | * |
||
405 | * You may override this method to return customized help. |
||
406 | * The default implementation returns help information retrieved from the PHPDoc comment. |
||
407 | * @return string |
||
408 | */ |
||
409 | public function getHelp() |
||
413 | |||
414 | /** |
||
415 | * Returns a one-line short summary describing the specified action. |
||
416 | * @param Action $action action to get summary for |
||
417 | * @return string a one-line short summary describing the specified action. |
||
418 | */ |
||
419 | 1 | public function getActionHelpSummary($action) |
|
423 | |||
424 | /** |
||
425 | * Returns the detailed help information for the specified action. |
||
426 | * @param Action $action action to get help for |
||
427 | * @return string the detailed help information for the specified action. |
||
428 | */ |
||
429 | 2 | public function getActionHelp($action) |
|
433 | |||
434 | /** |
||
435 | * Returns the help information for the anonymous arguments for the action. |
||
436 | * The returned value should be an array. The keys are the argument names, and the values are |
||
437 | * the corresponding help information. Each value must be an array of the following structure: |
||
438 | * |
||
439 | * - required: boolean, whether this argument is required. |
||
440 | * - type: string, the PHP type of this argument. |
||
441 | * - default: string, the default value of this argument |
||
442 | * - comment: string, the comment of this argument |
||
443 | * |
||
444 | * The default implementation will return the help information extracted from the doc-comment of |
||
445 | * the parameters corresponding to the action method. |
||
446 | * |
||
447 | * @param Action $action |
||
448 | * @return array the help information of the action arguments |
||
449 | */ |
||
450 | 4 | public function getActionArgsHelp($action) |
|
451 | { |
||
452 | 4 | $method = $this->getActionMethodReflection($action); |
|
453 | 4 | $tags = $this->parseDocCommentTags($method); |
|
454 | 4 | $params = isset($tags['param']) ? (array) $tags['param'] : []; |
|
455 | |||
456 | 4 | $args = []; |
|
457 | |||
458 | /** @var \ReflectionParameter $reflection */ |
||
459 | 4 | foreach ($method->getParameters() as $i => $reflection) { |
|
460 | 4 | $name = $reflection->getName(); |
|
461 | 4 | $tag = isset($params[$i]) ? $params[$i] : ''; |
|
462 | 4 | if (preg_match('/^(\S+)\s+(\$\w+\s+)?(.*)/s', $tag, $matches)) { |
|
463 | 4 | $type = $matches[1]; |
|
464 | 4 | $comment = $matches[3]; |
|
465 | 4 | } else { |
|
466 | $type = null; |
||
467 | $comment = $tag; |
||
468 | } |
||
469 | 4 | if ($reflection->isDefaultValueAvailable()) { |
|
470 | 2 | $args[$name] = [ |
|
471 | 2 | 'required' => false, |
|
472 | 2 | 'type' => $type, |
|
473 | 2 | 'default' => $reflection->getDefaultValue(), |
|
474 | 2 | 'comment' => $comment, |
|
475 | ]; |
||
476 | 2 | } else { |
|
477 | 2 | $args[$name] = [ |
|
478 | 2 | 'required' => true, |
|
479 | 2 | 'type' => $type, |
|
480 | 2 | 'default' => null, |
|
481 | 2 | 'comment' => $comment, |
|
482 | ]; |
||
483 | } |
||
484 | 4 | } |
|
485 | 4 | return $args; |
|
486 | } |
||
487 | |||
488 | /** |
||
489 | * Returns the help information for the options for the action. |
||
490 | * The returned value should be an array. The keys are the option names, and the values are |
||
491 | * the corresponding help information. Each value must be an array of the following structure: |
||
492 | * |
||
493 | * - type: string, the PHP type of this argument. |
||
494 | * - default: string, the default value of this argument |
||
495 | * - comment: string, the comment of this argument |
||
496 | * |
||
497 | * The default implementation will return the help information extracted from the doc-comment of |
||
498 | * the properties corresponding to the action options. |
||
499 | * |
||
500 | * @param Action $action |
||
501 | * @return array the help information of the action options |
||
502 | */ |
||
503 | 3 | public function getActionOptionsHelp($action) |
|
546 | |||
547 | private $_reflections = []; |
||
548 | |||
549 | /** |
||
550 | * @param Action $action |
||
551 | * @return \ReflectionMethod |
||
552 | */ |
||
553 | 5 | protected function getActionMethodReflection($action) |
|
564 | |||
565 | /** |
||
566 | * Parses the comment block into tags. |
||
567 | * @param \Reflector $reflection the comment block |
||
568 | * @return array the parsed tags |
||
569 | */ |
||
570 | 4 | protected function parseDocCommentTags($reflection) |
|
590 | |||
591 | /** |
||
592 | * Returns the first line of docblock. |
||
593 | * |
||
594 | * @param \Reflector $reflection |
||
595 | * @return string |
||
596 | */ |
||
597 | 2 | protected function parseDocCommentSummary($reflection) |
|
605 | |||
606 | /** |
||
607 | * Returns full description from the docblock. |
||
608 | * |
||
609 | * @param \Reflector $reflection |
||
610 | * @return string |
||
611 | */ |
||
612 | 2 | protected function parseDocCommentDetail($reflection) |
|
623 | } |
||
624 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.