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 Util 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 Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class Util |
||
25 | { |
||
26 | /** |
||
27 | * List of console style codes. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $ansiCodes = [ |
||
32 | 'bold' => 1, |
||
33 | 'fg-black' => 30, |
||
34 | 'fg-red' => 31, |
||
35 | 'fg-green' => 32, |
||
36 | 'fg-yellow' => 33, |
||
37 | 'fg-cyan' => 36, |
||
38 | 'fg-white' => 37, |
||
39 | 'bg-red' => 41, |
||
40 | 'bg-green' => 42, |
||
41 | 'bg-yellow' => 43 |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Detect a given command's location. |
||
46 | * |
||
47 | * @param string $cmd The command to locate |
||
48 | * @param string $path Directory where the command should be |
||
49 | * @param array $optionalLocations Some fallback locations where to search for the command |
||
50 | * @return string Absolute path to detected command including command itself |
||
51 | 5 | * @throws \RuntimeException |
|
52 | */ |
||
53 | public static function detectCmdLocation(string $cmd, string $path = '', array $optionalLocations = []): string |
||
83 | |||
84 | /** |
||
85 | * Detect a command in a given path. |
||
86 | * |
||
87 | * @param string $cmd |
||
88 | * @param string $path |
||
89 | * @return string |
||
90 | 2 | * @throws \RuntimeException |
|
91 | */ |
||
92 | 2 | View Code Duplication | public static function detectCmdLocationInPath(string $cmd, string $path): string |
101 | |||
102 | /** |
||
103 | * Detect command location using which cli command. |
||
104 | * |
||
105 | * @param string $cmd |
||
106 | 3 | * @return string |
|
107 | */ |
||
108 | 3 | public static function detectCmdLocationWithWhich($cmd): string |
|
118 | |||
119 | /** |
||
120 | * Check path list for executable command. |
||
121 | * |
||
122 | * @param string $cmd |
||
123 | * @param array $paths |
||
124 | * @return string |
||
125 | 2 | */ |
|
126 | View Code Duplication | public static function detectCmdLocationInPaths($cmd, array $paths): string |
|
137 | |||
138 | /** |
||
139 | * Return local $PATH variable. |
||
140 | * |
||
141 | * @return string |
||
142 | * @throws \RuntimeException |
||
143 | 3 | */ |
|
144 | public static function getEnvPath(): string |
||
154 | |||
155 | /** |
||
156 | * Returns the executable command if the command is executable, empty string otherwise. |
||
157 | * Search for $command.exe on Windows systems. |
||
158 | * |
||
159 | * @param string $command |
||
160 | * @return string |
||
161 | 5 | */ |
|
162 | public static function getExecutable($command): string |
||
176 | |||
177 | /** |
||
178 | * Is given path absolute. |
||
179 | * |
||
180 | * @param string $path |
||
181 | * @return bool |
||
182 | 5 | */ |
|
183 | public static function isAbsolutePath($path): bool |
||
209 | |||
210 | /** |
||
211 | * Is given path an absolute windows path. |
||
212 | * |
||
213 | * @param string $path |
||
214 | * @return bool |
||
215 | 3 | */ |
|
216 | public static function isAbsoluteWindowsPath($path): bool |
||
220 | |||
221 | /** |
||
222 | * Converts a path to an absolute one if necessary relative to a given base path. |
||
223 | * |
||
224 | * @param string $path |
||
225 | * @param string $base |
||
226 | * @param bool $useIncludePath |
||
227 | * @return string |
||
228 | 2 | */ |
|
229 | public static function toAbsolutePath(string $path, string $base, bool $useIncludePath = false): string |
||
245 | |||
246 | /** |
||
247 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
||
248 | * |
||
249 | * @author Sebastian Bergmann <[email protected]> |
||
250 | * @param string $color |
||
251 | * @param string $buffer |
||
252 | * @return string |
||
253 | 2 | */ |
|
254 | public static function formatWithColor(string $color, string $buffer): string |
||
273 | |||
274 | /** |
||
275 | * Fills up a text buffer with '*' to consume by default 72 chars. |
||
276 | * |
||
277 | * @param string $buffer |
||
278 | * @param int $length |
||
279 | * @return string |
||
280 | 1 | */ |
|
281 | public static function formatWithAsterisk(string $buffer, int $length = 72): string |
||
285 | |||
286 | /** |
||
287 | * Can command pipe operator be used. |
||
288 | * |
||
289 | * @return bool |
||
290 | 1 | */ |
|
291 | public static function canPipe(): bool |
||
295 | |||
296 | /** |
||
297 | * Removes a directory that is not empty. |
||
298 | * |
||
299 | * @param string $dir |
||
300 | 1 | */ |
|
301 | public static function removeDir(string $dir) |
||
315 | } |
||
316 |