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 |
||
22 | abstract class AbstractCommand |
||
23 | { |
||
24 | const CONFIG_FILE = 'phpoole.yml'; |
||
25 | |||
26 | /** |
||
27 | * @var Console |
||
28 | */ |
||
29 | protected $console; |
||
30 | /** |
||
31 | * @var Route |
||
32 | */ |
||
33 | protected $route; |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $path; |
||
38 | /** |
||
39 | * @var PHPoole |
||
40 | */ |
||
41 | protected $phpoole; |
||
42 | /** |
||
43 | * @var Filesystem |
||
44 | */ |
||
45 | protected $fs; |
||
46 | /** |
||
47 | * @var ProgressBar |
||
48 | */ |
||
49 | protected $progressBar = null; |
||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $pbMax = 0; |
||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $debug = false; |
||
58 | /** |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $quiet = false; |
||
62 | |||
63 | /** |
||
64 | * Start command processing. |
||
65 | * |
||
66 | * @param Route $route |
||
67 | * @param Console $console |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function __invoke(Route $route, Console $console) |
||
86 | |||
87 | /** |
||
88 | * Process the command. |
||
89 | */ |
||
90 | abstract public function processCommand(); |
||
91 | |||
92 | /** |
||
93 | * @return Console |
||
94 | */ |
||
95 | public function getConsole() |
||
99 | |||
100 | /** |
||
101 | * @return Route |
||
102 | */ |
||
103 | public function getRoute() |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getPath() |
||
115 | |||
116 | /** |
||
117 | * @param int $start |
||
118 | * @param int $max |
||
119 | * |
||
120 | * @return ProgressBar |
||
121 | */ |
||
122 | protected function newPB($start, $max) |
||
139 | |||
140 | /** |
||
141 | * @return ProgressBar |
||
142 | */ |
||
143 | protected function getPB() |
||
147 | |||
148 | /** |
||
149 | * @param array $options |
||
150 | * |
||
151 | * @return PHPoole |
||
152 | */ |
||
153 | public function getPHPoole(array $options = []) |
||
237 | |||
238 | /** |
||
239 | * @param string $text |
||
240 | */ |
||
241 | public function wl($text) |
||
245 | |||
246 | /** |
||
247 | * @param string $text |
||
248 | */ |
||
249 | public function wlAnnonce($text) |
||
253 | |||
254 | /** |
||
255 | * @param string $text |
||
256 | */ |
||
257 | public function wlDone($text) |
||
261 | |||
262 | /** |
||
263 | * @param string $text |
||
264 | */ |
||
265 | public function wlAlert($text) |
||
269 | |||
270 | /** |
||
271 | * @param string $text |
||
272 | */ |
||
273 | public function wlError($text) |
||
277 | } |
||
278 |
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
@return
doc comment to communicate to implementors of these methods what they are expected to return.