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 |
||
36 | class Controller extends \yii\base\Controller |
||
37 | { |
||
38 | const EXIT_CODE_NORMAL = 0; |
||
39 | const EXIT_CODE_ERROR = 1; |
||
40 | |||
41 | /** |
||
42 | * @var boolean whether to run the command interactively. |
||
43 | */ |
||
44 | public $interactive = true; |
||
45 | /** |
||
46 | * @var boolean whether to enable ANSI color in the output. |
||
47 | * If not set, ANSI color will only be enabled for terminals that support it. |
||
48 | */ |
||
49 | public $color; |
||
50 | |||
51 | /** |
||
52 | * @var array the options passed during execution. |
||
53 | */ |
||
54 | private $_passedOptions = []; |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Returns a value indicating whether ANSI color is enabled. |
||
59 | * |
||
60 | * ANSI color is enabled only if [[color]] is set true or is not set |
||
61 | * and the terminal supports ANSI color. |
||
62 | * |
||
63 | * @param resource $stream the stream to check. |
||
64 | * @return boolean Whether to enable ANSI style in output. |
||
65 | */ |
||
66 | public function isColorEnabled($stream = \STDOUT) |
||
70 | |||
71 | /** |
||
72 | * Runs an action with the specified action ID and parameters. |
||
73 | * If the action ID is empty, the method will use [[defaultAction]]. |
||
74 | * @param string $id the ID of the action to be executed. |
||
75 | * @param array $params the parameters (name-value pairs) to be passed to the action. |
||
76 | * @return integer the status of the action execution. 0 means normal, other values mean abnormal. |
||
77 | * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. |
||
78 | * @throws Exception if there are unknown options or missing arguments |
||
79 | * @see createAction |
||
80 | */ |
||
81 | 42 | public function runAction($id, $params = []) |
|
106 | |||
107 | /** |
||
108 | * Binds the parameters to the action. |
||
109 | * This method is invoked by [[Action]] when it begins to run with the given parameters. |
||
110 | * This method will first bind the parameters with the [[options()|options]] |
||
111 | * available to the action. It then validates the given arguments. |
||
112 | * @param Action $action the action to be bound with parameters |
||
113 | * @param array $params the parameters to be bound to the action |
||
114 | * @return array the valid parameters that the action can run with. |
||
115 | * @throws Exception if there are unknown options or missing arguments |
||
116 | */ |
||
117 | 42 | public function bindActionParams($action, $params) |
|
161 | |||
162 | /** |
||
163 | * Formats a string with ANSI codes |
||
164 | * |
||
165 | * You may pass additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
166 | * |
||
167 | * Example: |
||
168 | * |
||
169 | * ``` |
||
170 | * echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
171 | * ``` |
||
172 | * |
||
173 | * @param string $string the string to be formatted |
||
174 | * @return string |
||
175 | */ |
||
176 | public function ansiFormat($string) |
||
185 | |||
186 | /** |
||
187 | * Prints a string to STDOUT |
||
188 | * |
||
189 | * You may optionally format the string with ANSI codes by |
||
190 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
191 | * |
||
192 | * Example: |
||
193 | * |
||
194 | * ``` |
||
195 | * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
196 | * ``` |
||
197 | * |
||
198 | * @param string $string the string to print |
||
199 | * @return int|boolean Number of bytes printed or false on error |
||
200 | */ |
||
201 | public function stdout($string) |
||
210 | |||
211 | /** |
||
212 | * Prints a string to STDERR |
||
213 | * |
||
214 | * You may optionally format the string with ANSI codes by |
||
215 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
216 | * |
||
217 | * Example: |
||
218 | * |
||
219 | * ``` |
||
220 | * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
221 | * ``` |
||
222 | * |
||
223 | * @param string $string the string to print |
||
224 | * @return int|boolean Number of bytes printed or false on error |
||
225 | */ |
||
226 | public function stderr($string) |
||
235 | |||
236 | /** |
||
237 | * Prompts the user for input and validates it |
||
238 | * |
||
239 | * @param string $text prompt string |
||
240 | * @param array $options the options to validate the input: |
||
241 | * |
||
242 | * - required: whether it is required or not |
||
243 | * - default: default value if no input is inserted by the user |
||
244 | * - pattern: regular expression pattern to validate user input |
||
245 | * - validator: a callable function to validate input. The function must accept two parameters: |
||
246 | * - $input: the user input to validate |
||
247 | * - $error: the error value passed by reference if validation failed. |
||
248 | * @return string the user input |
||
249 | */ |
||
250 | public function prompt($text, $options = []) |
||
258 | |||
259 | /** |
||
260 | * Asks user to confirm by typing y or n. |
||
261 | * |
||
262 | * @param string $message to echo out before waiting for user input |
||
263 | * @param boolean $default this value is returned if no selection is made. |
||
264 | * @return boolean whether user confirmed. |
||
265 | * Will return true if [[interactive]] is false. |
||
266 | */ |
||
267 | 26 | public function confirm($message, $default = false) |
|
275 | |||
276 | /** |
||
277 | * Gives the user an option to choose from. Giving '?' as an input will show |
||
278 | * a list of options to choose from and their explanations. |
||
279 | * |
||
280 | * @param string $prompt the prompt message |
||
281 | * @param array $options Key-value array of options to choose from |
||
282 | * |
||
283 | * @return string An option character the user chose |
||
284 | */ |
||
285 | public function select($prompt, $options = []) |
||
289 | |||
290 | /** |
||
291 | * Returns the names of valid options for the action (id) |
||
292 | * An option requires the existence of a public member variable whose |
||
293 | * name is the option name. |
||
294 | * Child classes may override this method to specify possible options. |
||
295 | * |
||
296 | * Note that the values setting via options are not available |
||
297 | * until [[beforeAction()]] is being called. |
||
298 | * |
||
299 | * @param string $actionID the action id of the current request |
||
300 | * @return array the names of the options valid for the action |
||
301 | */ |
||
302 | 16 | public function options($actionID) |
|
307 | |||
308 | /** |
||
309 | * Returns properties corresponding to the options for the action id |
||
310 | * Child classes may override this method to specify possible properties. |
||
311 | * |
||
312 | * @param string $actionID the action id of the current request |
||
313 | * @return array properties corresponding to the options for the action |
||
314 | */ |
||
315 | 20 | public function getOptionValues($actionID) |
|
324 | |||
325 | /** |
||
326 | * Returns the names of valid options passed during execution. |
||
327 | * |
||
328 | * @return array the names of the options passed during execution |
||
329 | */ |
||
330 | public function getPassedOptions() |
||
334 | |||
335 | /** |
||
336 | * Returns the properties corresponding to the passed options |
||
337 | * |
||
338 | * @return array the properties corresponding to the passed options |
||
339 | */ |
||
340 | 18 | public function getPassedOptionValues() |
|
348 | |||
349 | /** |
||
350 | * Returns one-line short summary describing this controller. |
||
351 | * |
||
352 | * You may override this method to return customized summary. |
||
353 | * The default implementation returns first line from the PHPDoc comment. |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | public function getHelpSummary() |
||
361 | |||
362 | /** |
||
363 | * Returns help information for this controller. |
||
364 | * |
||
365 | * You may override this method to return customized help. |
||
366 | * The default implementation returns help information retrieved from the PHPDoc comment. |
||
367 | * @return string |
||
368 | */ |
||
369 | public function getHelp() |
||
373 | |||
374 | /** |
||
375 | * Returns a one-line short summary describing the specified action. |
||
376 | * @param Action $action action to get summary for |
||
377 | * @return string a one-line short summary describing the specified action. |
||
378 | */ |
||
379 | public function getActionHelpSummary($action) |
||
383 | |||
384 | /** |
||
385 | * Returns the detailed help information for the specified action. |
||
386 | * @param Action $action action to get help for |
||
387 | * @return string the detailed help information for the specified action. |
||
388 | */ |
||
389 | public function getActionHelp($action) |
||
393 | |||
394 | /** |
||
395 | * Returns the help information for the anonymous arguments for the action. |
||
396 | * The returned value should be an array. The keys are the argument names, and the values are |
||
397 | * the corresponding help information. Each value must be an array of the following structure: |
||
398 | * |
||
399 | * - required: boolean, whether this argument is required. |
||
400 | * - type: string, the PHP type of this argument. |
||
401 | * - default: string, the default value of this argument |
||
402 | * - comment: string, the comment of this argument |
||
403 | * |
||
404 | * The default implementation will return the help information extracted from the doc-comment of |
||
405 | * the parameters corresponding to the action method. |
||
406 | * |
||
407 | * @param Action $action |
||
408 | * @return array the help information of the action arguments |
||
409 | */ |
||
410 | 1 | public function getActionArgsHelp($action) |
|
448 | |||
449 | /** |
||
450 | * Returns the help information for the options for the action. |
||
451 | * The returned value should be an array. The keys are the option names, and the values are |
||
452 | * the corresponding help information. Each value must be an array of the following structure: |
||
453 | * |
||
454 | * - type: string, the PHP type of this argument. |
||
455 | * - default: string, the default value of this argument |
||
456 | * - comment: string, the comment of this argument |
||
457 | * |
||
458 | * The default implementation will return the help information extracted from the doc-comment of |
||
459 | * the properties corresponding to the action options. |
||
460 | * |
||
461 | * @param Action $action |
||
462 | * @return array the help information of the action options |
||
463 | */ |
||
464 | public function getActionOptionsHelp($action) |
||
507 | |||
508 | private $_reflections = []; |
||
509 | |||
510 | /** |
||
511 | * @param Action $action |
||
512 | * @return \ReflectionMethod |
||
513 | */ |
||
514 | 1 | protected function getActionMethodReflection($action) |
|
525 | |||
526 | /** |
||
527 | * Parses the comment block into tags. |
||
528 | * @param \Reflector $reflection the comment block |
||
529 | * @return array the parsed tags |
||
530 | */ |
||
531 | 1 | protected function parseDocCommentTags($reflection) |
|
551 | |||
552 | /** |
||
553 | * Returns the first line of docblock. |
||
554 | * |
||
555 | * @param \Reflector $reflection |
||
556 | * @return string |
||
557 | */ |
||
558 | protected function parseDocCommentSummary($reflection) |
||
566 | |||
567 | /** |
||
568 | * Returns full description from the docblock. |
||
569 | * |
||
570 | * @param \Reflector $reflection |
||
571 | * @return string |
||
572 | */ |
||
573 | protected function parseDocCommentDetail($reflection) |
||
584 | } |
||
585 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.