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 |
||
| 22 | abstract class Util |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * List of console style codes. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $ansiCodes = [ |
||
| 30 | 'bold' => 1, |
||
| 31 | 'fg-black' => 30, |
||
| 32 | 'fg-red' => 31, |
||
| 33 | 'fg-green' => 32, |
||
| 34 | 'fg-yellow' => 33, |
||
| 35 | 'fg-cyan' => 36, |
||
| 36 | 'fg-white' => 37, |
||
| 37 | 'bg-red' => 41, |
||
| 38 | 'bg-green' => 42, |
||
| 39 | 'bg-yellow' => 43 |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Detect a given command's location. |
||
| 44 | * |
||
| 45 | * @param string $cmd The command to locate |
||
| 46 | * @param string $path Directory where the command should be |
||
| 47 | * @param array $optionalLocations Some fallback locations where to search for the command |
||
| 48 | * @return string Absolute path to detected command including command itself |
||
| 49 | * @throws \RuntimeException |
||
| 50 | */ |
||
| 51 | 5 | public static function detectCmdLocation(string $cmd, string $path = '', array $optionalLocations = []) : string |
|
| 52 | { |
||
| 53 | $detectionSteps = [ |
||
| 54 | function($cmd) use ($path) { |
||
| 55 | 5 | if (!empty($path)) { |
|
| 56 | 2 | return self::detectCmdLocationInPath($cmd, $path); |
|
| 57 | } |
||
| 58 | 3 | return ''; |
|
| 59 | 5 | }, |
|
| 60 | function($cmd) { |
||
| 61 | 3 | return self::detectCmdLocationWithWhich($cmd); |
|
| 62 | 5 | }, |
|
| 63 | function($cmd) { |
||
| 64 | 2 | $paths = explode(PATH_SEPARATOR, self::getEnvPath()); |
|
| 65 | 2 | return self::detectCmdLocationInPaths($cmd, $paths); |
|
| 66 | 5 | }, |
|
| 67 | function($cmd) use ($optionalLocations) { |
||
| 68 | 2 | return self::detectCmdLocationInPaths($cmd, $optionalLocations); |
|
| 69 | 5 | } |
|
| 70 | ]; |
||
| 71 | |||
| 72 | 5 | foreach ($detectionSteps as $step) { |
|
| 73 | 5 | $bin = $step($cmd); |
|
| 74 | 4 | if (!empty($bin)) { |
|
| 75 | 3 | return $bin; |
|
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | 1 | throw new RuntimeException(sprintf('\'%s\' was nowhere to be found please specify the correct path', $cmd)); |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Detect a command in a given path. |
||
| 84 | * |
||
| 85 | * @param string $cmd |
||
| 86 | * @param string $path |
||
| 87 | * @return string |
||
| 88 | * @throws \RuntimeException |
||
| 89 | */ |
||
| 90 | 2 | View Code Duplication | public static function detectCmdLocationInPath(string $cmd, string $path) : string |
| 99 | |||
| 100 | /** |
||
| 101 | * Detect command location using which cli command. |
||
| 102 | * |
||
| 103 | * @param string $cmd |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | 3 | public static function detectCmdLocationWithWhich($cmd) : string |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Check path list for executable command. |
||
| 120 | * |
||
| 121 | * @param string $cmd |
||
| 122 | * @param array $paths |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | 2 | View Code Duplication | public static function detectCmdLocationInPaths($cmd, array $paths) : string |
| 136 | |||
| 137 | /** |
||
| 138 | * Return local $PATH variable. |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | * @throws \RuntimeException |
||
| 142 | */ |
||
| 143 | 3 | public static function getEnvPath() : string |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the executable command if the command is executable, empty string otherwise. |
||
| 156 | * Search for $command.exe on Windows systems. |
||
| 157 | * |
||
| 158 | * @param string $command |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | 5 | public static function getExecutable($command) : string |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Is given path absolute. |
||
| 178 | * |
||
| 179 | * @param string $path |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | 5 | public static function isAbsolutePath($path) : bool |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Is given path an absolute windows path. |
||
| 211 | * |
||
| 212 | * @param string $path |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | 3 | public static function isAbsoluteWindowsPath($path) : bool |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Converts a path to an absolute one if necessary relative to a given base path. |
||
| 222 | * |
||
| 223 | * @param string $path |
||
| 224 | * @param string $base |
||
| 225 | * @param bool $useIncludePath |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | 2 | public static function toAbsolutePath(string $path, string $base, bool $useIncludePath = false) : string |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
||
| 247 | * |
||
| 248 | * @author Sebastian Bergmann <[email protected]> |
||
| 249 | * @param string $color |
||
| 250 | * @param string $buffer |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | 2 | public static function formatWithColor(string $color, string $buffer) : string |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Fills up a text buffer with '*' to consume by default 72 chars. |
||
| 275 | * |
||
| 276 | * @param string $buffer |
||
| 277 | * @param int $length |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | 1 | public static function formatWithAsterisk(string $buffer, int $length = 72) : string |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Can command pipe operator be used. |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | 1 | public static function canPipe() : bool |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Removes a directory that is not empty. |
||
| 297 | * |
||
| 298 | * @param string $dir |
||
| 299 | */ |
||
| 300 | 1 | public static function removeDir(string $dir) |
|
| 314 | } |
||
| 315 |