Complex classes like DirHelper 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 DirHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class DirHelper |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Check if passed path exists or not. |
||
| 13 | * @param string $filePath |
||
| 14 | * @return bool |
||
| 15 | */ |
||
| 16 | public static function isDirSafe(string $filePath) : bool |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Check if passed path exists or try to create it. |
||
| 27 | * Return false if it fails to create it or if a file (and not a dir) passed as argument. |
||
| 28 | * @param string $filePath |
||
| 29 | * @param string $modeMask default '0755' |
||
| 30 | * @return bool |
||
| 31 | */ |
||
| 32 | public static function checkDirExistOrCreate(string $filePath, string $modeMask = '0755') : bool |
||
| 45 | |||
| 46 | /** |
||
| 47 | * If dir passed, check if finishes with '/' otherwise append a slash to path. |
||
| 48 | * If wrong or empty string passed, return '/'. |
||
| 49 | * @param string $path |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | public static function addFinalSlash(string $path) : string |
||
| 63 | |||
| 64 | /** |
||
| 65 | * for each dir passed in array, check if it finishes with '/' otherwise append a slash to path. |
||
| 66 | * If not dir, leave the element untouched. |
||
| 67 | * @param array $paths |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public static function addFinalSlashToAllPaths(array $paths) : array |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Check if path ends with slash '/' |
||
| 81 | * @param string $paths |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | public static function endsWithSlash(string $paths) : bool |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Check if path ends with star '*' |
||
| 91 | * @param string $paths |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | public static function endsWithStar(string $paths) : bool |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Check if path ends with $needle |
||
| 101 | * @param string $paths |
||
| 102 | * @param string $needle |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public static function endsWith(string $paths, string $needle) : bool |
||
| 106 | { |
||
| 107 | if ($paths == '') { |
||
| 108 | return false; |
||
| 109 | } |
||
| 110 | if ($needle == '') { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | // search forward starting from end minus needle length characters |
||
| 115 | // see: http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php |
||
| 116 | return (($temp = strlen($paths) - strlen($needle)) >= 0 && strpos($paths, $needle, $temp) !== false); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Check if path starts with slash '/' |
||
| 121 | * @param string $paths |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public static function startsWithSlash(string $paths) : bool |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Check if path starts with slash $needle |
||
| 131 | * @param string $paths |
||
| 132 | * @param string $needle |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public static function startsWith(string $paths, string $needle) : bool |
||
| 136 | { |
||
| 137 | if ($paths == '') { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | if ($needle == '') { |
||
| 141 | return false; |
||
| 142 | } |
||
| 143 | |||
| 144 | // search backwards starting from haystack length characters from the end |
||
| 145 | // see: http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php |
||
| 146 | return strrpos($paths, $needle, -strlen($paths)) !== false; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Find dirs matching a pattern (recursive with subdirs). |
||
| 151 | * Returns an array containing the matched dirs (full path and not files), |
||
| 152 | * an empty array if no dir matched or on error. |
||
| 153 | * @param string $pathPattern if is null it set to base_path()/* if exists otherwise __DIR__/*. It support glob() string pattern. |
||
| 154 | * @return array of dirs |
||
| 155 | */ |
||
| 156 | public static function findDirs(string $pathPattern) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Dir::Delete() |
||
| 177 | * get a dir path and remove all files and subdir in this dir. |
||
| 178 | * if $not_remove_dir==TRUE then when finish DO NOT REMOVE THE $directory dir. |
||
| 179 | * @param string $directory directory to empty |
||
| 180 | * @param bool $not_remove_dir TRUE if DO NOT REMOVE THE $directory dir but only files. |
||
| 181 | * @return bool true if success, otherwise false |
||
| 182 | **/ |
||
| 183 | public static function delete($directory, bool $not_remove_dir = false) : bool |
||
| 184 | { |
||
| 185 | $directory = self::removeFinalSlash($directory); |
||
| 186 | |||
| 187 | if (!self::isDirSafe($directory) || !is_readable($directory)) { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | $directoryHandle = opendir($directory); |
||
| 192 | while (false !== ($contents = readdir($directoryHandle))) { |
||
| 193 | if (self::isDotDir($contents)) { |
||
| 194 | continue; |
||
| 195 | } |
||
| 196 | $path = $directory . "/" . $contents; |
||
| 197 | |||
| 198 | if (is_dir($path)) { |
||
| 199 | self::delete($path, $not_remove_dir); |
||
| 200 | } else { |
||
| 201 | unlink($path); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | closedir($directoryHandle); |
||
| 205 | |||
| 206 | if (!$not_remove_dir) { |
||
| 207 | return true; |
||
| 208 | } |
||
| 209 | return rmdir($directory); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Remove final slash ('/') char in dir if ends with slash. |
||
| 214 | * @param $directory |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public static function removeFinalSlash($directory) : string |
||
| 224 | |||
| 225 | /** |
||
| 226 | * For each dir passed in array, check if not finishes with '/' otherwise remove a slash to path. |
||
| 227 | * If not dir, leave the element untouched. |
||
| 228 | * @param array $paths |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public static function removeFinalSlashToAllPaths(array $paths) : array |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Remove start slash ('/') char in dir if starts with slash. |
||
| 242 | * @param string $directory |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public static function removeStartSlash($directory) : string |
||
| 252 | |||
| 253 | /** |
||
| 254 | * For each dir passed in array, check if not started with '/' otherwise remove a slash to path. |
||
| 255 | * If not dir, leave the element untouched. |
||
| 256 | * @param array $paths |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | public static function removeStartSlashToAllPaths(array $paths) : array |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Dir::copy() |
||
| 270 | * Copy a source directory (files and all subdirectories) to destination directory. |
||
| 271 | * If Destination directory doesn't exists try to create it. |
||
| 272 | * @param $directorySource |
||
| 273 | * @param $directoryDestination |
||
| 274 | * @param array $excludedDirectory array of path to be escluded (i.e. it will not copied to destination folder) |
||
| 275 | * @param \Closure|null $copied a function with two arguments ($directorySource,$directoryDestination). |
||
| 276 | * @return bool true if success, otherwise false |
||
| 277 | */ |
||
| 278 | public static function copy( |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param $contents |
||
| 313 | * @param $directorySource |
||
| 314 | * @param $directoryDestination |
||
| 315 | * @param $excludedDirectory |
||
| 316 | * @param $copied |
||
| 317 | */ |
||
| 318 | private static function copyInternal( |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns whether the given path is on the local filesystem. |
||
| 344 | * |
||
| 345 | * @param string $path A path string |
||
| 346 | * |
||
| 347 | * @return boolean Returns true if the path is local, false for a URL |
||
| 348 | * @see https://github.com/laradic/support/blob/master/src/Path.php |
||
| 349 | */ |
||
| 350 | public static function isLocal($path) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns whether a path is absolute Unix path. |
||
| 357 | * |
||
| 358 | * @param string $path A path string |
||
| 359 | * |
||
| 360 | * @return boolean Returns true if the path is absolute unix path, false if it is |
||
| 361 | * relative or empty |
||
| 362 | */ |
||
| 363 | public static function isAbsoluteUnix($path) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Returns whether a path is absolute Windows Path. |
||
| 370 | * |
||
| 371 | * @param string $path A path string |
||
| 372 | * |
||
| 373 | * @return boolean Returns true if the path is absolute, false if it is |
||
| 374 | * relative or empty |
||
| 375 | * @see https://github.com/laradic/support/blob/master/src/Path.php |
||
| 376 | */ |
||
| 377 | public static function isAbsoluteWindows($path) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Check if win special drive C: or Normal win drive C:/ C:\ |
||
| 391 | * @param string $path |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | protected static function isAbsoluteWindowsRoot($path):bool |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns whether a path is absolute. |
||
| 411 | * |
||
| 412 | * @param string $path A path string |
||
| 413 | * |
||
| 414 | * @return boolean Returns true if the path is absolute, false if it is |
||
| 415 | * relative or empty |
||
| 416 | */ |
||
| 417 | public static function isAbsolute($path) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Returns whether a path is relative. |
||
| 424 | * |
||
| 425 | * @param string $path A path string |
||
| 426 | * |
||
| 427 | * @return boolean Returns true if the path is relative or empty, false if |
||
| 428 | * it is absolute |
||
| 429 | * @see https://github.com/laradic/support/blob/master/src/Path.php |
||
| 430 | */ |
||
| 431 | public static function isRelative($path) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Joins a split file system path. |
||
| 438 | * |
||
| 439 | * @param array|string |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | * @see https://github.com/laradic/support/blob/master/src/Path.php |
||
| 443 | */ |
||
| 444 | public static function join() : string |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Similar to the join() method, but also normalize()'s the result |
||
| 464 | * |
||
| 465 | * @param string|array |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | * @see https://github.com/laradic/support/blob/master/src/Path.php |
||
| 469 | */ |
||
| 470 | public static function njoin() : string |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Canonicalizes the given path. |
||
| 477 | * |
||
| 478 | * During normalization, all slashes are replaced by forward slashes ("/"). |
||
| 479 | * Furthermore, all "." and ".." segments are removed as far as possible. |
||
| 480 | * ".." segments at the beginning of relative paths are not removed. |
||
| 481 | * |
||
| 482 | * ```php |
||
| 483 | * echo DirHelper::canonicalize("\webmozart\puli\..\css\style.css"); |
||
| 484 | * // => /webmozart/style.css |
||
| 485 | * |
||
| 486 | * echo DirHelper::canonicalize("../css/./style.css"); |
||
| 487 | * // => ../css/style.css |
||
| 488 | * ``` |
||
| 489 | * |
||
| 490 | * This method is able to deal with both UNIX and Windows paths. |
||
| 491 | * |
||
| 492 | * @param string $path A path string |
||
| 493 | * |
||
| 494 | * @return string The canonical path |
||
| 495 | * @see https://github.com/laradic/support/blob/master/src/Path.php |
||
| 496 | */ |
||
| 497 | public static function canonicalize($path) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Collapse dot folder '.', '..', if possible |
||
| 517 | * @param string $root |
||
| 518 | * @param $part |
||
| 519 | * @param $canonicalParts |
||
| 520 | */ |
||
| 521 | protected static function collapseDotFolder($root, $part, &$canonicalParts) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Splits a part into its root directory and the remainder. |
||
| 542 | * |
||
| 543 | * If the path has no root directory, an empty root directory will be |
||
| 544 | * returned. |
||
| 545 | * |
||
| 546 | * If the root directory is a Windows style partition, the resulting root |
||
| 547 | * will always contain a trailing slash. |
||
| 548 | * |
||
| 549 | * list ($root, $path) = DirHelpersplit("C:/webmozart") |
||
| 550 | * // => array("C:/", "webmozart") |
||
| 551 | * |
||
| 552 | * list ($root, $path) = DirHelpersplit("C:") |
||
| 553 | * // => array("C:/", "") |
||
| 554 | * |
||
| 555 | * @param string $path The canonical path to split |
||
| 556 | * |
||
| 557 | * @return string[] An array with the root directory and the remaining relative |
||
| 558 | * path |
||
| 559 | * @see https://github.com/laradic/support/blob/master/src/Path.php |
||
| 560 | */ |
||
| 561 | private static function split($path) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Check if a directory is empty in efficent way. |
||
| 588 | * Check hidden files too. |
||
| 589 | * @param string $path |
||
| 590 | * @return bool |
||
| 591 | */ |
||
| 592 | public static function isDirEmpty(string $path) : bool |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Check if an antry is linux dot dir (i.e.: . or .. ) |
||
| 613 | * @param $entry |
||
| 614 | * @return bool |
||
| 615 | */ |
||
| 616 | public static function isDotDir($entry):bool |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Check if $path is a dir and is readable. |
||
| 623 | * Return false is you pass a file. |
||
| 624 | * @param string $path |
||
| 625 | * @return bool |
||
| 626 | */ |
||
| 627 | public static function isReadable(string $path):bool |
||
| 631 | } |
||
| 632 |