Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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) |
|
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 be located |
||
114 | * @param string $path Directory where the command should be located |
||
115 | 8 | * @param array $optionalLocations Some fallback locations where to investigate |
|
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 = []) |
||
160 | |||
161 | /** |
||
162 | * Return local $PATH variable. |
||
163 | * |
||
164 | * @return string |
||
165 | * @throws \RuntimeException |
||
166 | */ |
||
167 | public static function getEnvPath() |
||
177 | |||
178 | /** |
||
179 | * Returns the executable command if the command is executable, null otherwise. |
||
180 | 9 | * Search for $command.exe on Windows systems. |
|
181 | * |
||
182 | * @param string $command |
||
183 | * @return string |
||
184 | */ |
||
185 | public static function isExecutable($command) |
||
199 | |||
200 | /** |
||
201 | * Is given path absolute. |
||
202 | * |
||
203 | * @param string $path |
||
204 | 17 | * @return boolean |
|
205 | */ |
||
206 | public static function isAbsolutePath($path) |
||
232 | |||
233 | /** |
||
234 | * Is given path an absolute windows path. |
||
235 | 17 | * |
|
236 | * @param string $path |
||
237 | 17 | * @return bool |
|
238 | 3 | */ |
|
239 | public static function isAbsoluteWindowsPath($path) |
||
243 | 14 | ||
244 | 1 | /** |
|
245 | 1 | * Converts a path to an absolute one if necessary relative to a given base path. |
|
246 | 1 | * |
|
247 | 1 | * @param string $path |
|
248 | 1 | * @param string $base |
|
249 | 14 | * @param boolean $useIncludePath |
|
250 | * @return string |
||
251 | */ |
||
252 | public static function toAbsolutePath($path, $base, $useIncludePath = false) |
||
268 | 1 | ||
269 | 1 | /** |
|
270 | 1 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
|
271 | * |
||
272 | * @author Sebastian Bergmann <[email protected]> |
||
273 | * @param string $color |
||
274 | * @param string $buffer |
||
275 | * @return string |
||
276 | */ |
||
277 | public static function formatWithColor($color, $buffer) |
||
296 | |||
297 | /** |
||
298 | * Fills up a text buffer with '*' to consume 72 chars. |
||
299 | * |
||
300 | * @param string $buffer |
||
301 | * @param int $length |
||
302 | * @return string |
||
303 | */ |
||
304 | public static function formatWithAsterisk($buffer, $length = 75) |
||
308 | |||
309 | /** |
||
310 | * Removes a directory that is not empty. |
||
311 | * |
||
312 | * @param $dir |
||
313 | */ |
||
314 | public static function removeDir($dir) |
||
328 | } |
||
329 |