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 | /** |
||
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 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; |
||
65 | |||
66 | /** |
||
67 | * @var array the options passed during execution. |
||
68 | */ |
||
69 | private $_passedOptions = []; |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Returns a value indicating whether ANSI color is enabled. |
||
74 | * |
||
75 | * ANSI color is enabled only if [[color]] is set true or is not set |
||
76 | * and the terminal supports ANSI color. |
||
77 | * |
||
78 | * @param resource $stream the stream to check. |
||
79 | * @return bool Whether to enable ANSI style in output. |
||
80 | */ |
||
81 | 4 | public function isColorEnabled($stream = \STDOUT) |
|
85 | |||
86 | /** |
||
87 | * Runs an action with the specified action ID and parameters. |
||
88 | * If the action ID is empty, the method will use [[defaultAction]]. |
||
89 | * @param string $id the ID of the action to be executed. |
||
90 | * @param array $params the parameters (name-value pairs) to be passed to the action. |
||
91 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
92 | * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. |
||
93 | * @throws Exception if there are unknown options or missing arguments |
||
94 | * @see createAction |
||
95 | */ |
||
96 | 89 | public function runAction($id, $params = []) |
|
137 | |||
138 | /** |
||
139 | * Binds the parameters to the action. |
||
140 | * This method is invoked by [[Action]] when it begins to run with the given parameters. |
||
141 | * This method will first bind the parameters with the [[options()|options]] |
||
142 | * available to the action. It then validates the given arguments. |
||
143 | * @param Action $action the action to be bound with parameters |
||
144 | * @param array $params the parameters to be bound to the action |
||
145 | * @return array the valid parameters that the action can run with. |
||
146 | * @throws Exception if there are unknown options or missing arguments |
||
147 | */ |
||
148 | 96 | public function bindActionParams($action, $params) |
|
178 | |||
179 | /** |
||
180 | * Formats a string with ANSI codes. |
||
181 | * |
||
182 | * You may pass additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
183 | * |
||
184 | * Example: |
||
185 | * |
||
186 | * ``` |
||
187 | * echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
188 | * ``` |
||
189 | * |
||
190 | * @param string $string the string to be formatted |
||
191 | * @return string |
||
192 | */ |
||
193 | 4 | public function ansiFormat($string) |
|
203 | |||
204 | /** |
||
205 | * Prints a string to STDOUT. |
||
206 | * |
||
207 | * You may optionally format the string with ANSI codes by |
||
208 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
209 | * |
||
210 | * Example: |
||
211 | * |
||
212 | * ``` |
||
213 | * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
214 | * ``` |
||
215 | * |
||
216 | * @param string $string the string to print |
||
217 | * @return int|bool Number of bytes printed or false on error |
||
218 | */ |
||
219 | public function stdout($string) |
||
229 | |||
230 | /** |
||
231 | * Prints a string to STDERR. |
||
232 | * |
||
233 | * You may optionally format the string with ANSI codes by |
||
234 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
235 | * |
||
236 | * Example: |
||
237 | * |
||
238 | * ``` |
||
239 | * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
240 | * ``` |
||
241 | * |
||
242 | * @param string $string the string to print |
||
243 | * @return int|bool Number of bytes printed or false on error |
||
244 | */ |
||
245 | public function stderr($string) |
||
255 | |||
256 | /** |
||
257 | * Prompts the user for input and validates it. |
||
258 | * |
||
259 | * @param string $text prompt string |
||
260 | * @param array $options the options to validate the input: |
||
261 | * |
||
262 | * - required: whether it is required or not |
||
263 | * - default: default value if no input is inserted by the user |
||
264 | * - pattern: regular expression pattern to validate user input |
||
265 | * - validator: a callable function to validate input. The function must accept two parameters: |
||
266 | * - $input: the user input to validate |
||
267 | * - $error: the error value passed by reference if validation failed. |
||
268 | * |
||
269 | * An example of how to use the prompt method with a validator function. |
||
270 | * |
||
271 | * ```php |
||
272 | * $code = $this->prompt('Enter 4-Chars-Pin', ['required' => true, 'validator' => function($input, &$error) { |
||
273 | * if (strlen($input) !== 4) { |
||
274 | * $error = 'The Pin must be exactly 4 chars!'; |
||
275 | * return false; |
||
276 | * } |
||
277 | * return true; |
||
278 | * }]); |
||
279 | * ``` |
||
280 | * |
||
281 | * @return string the user input |
||
282 | */ |
||
283 | public function prompt($text, $options = []) |
||
291 | |||
292 | /** |
||
293 | * Asks user to confirm by typing y or n. |
||
294 | * |
||
295 | * A typical usage looks like the following: |
||
296 | * |
||
297 | * ```php |
||
298 | * if ($this->confirm("Are you sure?")) { |
||
299 | * echo "user typed yes\n"; |
||
300 | * } else { |
||
301 | * echo "user typed no\n"; |
||
302 | * } |
||
303 | * ``` |
||
304 | * |
||
305 | * @param string $message to echo out before waiting for user input |
||
306 | * @param bool $default this value is returned if no selection is made. |
||
307 | * @return bool whether user confirmed. |
||
308 | * Will return true if [[interactive]] is false. |
||
309 | */ |
||
310 | 46 | public function confirm($message, $default = false) |
|
318 | |||
319 | /** |
||
320 | * Gives the user an option to choose from. Giving '?' as an input will show |
||
321 | * a list of options to choose from and their explanations. |
||
322 | * |
||
323 | * @param string $prompt the prompt message |
||
324 | * @param array $options Key-value array of options to choose from |
||
325 | * |
||
326 | * @return string An option character the user chose |
||
327 | */ |
||
328 | public function select($prompt, $options = []) |
||
332 | |||
333 | /** |
||
334 | * Returns the names of valid options for the action (id) |
||
335 | * An option requires the existence of a public member variable whose |
||
336 | * name is the option name. |
||
337 | * Child classes may override this method to specify possible options. |
||
338 | * |
||
339 | * Note that the values setting via options are not available |
||
340 | * until [[beforeAction()]] is being called. |
||
341 | * |
||
342 | * @param string $actionID the action id of the current request |
||
343 | * @return string[] the names of the options valid for the action |
||
344 | */ |
||
345 | 82 | public function options($actionID) |
|
350 | |||
351 | /** |
||
352 | * Returns option alias names. |
||
353 | * Child classes may override this method to specify alias options. |
||
354 | * |
||
355 | * @return array the options alias names valid for the action |
||
356 | * where the keys is alias name for option and value is option name. |
||
357 | * |
||
358 | * @since 2.0.8 |
||
359 | * @see options() |
||
360 | */ |
||
361 | 2 | public function optionAliases() |
|
367 | |||
368 | /** |
||
369 | * Returns properties corresponding to the options for the action id |
||
370 | * Child classes may override this method to specify possible properties. |
||
371 | * |
||
372 | * @param string $actionID the action id of the current request |
||
373 | * @return array properties corresponding to the options for the action |
||
374 | */ |
||
375 | 42 | public function getOptionValues($actionID) |
|
385 | |||
386 | /** |
||
387 | * Returns the names of valid options passed during execution. |
||
388 | * |
||
389 | * @return array the names of the options passed during execution |
||
390 | */ |
||
391 | public function getPassedOptions() |
||
395 | |||
396 | /** |
||
397 | * Returns the properties corresponding to the passed options. |
||
398 | * |
||
399 | * @return array the properties corresponding to the passed options |
||
400 | */ |
||
401 | 39 | public function getPassedOptionValues() |
|
410 | |||
411 | /** |
||
412 | * Returns one-line short summary describing this controller. |
||
413 | * |
||
414 | * You may override this method to return customized summary. |
||
415 | * The default implementation returns first line from the PHPDoc comment. |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | 3 | public function getHelpSummary() |
|
423 | |||
424 | /** |
||
425 | * Returns help information for this controller. |
||
426 | * |
||
427 | * You may override this method to return customized help. |
||
428 | * The default implementation returns help information retrieved from the PHPDoc comment. |
||
429 | * @return string |
||
430 | */ |
||
431 | public function getHelp() |
||
435 | |||
436 | /** |
||
437 | * Returns a one-line short summary describing the specified action. |
||
438 | * @param Action $action action to get summary for |
||
439 | * @return string a one-line short summary describing the specified action. |
||
440 | */ |
||
441 | 1 | public function getActionHelpSummary($action) |
|
445 | |||
446 | /** |
||
447 | * Returns the detailed help information for the specified action. |
||
448 | * @param Action $action action to get help for |
||
449 | * @return string the detailed help information for the specified action. |
||
450 | */ |
||
451 | 2 | public function getActionHelp($action) |
|
455 | |||
456 | /** |
||
457 | * Returns the help information for the anonymous arguments for the action. |
||
458 | * |
||
459 | * The returned value should be an array. The keys are the argument names, and the values are |
||
460 | * the corresponding help information. Each value must be an array of the following structure: |
||
461 | * |
||
462 | * - required: boolean, whether this argument is required. |
||
463 | * - type: string, the PHP type of this argument. |
||
464 | * - default: string, the default value of this argument |
||
465 | * - comment: string, the comment of this argument |
||
466 | * |
||
467 | * The default implementation will return the help information extracted from the doc-comment of |
||
468 | * the parameters corresponding to the action method. |
||
469 | * |
||
470 | * @param Action $action |
||
471 | * @return array the help information of the action arguments |
||
472 | */ |
||
473 | 5 | public function getActionArgsHelp($action) |
|
514 | |||
515 | /** |
||
516 | * Returns the help information for the options for the action. |
||
517 | * |
||
518 | * The returned value should be an array. The keys are the option names, and the values are |
||
519 | * the corresponding help information. Each value must be an array of the following structure: |
||
520 | * |
||
521 | * - type: string, the PHP type of this argument. |
||
522 | * - default: string, the default value of this argument |
||
523 | * - comment: string, the comment of this argument |
||
524 | * |
||
525 | * The default implementation will return the help information extracted from the doc-comment of |
||
526 | * the properties corresponding to the action options. |
||
527 | * |
||
528 | * @param Action $action |
||
529 | * @return array the help information of the action options |
||
530 | */ |
||
531 | 3 | public function getActionOptionsHelp($action) |
|
575 | |||
576 | private $_reflections = []; |
||
577 | |||
578 | /** |
||
579 | * @param Action $action |
||
580 | * @return \ReflectionMethod |
||
581 | */ |
||
582 | 6 | protected function getActionMethodReflection($action) |
|
594 | |||
595 | /** |
||
596 | * Parses the comment block into tags. |
||
597 | * @param \Reflector $reflection the comment block |
||
598 | * @return array the parsed tags |
||
599 | */ |
||
600 | 5 | protected function parseDocCommentTags($reflection) |
|
621 | |||
622 | /** |
||
623 | * Returns the first line of docblock. |
||
624 | * |
||
625 | * @param \Reflector $reflection |
||
626 | * @return string |
||
627 | */ |
||
628 | 3 | protected function parseDocCommentSummary($reflection) |
|
629 | { |
||
630 | 3 | $docLines = preg_split('~\R~u', $reflection->getDocComment()); |
|
631 | 3 | if (isset($docLines[1])) { |
|
632 | 3 | return trim($docLines[1], "\t *"); |
|
633 | } |
||
634 | |||
635 | 1 | return ''; |
|
636 | } |
||
637 | |||
638 | /** |
||
639 | * Returns full description from the docblock. |
||
640 | * |
||
641 | * @param \Reflector $reflection |
||
642 | * @return string |
||
643 | */ |
||
644 | 2 | protected function parseDocCommentDetail($reflection) |
|
656 | } |
||
657 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.