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 | protected function dataCanBeFormatted($structuredOutput) |
||
226 | |||
227 | /** |
||
228 | * Run the main command callback |
||
229 | */ |
||
230 | protected function runCommandCallback($commandCallback, CommandData $commandData) |
||
241 | |||
242 | /** |
||
243 | * Determine the formatter that should be used to render |
||
244 | * output. |
||
245 | * |
||
246 | * If the user specified a format via the --format option, |
||
247 | * then always return that. Otherwise, return the default |
||
248 | * format, unless --pipe was specified, in which case |
||
249 | * return the default pipe format, format-pipe. |
||
250 | * |
||
251 | * n.b. --pipe is a handy option introduced in Drush 2 |
||
252 | * (or perhaps even Drush 1) that indicates that the command |
||
253 | * should select the output format that is most appropriate |
||
254 | * for use in scripts (e.g. to pipe to another command). |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function getFormat(FormatterOptions $options) |
||
274 | |||
275 | /** |
||
276 | * Determine whether we should use stdout or stderr. |
||
277 | */ |
||
278 | protected function chooseOutputStream(OutputInterface $output, $status) |
||
287 | |||
288 | /** |
||
289 | * Call the formatter to output the provided data. |
||
290 | */ |
||
291 | protected function writeUsingFormatter(OutputInterface $output, $structuredOutput, CommandData $commandData) |
||
303 | |||
304 | /** |
||
305 | * Create a FormatterOptions object for use in writing the formatted output. |
||
306 | * @param CommandData $commandData |
||
307 | * @return FormatterOptions |
||
308 | */ |
||
309 | protected function createFormatterOptions($commandData) |
||
318 | |||
319 | /** |
||
320 | * Description |
||
321 | * @param OutputInterface $output |
||
322 | * @param int $status |
||
323 | * @param string $structuredOutput |
||
324 | * @param mixed $originalResult |
||
325 | * @return type |
||
326 | */ |
||
327 | protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult) |
||
336 | |||
337 | /** |
||
338 | * If the result object is a string, then print it. |
||
339 | */ |
||
340 | protected function writeCommandOutput( |
||
351 | |||
352 | /** |
||
353 | * If a status code was set, then return it; otherwise, |
||
354 | * presume success. |
||
355 | */ |
||
356 | protected function interpretStatusCode($status) |
||
363 | } |
||
364 |