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/mysqldump', // Mac OS X |
||
52 | 2 | '/usr/mysql/bin/mysqldump', // Linux |
|
53 | 2 | ], |
|
54 | 'tar' => [], |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Register a base path. |
||
59 | * |
||
60 | * @param string $name |
||
61 | * @param string $path |
||
62 | 1 | * @throws \RuntimeException |
|
63 | */ |
||
64 | 1 | public static function registerBase($name, $path) |
|
65 | 1 | { |
|
66 | if (!self::isAbsolutePath($path)) { |
||
67 | throw new RuntimeException(sprintf('path has to be absolute: %s', $path)); |
||
68 | } |
||
69 | self::$basePaths[$name] = $path; |
||
70 | } |
||
71 | |||
72 | |||
73 | 125 | /** |
|
74 | * Adds a new 'path' to the list of optional command locations. |
||
75 | 125 | * |
|
76 | * @param string $command |
||
77 | * @param string $path |
||
78 | */ |
||
79 | public static function addCommandLocation($command, $path) |
||
83 | |||
84 | /** |
||
85 | 3 | * Returns the list of optional 'mysqldump' locations. |
|
86 | * |
||
87 | 3 | * @param string $command |
|
88 | 1 | * @return array |
|
89 | */ |
||
90 | 2 | public static function getCommandLocations($command) |
|
94 | |||
95 | /** |
||
96 | * Retrieve a registered path. |
||
97 | * |
||
98 | * @param string $name |
||
99 | * @return string array |
||
100 | * @throws \RuntimeException |
||
101 | */ |
||
102 | 128 | public static function getBase($name) |
|
109 | 1 | ||
110 | /** |
||
111 | 119 | * Detect a given command's location. |
|
112 | * |
||
113 | * @param string $cmd The command to locate |
||
114 | * @param string $path Directory where the command should be |
||
115 | 8 | * @param array $optionalLocations Some fallback locations where to search for the command |
|
116 | 8 | * @return string Absolute path to detected command including command itself |
|
117 | 8 | * @throws \RuntimeException |
|
118 | 8 | */ |
|
119 | public static function detectCmdLocation($cmd, $path = null, $optionalLocations = []) |
||
149 | |||
150 | 9 | /** |
|
151 | * Detect a command in a given path. |
||
152 | * |
||
153 | 9 | * @param string $cmd |
|
154 | 9 | * @param string $path |
|
155 | 8 | * @return string |
|
156 | */ |
||
157 | 1 | public static function detectCmdLocationInPath($cmd, $path) |
|
166 | |||
167 | /** |
||
168 | 128 | * Detect command location using which cli command. |
|
169 | * |
||
170 | 128 | * @param string $cmd |
|
171 | 126 | * @return null|string |
|
172 | */ |
||
173 | public static function detectCmdLocationWithWhich($cmd) |
||
184 | |||
185 | /** |
||
186 | * Check path list for executable command. |
||
187 | * |
||
188 | * @param string $cmd |
||
189 | 21 | * @param array $paths |
|
190 | * @return null|string |
||
191 | */ |
||
192 | 21 | public static function detectCmdLocationInPaths($cmd, array $paths) |
|
203 | |||
204 | 17 | /** |
|
205 | * Return local $PATH variable. |
||
206 | * |
||
207 | * @return string |
||
208 | * @throws \RuntimeException |
||
209 | 17 | */ |
|
210 | 1 | public static function getEnvPath() |
|
220 | |||
221 | /** |
||
222 | 3 | * Returns the executable command if the command is executable, null otherwise. |
|
223 | * Search for $command.exe on Windows systems. |
||
224 | 3 | * |
|
225 | * @param string $command |
||
226 | * @return string |
||
227 | */ |
||
228 | public static function isExecutable($command) |
||
242 | |||
243 | 14 | /** |
|
244 | 1 | * Is given path absolute. |
|
245 | 1 | * |
|
246 | 1 | * @param string $path |
|
247 | 1 | * @return boolean |
|
248 | 1 | */ |
|
249 | 14 | public static function isAbsolutePath($path) |
|
275 | |||
276 | /** |
||
277 | * Is given path an absolute windows path. |
||
278 | * |
||
279 | * @param string $path |
||
280 | * @return bool |
||
281 | */ |
||
282 | public static function isAbsoluteWindowsPath($path) |
||
286 | |||
287 | /** |
||
288 | * Converts a path to an absolute one if necessary relative to a given base path. |
||
289 | * |
||
290 | * @param string $path |
||
291 | * @param string $base |
||
292 | * @param boolean $useIncludePath |
||
293 | * @return string |
||
294 | */ |
||
295 | public static function toAbsolutePath($path, $base, $useIncludePath = false) |
||
311 | |||
312 | /** |
||
313 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
||
314 | * |
||
315 | * @author Sebastian Bergmann <[email protected]> |
||
316 | * @param string $color |
||
317 | * @param string $buffer |
||
318 | * @return string |
||
319 | */ |
||
320 | public static function formatWithColor($color, $buffer) |
||
339 | |||
340 | /** |
||
341 | * Fills up a text buffer with '*' to consume 72 chars. |
||
342 | * |
||
343 | * @param string $buffer |
||
344 | * @param int $length |
||
345 | * @return string |
||
346 | */ |
||
347 | public static function formatWithAsterisk($buffer, $length = 75) |
||
351 | |||
352 | /** |
||
353 | * Removes a directory that is not empty. |
||
354 | * |
||
355 | * @param $dir |
||
356 | */ |
||
357 | public static function removeDir($dir) |
||
371 | } |
||
372 |