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) |
|
147 | |||
148 | /** |
||
149 | * Formats a string with ANSI codes |
||
150 | * |
||
151 | * You may pass additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
152 | * |
||
153 | * Example: |
||
154 | * |
||
155 | * ``` |
||
156 | * echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
157 | * ``` |
||
158 | * |
||
159 | * @param string $string the string to be formatted |
||
160 | * @return string |
||
161 | */ |
||
162 | public function ansiFormat($string) |
||
171 | |||
172 | /** |
||
173 | * Prints a string to STDOUT |
||
174 | * |
||
175 | * You may optionally format the string with ANSI codes by |
||
176 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
177 | * |
||
178 | * Example: |
||
179 | * |
||
180 | * ``` |
||
181 | * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
182 | * ``` |
||
183 | * |
||
184 | * @param string $string the string to print |
||
185 | * @return int|boolean Number of bytes printed or false on error |
||
186 | */ |
||
187 | public function stdout($string) |
||
196 | |||
197 | /** |
||
198 | * Prints a string to STDERR |
||
199 | * |
||
200 | * You may optionally format the string with ANSI codes by |
||
201 | * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
||
202 | * |
||
203 | * Example: |
||
204 | * |
||
205 | * ``` |
||
206 | * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
||
207 | * ``` |
||
208 | * |
||
209 | * @param string $string the string to print |
||
210 | * @return int|boolean Number of bytes printed or false on error |
||
211 | */ |
||
212 | public function stderr($string) |
||
221 | |||
222 | /** |
||
223 | * Prompts the user for input and validates it |
||
224 | * |
||
225 | * @param string $text prompt string |
||
226 | * @param array $options the options to validate the input: |
||
227 | * |
||
228 | * - required: whether it is required or not |
||
229 | * - default: default value if no input is inserted by the user |
||
230 | * - pattern: regular expression pattern to validate user input |
||
231 | * - validator: a callable function to validate input. The function must accept two parameters: |
||
232 | * - $input: the user input to validate |
||
233 | * - $error: the error value passed by reference if validation failed. |
||
234 | * @return string the user input |
||
235 | */ |
||
236 | public function prompt($text, $options = []) |
||
244 | |||
245 | /** |
||
246 | * Asks user to confirm by typing y or n. |
||
247 | * |
||
248 | * @param string $message to echo out before waiting for user input |
||
249 | * @param boolean $default this value is returned if no selection is made. |
||
250 | * @return boolean whether user confirmed. |
||
251 | * Will return true if [[interactive]] is false. |
||
252 | */ |
||
253 | 26 | public function confirm($message, $default = false) |
|
261 | |||
262 | /** |
||
263 | * Gives the user an option to choose from. Giving '?' as an input will show |
||
264 | * a list of options to choose from and their explanations. |
||
265 | * |
||
266 | * @param string $prompt the prompt message |
||
267 | * @param array $options Key-value array of options to choose from |
||
268 | * |
||
269 | * @return string An option character the user chose |
||
270 | */ |
||
271 | public function select($prompt, $options = []) |
||
275 | |||
276 | /** |
||
277 | * Returns the names of valid options for the action (id) |
||
278 | * An option requires the existence of a public member variable whose |
||
279 | * name is the option name. |
||
280 | * Child classes may override this method to specify possible options. |
||
281 | * |
||
282 | * Note that the values setting via options are not available |
||
283 | * until [[beforeAction()]] is being called. |
||
284 | * |
||
285 | * @param string $actionID the action id of the current request |
||
286 | * @return array the names of the options valid for the action |
||
287 | */ |
||
288 | 16 | public function options($actionID) |
|
293 | |||
294 | /** |
||
295 | * Returns properties corresponding to the options for the action id |
||
296 | * Child classes may override this method to specify possible properties. |
||
297 | * |
||
298 | * @param string $actionID the action id of the current request |
||
299 | * @return array properties corresponding to the options for the action |
||
300 | */ |
||
301 | 20 | public function getOptionValues($actionID) |
|
310 | |||
311 | /** |
||
312 | * Returns the names of valid options passed during execution. |
||
313 | * |
||
314 | * @return array the names of the options passed during execution |
||
315 | */ |
||
316 | public function getPassedOptions() |
||
320 | |||
321 | /** |
||
322 | * Returns the properties corresponding to the passed options |
||
323 | * |
||
324 | * @return array the properties corresponding to the passed options |
||
325 | */ |
||
326 | 18 | public function getPassedOptionValues() |
|
334 | |||
335 | /** |
||
336 | * Returns one-line short summary describing this controller. |
||
337 | * |
||
338 | * You may override this method to return customized summary. |
||
339 | * The default implementation returns first line from the PHPDoc comment. |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public function getHelpSummary() |
||
347 | |||
348 | /** |
||
349 | * Returns help information for this controller. |
||
350 | * |
||
351 | * You may override this method to return customized help. |
||
352 | * The default implementation returns help information retrieved from the PHPDoc comment. |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getHelp() |
||
359 | |||
360 | /** |
||
361 | * Returns a one-line short summary describing the specified action. |
||
362 | * @param Action $action action to get summary for |
||
363 | * @return string a one-line short summary describing the specified action. |
||
364 | */ |
||
365 | public function getActionHelpSummary($action) |
||
369 | |||
370 | /** |
||
371 | * Returns the detailed help information for the specified action. |
||
372 | * @param Action $action action to get help for |
||
373 | * @return string the detailed help information for the specified action. |
||
374 | */ |
||
375 | public function getActionHelp($action) |
||
379 | |||
380 | /** |
||
381 | * Returns the help information for the anonymous arguments for the action. |
||
382 | * The returned value should be an array. The keys are the argument names, and the values are |
||
383 | * the corresponding help information. Each value must be an array of the following structure: |
||
384 | * |
||
385 | * - required: boolean, whether this argument is required. |
||
386 | * - type: string, the PHP type of this argument. |
||
387 | * - default: string, the default value of this argument |
||
388 | * - comment: string, the comment of this argument |
||
389 | * |
||
390 | * The default implementation will return the help information extracted from the doc-comment of |
||
391 | * the parameters corresponding to the action method. |
||
392 | * |
||
393 | * @param Action $action |
||
394 | * @return array the help information of the action arguments |
||
395 | */ |
||
396 | public function getActionArgsHelp($action) |
||
433 | |||
434 | /** |
||
435 | * Returns the help information for the options for the action. |
||
436 | * The returned value should be an array. The keys are the option names, and the values are |
||
437 | * the corresponding help information. Each value must be an array of the following structure: |
||
438 | * |
||
439 | * - type: string, the PHP type of this argument. |
||
440 | * - default: string, the default value of this argument |
||
441 | * - comment: string, the comment of this argument |
||
442 | * |
||
443 | * The default implementation will return the help information extracted from the doc-comment of |
||
444 | * the properties corresponding to the action options. |
||
445 | * |
||
446 | * @param Action $action |
||
447 | * @return array the help information of the action options |
||
448 | */ |
||
449 | public function getActionOptionsHelp($action) |
||
492 | |||
493 | private $_reflections = []; |
||
494 | |||
495 | /** |
||
496 | * @param Action $action |
||
497 | * @return \ReflectionMethod |
||
498 | */ |
||
499 | protected function getActionMethodReflection($action) |
||
510 | |||
511 | /** |
||
512 | * Parses the comment block into tags. |
||
513 | * @param \Reflector $reflection the comment block |
||
514 | * @return array the parsed tags |
||
515 | */ |
||
516 | protected function parseDocCommentTags($reflection) |
||
536 | |||
537 | /** |
||
538 | * Returns the first line of docblock. |
||
539 | * |
||
540 | * @param \Reflector $reflection |
||
541 | * @return string |
||
542 | */ |
||
543 | protected function parseDocCommentSummary($reflection) |
||
551 | |||
552 | /** |
||
553 | * Returns full description from the docblock. |
||
554 | * |
||
555 | * @param \Reflector $reflection |
||
556 | * @return string |
||
557 | */ |
||
558 | protected function parseDocCommentDetail($reflection) |
||
569 | } |
||
570 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.