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 | 216 | 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 | 219 | function($cmd) use ($path) { |
|
| 86 | 219 | if (!empty($path)) { |
|
| 87 | 213 | return self::detectCmdLocationInPath($cmd, $path); |
|
| 88 | } |
||
| 89 | 6 | return ''; |
|
| 90 | 219 | }, |
|
| 91 | 219 | function($cmd) { |
|
| 92 | 6 | return self::detectCmdLocationWithWhich($cmd); |
|
| 93 | 219 | }, |
|
| 94 | 219 | function($cmd) { |
|
| 95 | 2 | $paths = explode(PATH_SEPARATOR, self::getEnvPath()); |
|
| 96 | 2 | return self::detectCmdLocationInPaths($cmd, $paths); |
|
| 97 | 219 | }, |
|
| 98 | 219 | function($cmd) use ($optionalLocations) { |
|
| 99 | 2 | return self::detectCmdLocationInPaths($cmd, $optionalLocations); |
|
| 100 | 219 | } |
|
| 101 | ]; |
||
| 102 | |||
| 103 | 219 | foreach ($detectionSteps as $step) { |
|
| 104 | 219 | $bin = $step($cmd); |
|
| 105 | 218 | if (!empty($bin)) { |
|
| 106 | 218 | 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 | 213 | public static function detectCmdLocationInPath(string $cmd, string $path) : string |
|
| 122 | { |
||
| 123 | 213 | $command = $path . DIRECTORY_SEPARATOR . $cmd; |
|
| 124 | 213 | $bin = self::isExecutable($command); |
|
| 125 | 213 | if (empty($bin)) { |
|
| 126 | 1 | throw new RuntimeException(sprintf('wrong path specified for \'%s\': %s', $cmd, $path)); |
|
| 127 | } |
||
| 128 | 212 | return $bin; |
|
| 129 | } |
||
| 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 | public static function detectCmdLocationInPaths($cmd, array $paths) : string |
|
| 157 | { |
||
| 158 | 2 | foreach ($paths as $path) { |
|
| 159 | 2 | $command = $path . DIRECTORY_SEPARATOR . $cmd; |
|
| 160 | 2 | $bin = self::isExecutable($command); |
|
| 161 | 2 | if (!empty($bin)) { |
|
| 162 | 2 | return $bin; |
|
| 163 | } |
||
| 164 | } |
||
| 165 | 2 | return ''; |
|
| 166 | } |
||
| 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 | 219 | public static function isExecutable($command) : string |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Is given path absolute. |
||
| 209 | * |
||
| 210 | * @param string $path |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 49 | 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 | 46 | public static function toAbsolutePath(string $path, string $base, bool $useIncludePath = false) : string |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Return list of directories in a given path. |
||
| 278 | * |
||
| 279 | * @param string $path |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | 15 | public static function getDirectoryList(string $path) : array |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Returns directory depth of a given path. |
||
| 293 | * |
||
| 294 | * @param string $path |
||
| 295 | * @return int |
||
| 296 | */ |
||
| 297 | 10 | public static function getPathDepth(string $path) : int |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
||
| 304 | * |
||
| 305 | * @author Sebastian Bergmann <[email protected]> |
||
| 306 | * @param string $color |
||
| 307 | * @param string $buffer |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | 3 | public static function formatWithColor(string $color, string $buffer) : string |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Fills up a text buffer with '*' to consume 72 chars. |
||
| 332 | * |
||
| 333 | * @param string $buffer |
||
| 334 | * @param int $length |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | 14 | public static function formatWithAsterisk(string $buffer, int $length = 75) : string |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Can command pipe operator be used. |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | 3 | public static function canPipe() : bool |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Removes a directory that is not empty. |
||
| 354 | * |
||
| 355 | * @param string $dir |
||
| 356 | */ |
||
| 357 | 1 | public static function removeDir(string $dir) |
|
| 371 | } |
||
| 372 |