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 | * @param array $options |
||
158 | * |
||
159 | * @return PHPoole |
||
160 | */ |
||
161 | public function getPHPoole(array $options = []) |
||
250 | |||
251 | /** |
||
252 | * @param string $text |
||
253 | */ |
||
254 | public function wl($text) |
||
258 | |||
259 | /** |
||
260 | * @param string $text |
||
261 | */ |
||
262 | public function wlAnnonce($text) |
||
266 | |||
267 | /** |
||
268 | * @param string $text |
||
269 | */ |
||
270 | public function wlDone($text) |
||
274 | |||
275 | /** |
||
276 | * @param string $text |
||
277 | */ |
||
278 | public function wlAlert($text) |
||
282 | |||
283 | /** |
||
284 | * @param string $text |
||
285 | */ |
||
286 | public function wlError($text) |
||
290 | } |
||
291 |
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.