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($options) |
||
228 | |||
229 | /** |
||
230 | * Determine whether we should use stdout or stderr. |
||
231 | */ |
||
232 | protected function chooseOutputStream(OutputInterface $output, $status) |
||
241 | |||
242 | /** |
||
243 | * Call the formatter to output the provided data. |
||
244 | */ |
||
245 | protected function writeUsingFormatter(OutputInterface $output, $structuredOutput, CommandData $commandData) |
||
257 | |||
258 | /** |
||
259 | * Create a FormatterOptions object for use in writing the formatted output. |
||
260 | * @param CommandData $commandData |
||
261 | * @return FormatterOptions |
||
262 | */ |
||
263 | protected function createFormatterOptions($commandData) |
||
272 | |||
273 | /** |
||
274 | * Description |
||
275 | * @param OutputInterface $output |
||
276 | * @param int $status |
||
277 | * @param string $structuredOutput |
||
278 | * @param mixed $originalResult |
||
279 | * @return type |
||
280 | */ |
||
281 | protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult) |
||
290 | |||
291 | /** |
||
292 | * If the result object is a string, then print it. |
||
293 | */ |
||
294 | protected function writeCommandOutput( |
||
305 | |||
306 | /** |
||
307 | * If a status code was set, then return it; otherwise, |
||
308 | * presume success. |
||
309 | */ |
||
310 | protected function interpretStatusCode($status) |
||
317 | } |
||
318 |