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 |
||
| 20 | class CommandProcessor |
||
| 21 | { |
||
| 22 | /** var HookManager */ |
||
| 23 | protected $hookManager; |
||
| 24 | /** var FormatterManager */ |
||
| 25 | protected $formatterManager; |
||
| 26 | /** var callable */ |
||
| 27 | protected $displayErrorFunction; |
||
| 28 | /** var PrepareFormatterOptions[] */ |
||
| 29 | protected $prepareOptionsList = []; |
||
| 30 | |||
| 31 | public function __construct(HookManager $hookManager) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Return the hook manager |
||
| 38 | * @return HookManager |
||
| 39 | */ |
||
| 40 | public function hookManager() |
||
| 44 | |||
| 45 | public function addPrepareFormatter(PrepareFormatter $preparer) |
||
| 49 | |||
| 50 | public function setFormatterManager(FormatterManager $formatterManager) |
||
| 55 | |||
| 56 | public function setDisplayErrorFunction(callable $fn) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Return the formatter manager |
||
| 64 | * @return FormatterManager |
||
| 65 | */ |
||
| 66 | public function formatterManager() |
||
| 70 | |||
| 71 | public function initializeHook( |
||
| 78 | |||
| 79 | public function optionsHook( |
||
| 86 | |||
| 87 | public function interact( |
||
| 95 | |||
| 96 | public function process( |
||
| 115 | |||
| 116 | public function validateRunAndAlter( |
||
| 132 | |||
| 133 | public function processResults($names, $result, CommandData $commandData) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Handle the result output and status code calculation. |
||
| 140 | */ |
||
| 141 | public function handleResults(OutputInterface $output, $names, $result, CommandData $commandData) |
||
| 161 | |||
| 162 | protected function dataCanBeFormatted($structuredOutput) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Run the main command callback |
||
| 174 | */ |
||
| 175 | protected function runCommandCallback($commandCallback, CommandData $commandData) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Determine the formatter that should be used to render |
||
| 189 | * output. |
||
| 190 | * |
||
| 191 | * If the user specified a format via the --format option, |
||
| 192 | * then always return that. Otherwise, return the default |
||
| 193 | * format, unless --pipe was specified, in which case |
||
| 194 | * return the default pipe format, format-pipe. |
||
| 195 | * |
||
| 196 | * n.b. --pipe is a handy option introduced in Drush 2 |
||
| 197 | * (or perhaps even Drush 1) that indicates that the command |
||
| 198 | * should select the output format that is most appropriate |
||
| 199 | * for use in scripts (e.g. to pipe to another command). |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | protected function getFormat(FormatterOptions $options) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Determine whether we should use stdout or stderr. |
||
| 222 | */ |
||
| 223 | protected function chooseOutputStream(OutputInterface $output, $status) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Call the formatter to output the provided data. |
||
| 235 | */ |
||
| 236 | protected function writeUsingFormatter(OutputInterface $output, $structuredOutput, CommandData $commandData) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Create a FormatterOptions object for use in writing the formatted output. |
||
| 251 | * @param CommandData $commandData |
||
| 252 | * @return FormatterOptions |
||
| 253 | */ |
||
| 254 | protected function createFormatterOptions($commandData) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Description |
||
| 266 | * @param OutputInterface $output |
||
| 267 | * @param int $status |
||
| 268 | * @param string $structuredOutput |
||
| 269 | * @param mixed $originalResult |
||
| 270 | * @return type |
||
| 271 | */ |
||
| 272 | protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * If the result object is a string, then print it. |
||
| 284 | */ |
||
| 285 | protected function writeCommandOutput( |
||
| 296 | |||
| 297 | /** |
||
| 298 | * If a status code was set, then return it; otherwise, |
||
| 299 | * presume success. |
||
| 300 | */ |
||
| 301 | protected function interpretStatusCode($status) |
||
| 308 | } |
||
| 309 |