Complex classes like AbstractCommand 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 AbstractCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class AbstractCommand |
||
| 24 | { |
||
| 25 | const CONFIG_FILE = 'phpoole.yml'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Console |
||
| 29 | */ |
||
| 30 | protected $console; |
||
| 31 | /** |
||
| 32 | * @var Route |
||
| 33 | */ |
||
| 34 | protected $route; |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $path; |
||
| 39 | /** |
||
| 40 | * @var PHPoole |
||
| 41 | */ |
||
| 42 | protected $phpoole; |
||
| 43 | /** |
||
| 44 | * @var Filesystem |
||
| 45 | */ |
||
| 46 | protected $fs; |
||
| 47 | /** |
||
| 48 | * @var ProgressBar |
||
| 49 | */ |
||
| 50 | protected $progressBar = null; |
||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $pbMax = 0; |
||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $debug = false; |
||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $quiet = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Start command processing. |
||
| 66 | * |
||
| 67 | * @param Route $route |
||
| 68 | * @param Console $console |
||
| 69 | * |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | public function __invoke(Route $route, Console $console) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Process the command. |
||
| 97 | */ |
||
| 98 | abstract public function processCommand(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return Console |
||
| 102 | */ |
||
| 103 | public function getConsole() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return Route |
||
| 110 | */ |
||
| 111 | public function getRoute() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getPath() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param int $start |
||
| 126 | * @param int $max |
||
| 127 | * |
||
| 128 | * @return ProgressBar |
||
| 129 | */ |
||
| 130 | protected function newPB($start, $max) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return ProgressBar |
||
| 150 | */ |
||
| 151 | protected function getPB() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Print progress bar. |
||
| 158 | */ |
||
| 159 | protected function printPB($itemsCount, $itemsMax, $message) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param array $config |
||
| 171 | * @param array $options |
||
| 172 | * |
||
| 173 | * @return PHPoole |
||
| 174 | */ |
||
| 175 | public function getPHPoole( |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Custom message callback function. |
||
| 209 | */ |
||
| 210 | public function messageCallback() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $text |
||
| 269 | */ |
||
| 270 | public function wl($text) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $text |
||
| 277 | */ |
||
| 278 | public function wlAnnonce($text) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $text |
||
| 285 | */ |
||
| 286 | public function wlDone($text) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $text |
||
| 293 | */ |
||
| 294 | public function wlAlert($text) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $text |
||
| 301 | */ |
||
| 302 | public function wlError($text) |
||
| 306 | } |
||
| 307 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.