| Total Complexity | 42 |
| Total Lines | 139 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UFileSystem 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.
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 UFileSystem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class UFileSystem { |
||
| 12 | use UFileSystemWriter; |
||
| 13 | |||
| 14 | public static function glob_recursive($pattern, $flags=0) { |
||
| 15 | $files=\glob($pattern, $flags); |
||
| 16 | foreach ( \glob(\dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir ) { |
||
| 17 | $files=\array_merge($files, self::glob_recursive($dir . '/' . \basename($pattern), $flags)); |
||
| 18 | } |
||
| 19 | return $files; |
||
| 20 | } |
||
| 21 | |||
| 22 | public static function deleteAllFilesFromFolder($folder) { |
||
| 23 | $files=\glob($folder . '/*'); |
||
| 24 | foreach ( $files as $file ) { |
||
| 25 | if (\is_file($file)) |
||
| 26 | \unlink($file); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | public static function deleteFile($filename){ |
||
| 31 | if (\file_exists($filename)) |
||
| 32 | return \unlink($filename); |
||
| 33 | return false; |
||
| 34 | } |
||
| 35 | |||
| 36 | public static function safeMkdir($dir) { |
||
| 37 | if (!\is_dir($dir)) |
||
| 38 | return \mkdir($dir, 0777, true); |
||
| 39 | return true; |
||
| 40 | } |
||
| 41 | |||
| 42 | public static function cleanPathname($path) { |
||
| 43 | if (UString::isNotNull($path)) { |
||
| 44 | if (\DS === "/") |
||
| 45 | $path=\str_replace("\\", \DS, $path); |
||
| 46 | else |
||
| 47 | $path=\str_replace("/", \DS, $path); |
||
| 48 | $path=\str_replace(\DS . \DS, \DS, $path); |
||
| 49 | if (!UString::endswith($path, \DS)) { |
||
| 50 | $path=$path . \DS; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | return $path; |
||
| 54 | } |
||
| 55 | |||
| 56 | public static function cleanFilePathname($path) { |
||
| 57 | if (UString::isNotNull($path)) { |
||
| 58 | if (\DS === "/") |
||
| 59 | $path=\str_replace("\\", \DS, $path); |
||
| 60 | else |
||
| 61 | $path=\str_replace("/", \DS, $path); |
||
| 62 | $path=\str_replace(\DS . \DS, \DS, $path); |
||
| 63 | } |
||
| 64 | return $path; |
||
| 65 | } |
||
| 66 | |||
| 67 | public static function tryToRequire($file) { |
||
| 68 | if (\file_exists($file)) { |
||
| 69 | require_once ($file); |
||
| 70 | return true; |
||
| 71 | } |
||
| 72 | return false; |
||
| 73 | } |
||
| 74 | |||
| 75 | public static function lastModified($filename) { |
||
| 76 | return \filemtime($filename); |
||
| 77 | } |
||
| 78 | |||
| 79 | public static function load($filename){ |
||
| 80 | if (\file_exists($filename)) { |
||
| 81 | return \file_get_contents($filename); |
||
| 82 | } |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | |||
| 86 | public static function getDirFromNamespace($ns){ |
||
| 87 | return \ROOT . \DS . str_replace ( "\\", \DS, $ns ); |
||
| 88 | } |
||
| 89 | |||
| 90 | public static function delTree($dir) { |
||
| 91 | $files = array_diff(scandir($dir), array('.','..')); |
||
| 92 | foreach ($files as $file) { |
||
| 93 | (is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file"); |
||
| 94 | } |
||
| 95 | return rmdir($dir); |
||
| 96 | } |
||
| 97 | |||
| 98 | public static function getLines($filename,$reverse=false,$maxLines=null,$lineCallback=null){ |
||
| 150 | } |
||
| 151 | } |
||
| 152 |