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 console color codes. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $ansiCodes = [ |
||
25 | 'bold' => 1, |
||
26 | 'fg-black' => 30, |
||
27 | 'fg-red' => 31, |
||
28 | 'fg-green' => 32, |
||
29 | 'fg-yellow' => 33, |
||
30 | 'fg-cyan' => 36, |
||
31 | 'fg-white' => 37, |
||
32 | 'bg-red' => 41, |
||
33 | 'bg-green' => 42, |
||
34 | 'bg-yellow' => 43 |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * Optional command locations |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private static $optionalCommandLocations = [ |
||
43 | 'mongodump' => [], |
||
44 | 'mysqldump' => [ |
||
45 | '/usr/local/mysql/bin', // Mac OS X |
||
46 | '/usr/mysql/bin', // Linux |
||
47 | ], |
||
48 | 'tar' => [], |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * Adds a new 'path' to the list of optional command locations. |
||
53 | * |
||
54 | * @param string $command |
||
55 | * @param string $path |
||
56 | */ |
||
57 | 1 | public static function addCommandLocation($command, $path) |
|
61 | |||
62 | /** |
||
63 | * Returns the list of optional 'mysqldump' locations. |
||
64 | * |
||
65 | * @param string $command |
||
66 | * @return array |
||
67 | */ |
||
68 | 209 | public static function getCommandLocations($command) : array |
|
72 | |||
73 | /** |
||
74 | * Detect a given command's location. |
||
75 | * |
||
76 | * @param string $cmd The command to locate |
||
77 | * @param string $path Directory where the command should be |
||
78 | * @param array $optionalLocations Some fallback locations where to search for the command |
||
79 | * @return string Absolute path to detected command including command itself |
||
80 | * @throws \RuntimeException |
||
81 | */ |
||
82 | public static function detectCmdLocation(string $cmd, string $path = '', array $optionalLocations = []) : string |
||
83 | { |
||
84 | $detectionSteps = [ |
||
85 | 212 | function($cmd) use ($path) { |
|
86 | 212 | if (!empty($path)) { |
|
87 | 206 | return self::detectCmdLocationInPath($cmd, $path); |
|
88 | } |
||
89 | 6 | return ''; |
|
90 | 212 | }, |
|
91 | 212 | function($cmd) { |
|
92 | 6 | return self::detectCmdLocationWithWhich($cmd); |
|
93 | 212 | }, |
|
94 | 212 | function($cmd) { |
|
95 | 2 | $paths = explode(PATH_SEPARATOR, self::getEnvPath()); |
|
96 | 2 | return self::detectCmdLocationInPaths($cmd, $paths); |
|
97 | 212 | }, |
|
98 | 212 | function($cmd) use ($optionalLocations) { |
|
99 | 2 | return self::detectCmdLocationInPaths($cmd, $optionalLocations); |
|
100 | 212 | } |
|
101 | ]; |
||
102 | |||
103 | 212 | foreach ($detectionSteps as $step) { |
|
104 | 212 | $bin = $step($cmd); |
|
105 | 211 | if (!empty($bin)) { |
|
106 | 211 | return $bin; |
|
107 | } |
||
108 | } |
||
109 | |||
110 | 1 | throw new RuntimeException(sprintf('\'%s\' was nowhere to be found please specify the correct path', $cmd)); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Detect a command in a given path. |
||
115 | * |
||
116 | * @param string $cmd |
||
117 | * @param string $path |
||
118 | * @return string |
||
119 | * @throws \RuntimeException |
||
120 | */ |
||
121 | 206 | View Code Duplication | public static function detectCmdLocationInPath(string $cmd, string $path) : string |
130 | |||
131 | /** |
||
132 | * Detect command location using which cli command. |
||
133 | * |
||
134 | * @param string $cmd |
||
135 | * @return string |
||
136 | */ |
||
137 | 6 | public static function detectCmdLocationWithWhich($cmd) : string |
|
148 | |||
149 | /** |
||
150 | * Check path list for executable command. |
||
151 | * |
||
152 | * @param string $cmd |
||
153 | * @param array $paths |
||
154 | * @return string |
||
155 | */ |
||
156 | 2 | View Code Duplication | public static function detectCmdLocationInPaths($cmd, array $paths) : string |
167 | |||
168 | /** |
||
169 | * Return local $PATH variable. |
||
170 | * |
||
171 | * @return string |
||
172 | * @throws \RuntimeException |
||
173 | */ |
||
174 | 3 | public static function getEnvPath() : string |
|
184 | |||
185 | /** |
||
186 | * Returns the executable command if the command is executable, null otherwise. |
||
187 | * Search for $command.exe on Windows systems. |
||
188 | * |
||
189 | * @param string $command |
||
190 | * @return string |
||
191 | */ |
||
192 | 212 | public static function isExecutable($command) : string |
|
206 | |||
207 | /** |
||
208 | * Is given path absolute. |
||
209 | * |
||
210 | * @param string $path |
||
211 | * @return bool |
||
212 | */ |
||
213 | 44 | public static function isAbsolutePath($path) : bool |
|
239 | |||
240 | /** |
||
241 | * Is given path an absolute windows path. |
||
242 | * |
||
243 | * @param string $path |
||
244 | * @return bool |
||
245 | */ |
||
246 | 3 | public static function isAbsoluteWindowsPath($path) : bool |
|
250 | |||
251 | /** |
||
252 | * Converts a path to an absolute one if necessary relative to a given base path. |
||
253 | * |
||
254 | * @param string $path |
||
255 | * @param string $base |
||
256 | * @param boolean $useIncludePath |
||
257 | * @return string |
||
258 | */ |
||
259 | 41 | public static function toAbsolutePath(string $path, string $base, bool $useIncludePath = false) : string |
|
275 | |||
276 | /** |
||
277 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
||
278 | * |
||
279 | * @author Sebastian Bergmann <[email protected]> |
||
280 | * @param string $color |
||
281 | * @param string $buffer |
||
282 | * @return string |
||
283 | */ |
||
284 | 3 | public static function formatWithColor(string $color, string $buffer) : string |
|
303 | |||
304 | /** |
||
305 | * Fills up a text buffer with '*' to consume 72 chars. |
||
306 | * |
||
307 | * @param string $buffer |
||
308 | * @param int $length |
||
309 | * @return string |
||
310 | */ |
||
311 | 14 | public static function formatWithAsterisk(string $buffer, int $length = 75) : string |
|
315 | |||
316 | /** |
||
317 | * Can command pipe operator be used. |
||
318 | * |
||
319 | * @return bool |
||
320 | */ |
||
321 | 3 | public static function canPipe() : bool |
|
325 | |||
326 | /** |
||
327 | * Removes a directory that is not empty. |
||
328 | * |
||
329 | * @param string $dir |
||
330 | */ |
||
331 | 1 | public static function removeDir(string $dir) |
|
345 | } |
||
346 |