Complex classes like FileHelper 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 FileHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class FileHelper |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Simple pathinfo wrapper. |
||
| 13 | * @param string $filePath |
||
| 14 | * @param int $fileInfOoptions |
||
| 15 | * @return string |
||
| 16 | * @see http://php.net/manual/en/function.pathinfo.php |
||
| 17 | */ |
||
| 18 | public static function getPathinfoPart(string $filePath, int $fileInfOoptions) : string |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Return the file name of file (without path and without extension). |
||
| 34 | * Return empty string if $filePath is null, empty or is a directory. |
||
| 35 | * Ex.: /public/upload/pippo.txt return '/public/upload' |
||
| 36 | * @param string $filePath |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | public static function getDirname(string $filePath) : string |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Return the file name of file (without path and with extension). |
||
| 46 | * Return empty string if $filePath is null, empty or is a directory. |
||
| 47 | * Ex.: /public/upload/pippo.txt return 'pippo.txt' |
||
| 48 | * @param string $filePath |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | public static function getFilename(string $filePath) : string |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Return the file name of file (without path and without extension). |
||
| 58 | * Return empty string if $filePath is null, empty or is a directory. |
||
| 59 | * Ex.: /public/upload/pippo.txt return 'pippo' |
||
| 60 | * @param string $filePath |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public static function getFilenameWithoutExtension(string $filePath) : string |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Return the file name of file (without path and without extension). |
||
| 70 | * Return empty string if $filePath is null, empty or is a directory. |
||
| 71 | * Ex.: /public/upload/pippo.txt return '.txt' |
||
| 72 | * @param string $filePath |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public static function getFilenameExtension(string $filePath) : string |
||
| 79 | |||
| 80 | /** |
||
| 81 | * unlink file if exists. |
||
| 82 | * Return false if exists and unlink fails or if filePath is a dir. |
||
| 83 | * @param string $filePath |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public static function unlinkSafe(string $filePath) : bool |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Check if passed file exists or not. |
||
| 101 | * If dir passed return false. |
||
| 102 | * @param string $filePath |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public static function fileExistsSafe(string $filePath) : bool |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Find files matching a pattern (recursive with matched files in subdirs). |
||
| 120 | * Returns an array containing the matched files (full path and not directories), |
||
| 121 | * an empty array if no file matched or on error. |
||
| 122 | * @param string $fileNamePattern if is null it set to base_path()/* if exists otherwise __DIR__/* . It support glob() string pattern. |
||
| 123 | * @param int $flags glob() Valid flags |
||
| 124 | * @return array of files (full path) |
||
| 125 | */ |
||
| 126 | public static function findFiles(string $fileNamePattern, int $flags = 0) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Equals to file_put_contents but safe, i.e. |
||
| 172 | * accept empty string and return false without raise an error, |
||
| 173 | * accept a directory and return false without raise an error, |
||
| 174 | * and if $forceCreateDirIfNotExists is set to true and path doesn't exists, file_put_contents fails |
||
| 175 | * so, this class, try to create the complete path before save file. |
||
| 176 | * @param string $filename file name including folder. |
||
| 177 | * example :: /path/to/file/filename.ext or filename.ext |
||
| 178 | * @param string $data The data to write. |
||
| 179 | * @param bool $forceCreateDirIfNotExists if true and path not exists, try to create it. |
||
| 180 | * @param string $modeMask The mask applied to dir if need to create some dir. |
||
| 181 | * @param int $flags same flags used for file_put_contents. |
||
| 182 | * @see more info: http://php.net/manual/en/function.file-put-contents.php |
||
| 183 | * @return bool TRUE file created succesfully, return FALSE if failed to create file. |
||
| 184 | */ |
||
| 185 | public static function filePutContentsSafe( |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Return mime type of a passed file in optimized mode. |
||
| 217 | * @param string $fullPathFile |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public static function getMimeType(string $fullPathFile) : string |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Return mime type of a passed file using finfo |
||
| 294 | * @param string $fullPathFile |
||
| 295 | * @return string return empty string if it fails. |
||
| 296 | */ |
||
| 297 | public static function getMimeTypeByFinfo(string $fullPathFile) : string |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Return mime type of a passed file using mime_content_type() |
||
| 313 | * @param string $fullPathFile |
||
| 314 | * @return string return empty string if it fails. |
||
| 315 | */ |
||
| 316 | public static function getMimeTypeByMime_content_type(string $fullPathFile) : string |
||
| 323 | } |
||
| 324 |