Complex classes like CommandProcessor 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 CommandProcessor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class CommandProcessor implements LoggerAwareInterface |
||
31 | { |
||
32 | use LoggerAwareTrait; |
||
33 | |||
34 | /** var HookManager */ |
||
35 | protected $hookManager; |
||
36 | /** var FormatterManager */ |
||
37 | protected $formatterManager; |
||
38 | /** var callable */ |
||
39 | protected $displayErrorFunction; |
||
40 | /** var PrepareFormatterOptions[] */ |
||
41 | protected $prepareOptionsList = []; |
||
42 | /** var boolean */ |
||
43 | protected $passExceptions; |
||
44 | |||
45 | public function __construct(HookManager $hookManager) |
||
49 | |||
50 | /** |
||
51 | * Return the hook manager |
||
52 | * @return HookManager |
||
53 | */ |
||
54 | public function hookManager() |
||
58 | |||
59 | public function addPrepareFormatter(PrepareFormatter $preparer) |
||
63 | |||
64 | public function setFormatterManager(FormatterManager $formatterManager) |
||
69 | |||
70 | public function setDisplayErrorFunction(callable $fn) |
||
75 | |||
76 | /** |
||
77 | * Set a mode to make the annotated command library re-throw |
||
78 | * any exception that it catches while processing a command. |
||
79 | * |
||
80 | * The default behavior in the current (2.x) branch is to catch |
||
81 | * the exception and replace it with a CommandError object that |
||
82 | * may be processed by the normal output processing passthrough. |
||
83 | * |
||
84 | * In the 3.x branch, exceptions will never be caught; they will |
||
85 | * be passed through, as if setPassExceptions(true) were called. |
||
86 | * This is the recommended behavior. |
||
87 | */ |
||
88 | public function setPassExceptions($passExceptions) |
||
93 | |||
94 | public function commandErrorForException(\Exception $e) |
||
101 | |||
102 | /** |
||
103 | * Return the formatter manager |
||
104 | * @return FormatterManager |
||
105 | */ |
||
106 | public function formatterManager() |
||
110 | |||
111 | public function initializeHook( |
||
119 | |||
120 | public function optionsHook( |
||
128 | |||
129 | public function interact( |
||
138 | |||
139 | public function process( |
||
158 | |||
159 | public function validateRunAndAlter( |
||
184 | |||
185 | public function processResults($names, $result, CommandData $commandData) |
||
190 | |||
191 | /** |
||
192 | * Handle the result output and status code calculation. |
||
193 | */ |
||
194 | public function handleResults(OutputInterface $output, $names, $result, CommandData $commandData) |
||
216 | |||
217 | /** |
||
218 | * Run the main command callback |
||
219 | */ |
||
220 | protected function runCommandCallback($commandCallback, CommandData $commandData) |
||
231 | |||
232 | /** |
||
233 | * Determine the formatter that should be used to render |
||
234 | * output. |
||
235 | * |
||
236 | * If the user specified a format via the --format option, |
||
237 | * then always return that. Otherwise, return the default |
||
238 | * format, unless --pipe was specified, in which case |
||
239 | * return the default pipe format, format-pipe. |
||
240 | * |
||
241 | * n.b. --pipe is a handy option introduced in Drush 2 |
||
242 | * (or perhaps even Drush 1) that indicates that the command |
||
243 | * should select the output format that is most appropriate |
||
244 | * for use in scripts (e.g. to pipe to another command). |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | protected function getFormat(FormatterOptions $options) |
||
264 | |||
265 | /** |
||
266 | * Determine whether we should use stdout or stderr. |
||
267 | */ |
||
268 | protected function chooseOutputStream(OutputInterface $output, $status) |
||
277 | |||
278 | /** |
||
279 | * Call the formatter to output the provided data. |
||
280 | */ |
||
281 | protected function writeUsingFormatter(OutputInterface $output, $structuredOutput, CommandData $commandData) |
||
293 | |||
294 | /** |
||
295 | * Create a FormatterOptions object for use in writing the formatted output. |
||
296 | * @param CommandData $commandData |
||
297 | * @return FormatterOptions |
||
298 | */ |
||
299 | protected function createFormatterOptions($commandData) |
||
308 | |||
309 | /** |
||
310 | * Description |
||
311 | * @param OutputInterface $output |
||
312 | * @param int $status |
||
313 | * @param string $structuredOutput |
||
314 | * @param mixed $originalResult |
||
315 | * @return type |
||
316 | */ |
||
317 | protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult) |
||
326 | |||
327 | /** |
||
328 | * If the result object is a string, then print it. |
||
329 | */ |
||
330 | protected function writeCommandOutput( |
||
341 | |||
342 | /** |
||
343 | * If a status code was set, then return it; otherwise, |
||
344 | * presume success. |
||
345 | */ |
||
346 | protected function interpretStatusCode($status) |
||
353 | } |
||
354 |