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) |
||
| 92 | |||
| 93 | public function commandErrorForException(\Exception $e) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Return the formatter manager |
||
| 103 | * @return FormatterManager |
||
| 104 | */ |
||
| 105 | public function formatterManager() |
||
| 109 | |||
| 110 | public function initializeHook( |
||
| 118 | |||
| 119 | public function optionsHook( |
||
| 127 | |||
| 128 | public function interact( |
||
| 137 | |||
| 138 | public function process( |
||
| 157 | |||
| 158 | public function validateRunAndAlter( |
||
| 183 | |||
| 184 | public function processResults($names, $result, CommandData $commandData) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Handle the result output and status code calculation. |
||
| 192 | */ |
||
| 193 | public function handleResults(OutputInterface $output, $names, $result, CommandData $commandData) |
||
| 215 | |||
| 216 | protected function dataCanBeFormatted($structuredOutput) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Run the main command callback |
||
| 228 | */ |
||
| 229 | protected function runCommandCallback($commandCallback, CommandData $commandData) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Determine the formatter that should be used to render |
||
| 243 | * output. |
||
| 244 | * |
||
| 245 | * If the user specified a format via the --format option, |
||
| 246 | * then always return that. Otherwise, return the default |
||
| 247 | * format, unless --pipe was specified, in which case |
||
| 248 | * return the default pipe format, format-pipe. |
||
| 249 | * |
||
| 250 | * n.b. --pipe is a handy option introduced in Drush 2 |
||
| 251 | * (or perhaps even Drush 1) that indicates that the command |
||
| 252 | * should select the output format that is most appropriate |
||
| 253 | * for use in scripts (e.g. to pipe to another command). |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function getFormat(FormatterOptions $options) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Determine whether we should use stdout or stderr. |
||
| 276 | */ |
||
| 277 | protected function chooseOutputStream(OutputInterface $output, $status) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Call the formatter to output the provided data. |
||
| 289 | */ |
||
| 290 | protected function writeUsingFormatter(OutputInterface $output, $structuredOutput, CommandData $commandData) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Create a FormatterOptions object for use in writing the formatted output. |
||
| 305 | * @param CommandData $commandData |
||
| 306 | * @return FormatterOptions |
||
| 307 | */ |
||
| 308 | protected function createFormatterOptions($commandData) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Description |
||
| 320 | * @param OutputInterface $output |
||
| 321 | * @param int $status |
||
| 322 | * @param string $structuredOutput |
||
| 323 | * @param mixed $originalResult |
||
| 324 | * @return type |
||
| 325 | */ |
||
| 326 | protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * If the result object is a string, then print it. |
||
| 338 | */ |
||
| 339 | protected function writeCommandOutput( |
||
| 350 | |||
| 351 | /** |
||
| 352 | * If a status code was set, then return it; otherwise, |
||
| 353 | * presume success. |
||
| 354 | */ |
||
| 355 | protected function interpretStatusCode($status) |
||
| 362 | } |
||
| 363 |