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 |
||
| 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 |
||
| 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 |
||
| 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 |