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 commands 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 = []) |
||
155 | 8 | ||
156 | /** |
||
157 | 1 | * Check path list for executable command. |
|
158 | 1 | * |
|
159 | * @param array $paths |
||
160 | * @param $cmd |
||
161 | * @return null|string |
||
162 | */ |
||
163 | public static function detectCmdLocationInPaths(array $paths, $cmd) |
||
174 | 9 | ||
175 | /** |
||
176 | * Return local $PATH variable. |
||
177 | * |
||
178 | * @return string |
||
179 | * @throws \RuntimeException |
||
180 | 9 | */ |
|
181 | public static function getEnvPath() |
||
191 | |||
192 | 21 | /** |
|
193 | 4 | * Returns the executable command if the command is executable, null otherwise. |
|
194 | * Search for $command.exe on Windows systems. |
||
195 | * |
||
196 | * @param string $command |
||
197 | * @return string |
||
198 | */ |
||
199 | public static function isExecutable($command) |
||
213 | 16 | ||
214 | /** |
||
215 | * Is given path absolute. |
||
216 | * |
||
217 | * @param string $path |
||
218 | * @return boolean |
||
219 | */ |
||
220 | public static function isAbsolutePath($path) |
||
246 | 1 | ||
247 | 1 | /** |
|
248 | 1 | * Is given path an absolute windows path. |
|
249 | 14 | * |
|
250 | * @param string $path |
||
251 | * @return bool |
||
252 | */ |
||
253 | public static function isAbsoluteWindowsPath($path) |
||
257 | 1 | ||
258 | /** |
||
259 | 1 | * Converts a path to an absolute one if necessary relative to a given base path. |
|
260 | 1 | * |
|
261 | 1 | * @param string $path |
|
262 | * @param string $base |
||
263 | 1 | * @param boolean $useIncludePath |
|
264 | 1 | * @return string |
|
265 | 1 | */ |
|
266 | 1 | public static function toAbsolutePath($path, $base, $useIncludePath = false) |
|
282 | |||
283 | /** |
||
284 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
||
285 | * |
||
286 | * @author Sebastian Bergmann <[email protected]> |
||
287 | * @param string $color |
||
288 | * @param string $buffer |
||
289 | * @return string |
||
290 | */ |
||
291 | public static function formatWithColor($color, $buffer) |
||
310 | |||
311 | /** |
||
312 | * Fills up a text buffer with '*' to consume 72 chars. |
||
313 | * |
||
314 | * @param string $buffer |
||
315 | * @param int $length |
||
316 | * @return string |
||
317 | */ |
||
318 | public static function formatWithAsterisk($buffer, $length = 75) |
||
322 | |||
323 | /** |
||
324 | * Removes a directory that is not empty. |
||
325 | * |
||
326 | * @param $dir |
||
327 | */ |
||
328 | public static function removeDir($dir) |
||
342 | } |
||
343 |