Complex classes like HelpController 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 HelpController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class HelpController extends Controller |
||
39 | { |
||
40 | /** |
||
41 | * Displays available commands or the detailed information |
||
42 | * about a particular command. |
||
43 | * |
||
44 | * @param string $command The name of the command to show help about. |
||
45 | * If not provided, all available commands will be displayed. |
||
46 | * @return int the exit status |
||
47 | * @throws Exception if the command for help is unknown |
||
48 | */ |
||
49 | public function actionIndex($command = null) |
||
70 | |||
71 | /** |
||
72 | * Returns all available command names. |
||
73 | * @return array all available command names |
||
74 | */ |
||
75 | public function getCommands() |
||
81 | |||
82 | /** |
||
83 | * Returns an array of commands an their descriptions. |
||
84 | * @return array all available commands as keys and their description as values. |
||
85 | */ |
||
86 | protected function getCommandDescriptions() |
||
87 | { |
||
88 | $descriptions = []; |
||
89 | foreach ($this->getCommands() as $command) { |
||
90 | $description = ''; |
||
91 | |||
92 | $result = Yii::$app->createController($command); |
||
93 | if ($result !== false && $result[0] instanceof Controller) { |
||
94 | list($controller, $actionID) = $result; |
||
|
|||
95 | /** @var Controller $controller */ |
||
96 | $description = $controller->getHelpSummary(); |
||
97 | } |
||
98 | |||
99 | $descriptions[$command] = $description; |
||
100 | } |
||
101 | |||
102 | return $descriptions; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Returns all available actions of the specified controller. |
||
107 | * @param Controller $controller the controller instance |
||
108 | * @return array all available action IDs. |
||
109 | */ |
||
110 | public function getActions($controller) |
||
124 | |||
125 | /** |
||
126 | * Returns available commands of a specified module. |
||
127 | * @param \yii\base\Module $module the module instance |
||
128 | * @return array the available command names |
||
129 | */ |
||
130 | protected function getModuleCommands($module) |
||
163 | |||
164 | /** |
||
165 | * Validates if the given class is a valid console controller class. |
||
166 | * @param string $controllerClass |
||
167 | * @return bool |
||
168 | */ |
||
169 | protected function validateControllerClass($controllerClass) |
||
178 | |||
179 | /** |
||
180 | * Displays all available commands. |
||
181 | */ |
||
182 | protected function getDefaultHelp() |
||
183 | { |
||
184 | $commands = $this->getCommandDescriptions(); |
||
185 | $this->stdout($this->getDefaultHelpHeader()); |
||
186 | if (!empty($commands)) { |
||
187 | $this->stdout("\nThe following commands are available:\n\n", Console::BOLD); |
||
188 | $len = 0; |
||
189 | foreach ($commands as $command => $description) { |
||
190 | $result = Yii::$app->createController($command); |
||
191 | if ($result !== false && $result[0] instanceof Controller) { |
||
192 | /** @var $controller Controller */ |
||
193 | list($controller, $actionID) = $result; |
||
194 | $actions = $this->getActions($controller); |
||
195 | if (!empty($actions)) { |
||
196 | $prefix = $controller->getUniqueId(); |
||
197 | foreach ($actions as $action) { |
||
198 | $string = $prefix . '/' . $action; |
||
199 | if ($action === $controller->defaultAction) { |
||
200 | $string .= ' (default)'; |
||
201 | } |
||
202 | if (($l = strlen($string)) > $len) { |
||
203 | $len = $l; |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | } elseif (($l = strlen($command)) > $len) { |
||
208 | $len = $l; |
||
209 | } |
||
210 | } |
||
211 | foreach ($commands as $command => $description) { |
||
212 | $this->stdout('- ' . $this->ansiFormat($command, Console::FG_YELLOW)); |
||
213 | $this->stdout(str_repeat(' ', $len + 4 - strlen($command))); |
||
214 | $this->stdout(Console::wrapText($description, $len + 4 + 2), Console::BOLD); |
||
215 | $this->stdout("\n"); |
||
216 | |||
217 | $result = Yii::$app->createController($command); |
||
218 | if ($result !== false && $result[0] instanceof Controller) { |
||
219 | list($controller, $actionID) = $result; |
||
220 | $actions = $this->getActions($controller); |
||
221 | if (!empty($actions)) { |
||
222 | $prefix = $controller->getUniqueId(); |
||
223 | foreach ($actions as $action) { |
||
224 | $string = ' ' . $prefix . '/' . $action; |
||
225 | $this->stdout(' ' . $this->ansiFormat($string, Console::FG_GREEN)); |
||
226 | if ($action === $controller->defaultAction) { |
||
227 | $string .= ' (default)'; |
||
228 | $this->stdout(' (default)', Console::FG_YELLOW); |
||
229 | } |
||
230 | $summary = $controller->getActionHelpSummary($controller->createAction($action)); |
||
231 | if ($summary !== '') { |
||
232 | $this->stdout(str_repeat(' ', $len + 4 - strlen($string))); |
||
233 | $this->stdout(Console::wrapText($summary, $len + 4 + 2)); |
||
234 | } |
||
235 | $this->stdout("\n"); |
||
236 | } |
||
237 | } |
||
238 | $this->stdout("\n"); |
||
239 | } |
||
240 | } |
||
241 | $scriptName = $this->getScriptName(); |
||
242 | $this->stdout("\nTo see the help of each command, enter:\n", Console::BOLD); |
||
243 | $this->stdout("\n $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' ' |
||
244 | . $this->ansiFormat('<command-name>', Console::FG_CYAN) . "\n\n"); |
||
245 | } else { |
||
246 | $this->stdout("\nNo commands are found.\n\n", Console::BOLD); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Displays the overall information of the command. |
||
252 | * @param Controller $controller the controller instance |
||
253 | */ |
||
254 | protected function getCommandHelp($controller) |
||
295 | |||
296 | /** |
||
297 | * Displays the detailed information of a command action. |
||
298 | * @param Controller $controller the controller instance |
||
299 | * @param string $actionID action ID |
||
300 | * @throws Exception if the action does not exist |
||
301 | */ |
||
302 | protected function getSubCommandHelp($controller, $actionID) |
||
369 | |||
370 | /** |
||
371 | * Generates a well-formed string for an argument or option. |
||
372 | * @param string $name the name of the argument or option |
||
373 | * @param bool $required whether the argument is required |
||
374 | * @param string $type the type of the option or argument |
||
375 | * @param mixed $defaultValue the default value of the option or argument |
||
376 | * @param string $comment comment about the option or argument |
||
377 | * @return string the formatted string for the argument or option |
||
378 | */ |
||
379 | protected function formatOptionHelp($name, $required, $type, $defaultValue, $comment) |
||
415 | |||
416 | /** |
||
417 | * @param Controller $controller the controller instance |
||
418 | * @param string $option the option name |
||
419 | * @return string the formatted string for the alias argument or option |
||
420 | * @since 2.0.8 |
||
421 | */ |
||
422 | protected function formatOptionAliases($controller, $option) |
||
432 | |||
433 | /** |
||
434 | * @return string the name of the cli script currently running. |
||
435 | */ |
||
436 | protected function getScriptName() |
||
440 | |||
441 | /** |
||
442 | * Return a default help header. |
||
443 | * @return string default help header. |
||
444 | * @since 2.0.11 |
||
445 | */ |
||
446 | protected function getDefaultHelpHeader() |
||
450 | } |
||
451 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.