Complex classes like Cli 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 Cli, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class Cli |
||
18 | { |
||
19 | /** |
||
20 | * List of paths |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $basePaths = []; |
||
|
|||
25 | |||
26 | /** |
||
27 | * List of console color codes. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $ansiCodes = [ |
||
32 | 'bold' => 1, |
||
33 | 'fg-black' => 30, |
||
34 | 'fg-red' => 31, |
||
35 | 'fg-yellow' => 33, |
||
36 | 'fg-cyan' => 36, |
||
37 | 'fg-white' => 37, |
||
38 | 'bg-red' => 41, |
||
39 | 'bg-green' => 42, |
||
40 | 'bg-yellow' => 43 |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Optional command locations |
||
45 | * |
||
46 | * @var array |
||
47 | 3 | */ |
|
48 | private static $optionalCommandLocations = [ |
||
49 | 3 | 'mongodump' => [], |
|
50 | 1 | 'mysqldump' => [ |
|
51 | '/usr/local/mysql/bin', // Mac OS X |
||
52 | 2 | '/usr/mysql/bin', // Linux |
|
53 | 2 | ], |
|
54 | 'tar' => [], |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Adds a new 'path' to the list of optional command locations. |
||
59 | * |
||
60 | * @param string $command |
||
61 | * @param string $path |
||
62 | 1 | */ |
|
63 | public static function addCommandLocation($command, $path) |
||
67 | |||
68 | /** |
||
69 | * Returns the list of optional 'mysqldump' locations. |
||
70 | * |
||
71 | * @param string $command |
||
72 | * @return array |
||
73 | 125 | */ |
|
74 | public static function getCommandLocations($command) |
||
78 | |||
79 | /** |
||
80 | * Detect a given command's location. |
||
81 | * |
||
82 | * @param string $cmd The command to locate |
||
83 | * @param string $path Directory where the command should be |
||
84 | * @param array $optionalLocations Some fallback locations where to search for the command |
||
85 | 3 | * @return string Absolute path to detected command including command itself |
|
86 | * @throws \RuntimeException |
||
87 | 3 | */ |
|
88 | 1 | public static function detectCmdLocation($cmd, $path = null, $optionalLocations = []) |
|
118 | 8 | ||
119 | /** |
||
120 | * Detect a command in a given path. |
||
121 | 8 | * |
|
122 | * @param string $cmd |
||
123 | * @param string $path |
||
124 | 8 | * @return string |
|
125 | 8 | */ |
|
126 | 8 | public static function detectCmdLocationInPath($cmd, $path) |
|
135 | 1 | ||
136 | 1 | /** |
|
137 | 1 | * Detect command location using which cli command. |
|
138 | 1 | * |
|
139 | * @param string $cmd |
||
140 | 1 | * @return null|string |
|
141 | 1 | */ |
|
142 | public static function detectCmdLocationWithWhich($cmd) |
||
153 | 9 | ||
154 | 9 | /** |
|
155 | 8 | * Check path list for executable command. |
|
156 | * |
||
157 | 1 | * @param string $cmd |
|
158 | 1 | * @param array $paths |
|
159 | * @return null|string |
||
160 | */ |
||
161 | public static function detectCmdLocationInPaths($cmd, array $paths) |
||
172 | |||
173 | /** |
||
174 | 9 | * Return local $PATH variable. |
|
175 | * |
||
176 | * @return string |
||
177 | * @throws \RuntimeException |
||
178 | */ |
||
179 | public static function getEnvPath() |
||
189 | 21 | ||
190 | /** |
||
191 | * Returns the executable command if the command is executable, null otherwise. |
||
192 | 21 | * Search for $command.exe on Windows systems. |
|
193 | 4 | * |
|
194 | * @param string $command |
||
195 | * @return string |
||
196 | */ |
||
197 | public static function isExecutable($command) |
||
211 | |||
212 | /** |
||
213 | 16 | * Is given path absolute. |
|
214 | * |
||
215 | * @param string $path |
||
216 | * @return boolean |
||
217 | */ |
||
218 | public static function isAbsolutePath($path) |
||
244 | 1 | ||
245 | 1 | /** |
|
246 | 1 | * Is given path an absolute windows path. |
|
247 | 1 | * |
|
248 | 1 | * @param string $path |
|
249 | 14 | * @return bool |
|
250 | */ |
||
251 | public static function isAbsoluteWindowsPath($path) |
||
255 | |||
256 | /** |
||
257 | 1 | * Converts a path to an absolute one if necessary relative to a given base path. |
|
258 | * |
||
259 | 1 | * @param string $path |
|
260 | 1 | * @param string $base |
|
261 | 1 | * @param boolean $useIncludePath |
|
262 | * @return string |
||
263 | 1 | */ |
|
264 | 1 | public static function toAbsolutePath($path, $base, $useIncludePath = false) |
|
280 | |||
281 | /** |
||
282 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
||
283 | * |
||
284 | * @author Sebastian Bergmann <[email protected]> |
||
285 | * @param string $color |
||
286 | * @param string $buffer |
||
287 | * @return string |
||
288 | */ |
||
289 | public static function formatWithColor($color, $buffer) |
||
308 | |||
309 | /** |
||
310 | * Fills up a text buffer with '*' to consume 72 chars. |
||
311 | * |
||
312 | * @param string $buffer |
||
313 | * @param int $length |
||
314 | * @return string |
||
315 | */ |
||
316 | public static function formatWithAsterisk($buffer, $length = 75) |
||
320 | |||
321 | /** |
||
322 | * Can command pipe operator be used. |
||
323 | * |
||
324 | * @return bool |
||
325 | */ |
||
326 | public static function canPipe() |
||
330 | |||
331 | /** |
||
332 | * Removes a directory that is not empty. |
||
333 | * |
||
334 | * @param $dir |
||
335 | */ |
||
336 | public static function removeDir($dir) |
||
350 | } |
||
351 |
This check marks private properties in classes that are never used. Those properties can be removed.