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) |
||
227 | |||
228 | protected function dataCanBeFormatted($structuredOutput) |
||
237 | |||
238 | /** |
||
239 | * Run the main command callback |
||
240 | */ |
||
241 | protected function runCommandCallback($commandCallback, CommandData $commandData) |
||
255 | |||
256 | public function injectIntoCommandData($commandData, $injectedClasses) |
||
263 | |||
264 | protected function getInstanceToInject(CommandData $commandData, $injectedClass) |
||
275 | |||
276 | /** |
||
277 | * Determine the formatter that should be used to render |
||
278 | * output. |
||
279 | * |
||
280 | * If the user specified a format via the --format option, |
||
281 | * then always return that. Otherwise, return the default |
||
282 | * format, unless --pipe was specified, in which case |
||
283 | * return the default pipe format, format-pipe. |
||
284 | * |
||
285 | * n.b. --pipe is a handy option introduced in Drush 2 |
||
286 | * (or perhaps even Drush 1) that indicates that the command |
||
287 | * should select the output format that is most appropriate |
||
288 | * for use in scripts (e.g. to pipe to another command). |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | protected function getFormat(FormatterOptions $options) |
||
308 | |||
309 | /** |
||
310 | * Determine whether we should use stdout or stderr. |
||
311 | */ |
||
312 | protected function chooseOutputStream(OutputInterface $output, $status) |
||
321 | |||
322 | /** |
||
323 | * Call the formatter to output the provided data. |
||
324 | */ |
||
325 | protected function writeUsingFormatter(OutputInterface $output, $structuredOutput, CommandData $commandData, $status = 0) |
||
337 | |||
338 | /** |
||
339 | * Create a FormatterOptions object for use in writing the formatted output. |
||
340 | * @param CommandData $commandData |
||
341 | * @return FormatterOptions |
||
342 | */ |
||
343 | protected function createFormatterOptions($commandData) |
||
352 | |||
353 | /** |
||
354 | * Description |
||
355 | * @param OutputInterface $output |
||
356 | * @param int $status |
||
357 | * @param string $structuredOutput |
||
358 | * @param mixed $originalResult |
||
359 | * @return type |
||
360 | */ |
||
361 | protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult) |
||
370 | |||
371 | /** |
||
372 | * If the result object is a string, then print it. |
||
373 | */ |
||
374 | protected function writeCommandOutput( |
||
386 | |||
387 | /** |
||
388 | * If a status code was set, then return it; otherwise, |
||
389 | * presume success. |
||
390 | */ |
||
391 | protected function interpretStatusCode($status) |
||
398 | } |
||
399 |