| Total Complexity | 57 |
| Total Lines | 385 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 3 | Features | 0 |
Complex classes like Filesystem 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 Filesystem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Filesystem implements FilesystemContract |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Get the base path of the HydePHP project. |
||
| 29 | * |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | public static function basePath(): string |
||
| 33 | { |
||
| 34 | return self::kernel()->path(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Format the given project path to be absolute. Already absolute paths are normalized. |
||
| 39 | * |
||
| 40 | * @param string $path |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public static function absolutePath(string $path): string |
||
| 44 | { |
||
| 45 | return self::kernel()->pathToAbsolute(self::relativePath($path)); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Remove the absolute path from the given project path so that it becomes relative. |
||
| 50 | * |
||
| 51 | * @param string $path |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | public static function relativePath(string $path): string |
||
| 55 | { |
||
| 56 | return self::kernel()->pathToRelative($path); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * A smarter glob function that will run the specified glob pattern a bit more intelligently. |
||
| 61 | * While this method will use the absolute path when interacting with the filesystem, |
||
| 62 | * the returned collection will only contain relative paths. |
||
| 63 | * |
||
| 64 | * @param string $pattern |
||
| 65 | * @param int $flags |
||
| 66 | * @return \Illuminate\Support\Collection<string> |
||
| 67 | */ |
||
| 68 | public static function smartGlob(string $pattern, int $flags = 0): Collection |
||
| 69 | { |
||
| 70 | return collect(self::glob($pattern, $flags)) |
||
|
|
|||
| 71 | ->map(fn (string $path): string => self::relativePath($path)); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Touch one or more files in the project's directory. |
||
| 76 | * |
||
| 77 | * @param string|array $path |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public static function touch(string|array $path): bool |
||
| 81 | { |
||
| 82 | return self::kernel()->filesystem()->touch($path); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Unlink one or more files in the project's directory. |
||
| 87 | * |
||
| 88 | * @param string|array $path |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | public static function unlink(string|array $path): bool |
||
| 92 | { |
||
| 93 | return self::kernel()->filesystem()->unlink($path); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get the contents of a file. |
||
| 98 | * |
||
| 99 | * @param string $path |
||
| 100 | * @param bool $lock |
||
| 101 | * @return string |
||
| 102 | * |
||
| 103 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 104 | */ |
||
| 105 | public static function getContents(string $path, bool $lock = false): string |
||
| 106 | { |
||
| 107 | return self::get($path, $lock); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Write the contents of a file. |
||
| 112 | * |
||
| 113 | * @param string $path |
||
| 114 | * @param string $contents |
||
| 115 | * @param bool $lock |
||
| 116 | * @return int|bool |
||
| 117 | */ |
||
| 118 | public static function putContents(string $path, string $contents, bool $lock = false): bool|int |
||
| 119 | { |
||
| 120 | return self::put($path, $contents, $lock); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** @inheritDoc */ |
||
| 124 | public static function exists(string $path): bool |
||
| 125 | { |
||
| 126 | return self::filesystem()->exists(self::absolutePath($path)); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** @inheritDoc */ |
||
| 130 | public static function missing(string $path): bool |
||
| 131 | { |
||
| 132 | return self::filesystem()->missing(self::absolutePath($path)); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** @inheritDoc */ |
||
| 136 | public static function get(string $path, bool $lock = false): string |
||
| 137 | { |
||
| 138 | return self::filesystem()->get(self::absolutePath($path), $lock); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** @inheritDoc */ |
||
| 142 | public static function sharedGet(string $path): string |
||
| 143 | { |
||
| 144 | return self::filesystem()->sharedGet(self::absolutePath($path)); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** @inheritDoc */ |
||
| 148 | public static function getRequire(string $path, array $data = []): mixed |
||
| 149 | { |
||
| 150 | return self::filesystem()->getRequire(self::absolutePath($path), $data); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** @inheritDoc */ |
||
| 154 | public static function requireOnce(string $path, array $data = []): mixed |
||
| 155 | { |
||
| 156 | return self::filesystem()->requireOnce(self::absolutePath($path), $data); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** @inheritDoc */ |
||
| 160 | public static function lines(string $path): \Illuminate\Support\LazyCollection |
||
| 161 | { |
||
| 162 | return self::filesystem()->lines(self::absolutePath($path)); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** @inheritDoc */ |
||
| 166 | public static function hash(string $path, string $algorithm = 'md5'): string |
||
| 167 | { |
||
| 168 | return self::filesystem()->hash(self::absolutePath($path), $algorithm); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** @inheritDoc */ |
||
| 172 | public static function put(string $path, string $contents, bool $lock = false): bool|int |
||
| 173 | { |
||
| 174 | return self::filesystem()->put(self::absolutePath($path), $contents, $lock); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** @inheritDoc */ |
||
| 178 | public static function replace(string $path, string $content): void |
||
| 179 | { |
||
| 180 | self::filesystem()->replace(self::absolutePath($path), $content); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** @inheritDoc */ |
||
| 184 | public static function replaceInFile(array|string $search, array|string $replace, string $path): void |
||
| 185 | { |
||
| 186 | self::filesystem()->replaceInFile($search, $replace, self::absolutePath($path)); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** @inheritDoc */ |
||
| 190 | public static function prepend(string $path, string $data): int |
||
| 191 | { |
||
| 192 | return self::filesystem()->prepend(self::absolutePath($path), $data); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** @inheritDoc */ |
||
| 196 | public static function append(string $path, string $data): int |
||
| 197 | { |
||
| 198 | return self::filesystem()->append(self::absolutePath($path), $data); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** @inheritDoc */ |
||
| 202 | public static function chmod(string $path, int $mode = null): mixed |
||
| 203 | { |
||
| 204 | return self::filesystem()->chmod(self::absolutePath($path), $mode); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** @inheritDoc */ |
||
| 208 | public static function delete(array|string $paths): bool |
||
| 209 | { |
||
| 210 | return self::filesystem()->delete(self::qualifyPossiblePathArray($paths)); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** @inheritDoc */ |
||
| 214 | public static function move(string $path, string $target): bool |
||
| 215 | { |
||
| 216 | return self::filesystem()->move(self::absolutePath($path), self::absolutePath($target)); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** @inheritDoc */ |
||
| 220 | public static function copy(string $path, string $target): bool |
||
| 221 | { |
||
| 222 | return self::filesystem()->copy(self::absolutePath($path), self::absolutePath($target)); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** @inheritDoc */ |
||
| 226 | public static function link(string $target, string $link): void |
||
| 227 | { |
||
| 228 | self::filesystem()->link(self::absolutePath($target), self::absolutePath($link)); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** @inheritDoc */ |
||
| 232 | public static function relativeLink(string $target, string $link): void |
||
| 233 | { |
||
| 234 | self::filesystem()->relativeLink(self::absolutePath($target), self::absolutePath($link)); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** @inheritDoc */ |
||
| 238 | public static function name(string $path): string |
||
| 239 | { |
||
| 240 | return self::filesystem()->name(self::absolutePath($path)); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** @inheritDoc */ |
||
| 244 | public static function basename(string $path): string |
||
| 245 | { |
||
| 246 | return self::filesystem()->basename(self::absolutePath($path)); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** @inheritDoc */ |
||
| 250 | public static function dirname(string $path): string |
||
| 251 | { |
||
| 252 | return self::filesystem()->dirname(self::absolutePath($path)); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** @inheritDoc */ |
||
| 256 | public static function extension(string $path): string |
||
| 257 | { |
||
| 258 | return self::filesystem()->extension(self::absolutePath($path)); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** @inheritDoc */ |
||
| 262 | public static function guessExtension(string $path): ?string |
||
| 263 | { |
||
| 264 | return self::filesystem()->guessExtension(self::absolutePath($path)); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** @inheritDoc */ |
||
| 268 | public static function type(string $path): string |
||
| 269 | { |
||
| 270 | return self::filesystem()->type(self::absolutePath($path)); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** @inheritDoc */ |
||
| 274 | public static function mimeType(string $path): bool|string |
||
| 275 | { |
||
| 276 | return self::filesystem()->mimeType(self::absolutePath($path)); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** @inheritDoc */ |
||
| 280 | public static function size(string $path): int |
||
| 281 | { |
||
| 282 | return self::filesystem()->size(self::absolutePath($path)); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** @inheritDoc */ |
||
| 286 | public static function lastModified(string $path): int |
||
| 287 | { |
||
| 288 | return self::filesystem()->lastModified(self::absolutePath($path)); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** @inheritDoc */ |
||
| 292 | public static function isDirectory(string $directory): bool |
||
| 293 | { |
||
| 294 | return self::filesystem()->isDirectory(self::absolutePath($directory)); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** @inheritDoc */ |
||
| 298 | public static function isEmptyDirectory(string $directory, bool $ignoreDotFiles = false): bool |
||
| 299 | { |
||
| 300 | return self::filesystem()->isEmptyDirectory(self::absolutePath($directory), $ignoreDotFiles); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** @inheritDoc */ |
||
| 304 | public static function isReadable(string $path): bool |
||
| 305 | { |
||
| 306 | return self::filesystem()->isReadable(self::absolutePath($path)); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** @inheritDoc */ |
||
| 310 | public static function isWritable(string $path): bool |
||
| 311 | { |
||
| 312 | return self::filesystem()->isWritable(self::absolutePath($path)); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** @inheritDoc */ |
||
| 316 | public static function hasSameHash(string $firstFile, string $secondFile): bool |
||
| 317 | { |
||
| 318 | return self::filesystem()->hasSameHash(self::absolutePath($firstFile), self::absolutePath($secondFile)); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** @inheritDoc */ |
||
| 322 | public static function isFile(string $file): bool |
||
| 323 | { |
||
| 324 | return self::filesystem()->isFile(self::absolutePath($file)); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** @inheritDoc */ |
||
| 328 | public static function glob(string $pattern, int $flags = 0): array |
||
| 329 | { |
||
| 330 | return self::filesystem()->glob(self::absolutePath($pattern), $flags); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** @inheritDoc */ |
||
| 334 | public static function files(string $directory, bool $hidden = false): array |
||
| 335 | { |
||
| 336 | return self::filesystem()->files(self::absolutePath($directory), $hidden); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** @inheritDoc */ |
||
| 340 | public static function allFiles(string $directory, bool $hidden = false): array |
||
| 341 | { |
||
| 342 | return self::filesystem()->allFiles(self::absolutePath($directory), $hidden); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** @inheritDoc*/ |
||
| 346 | public static function directories(string $directory): array |
||
| 347 | { |
||
| 348 | return self::filesystem()->directories(self::absolutePath($directory)); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** @inheritDoc */ |
||
| 352 | public static function ensureDirectoryExists(string $path, int $mode = 0755, bool $recursive = true): void |
||
| 355 | } |
||
| 356 | |||
| 357 | /** @inheritDoc */ |
||
| 358 | public static function makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false): bool |
||
| 359 | { |
||
| 360 | return self::filesystem()->makeDirectory(self::absolutePath($path), $mode, $recursive, $force); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** @inheritDoc */ |
||
| 364 | public static function moveDirectory(string $from, string $to, bool $overwrite = false): bool |
||
| 365 | { |
||
| 366 | return self::filesystem()->moveDirectory(self::absolutePath($from), self::absolutePath($to), $overwrite); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** @inheritDoc */ |
||
| 370 | public static function copyDirectory(string $directory, string $destination, ?int $options = null): bool |
||
| 371 | { |
||
| 372 | return self::filesystem()->copyDirectory(self::absolutePath($directory), self::absolutePath($destination), $options); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** @inheritDoc */ |
||
| 376 | public static function deleteDirectory(string $directory, bool $preserve = false): bool |
||
| 377 | { |
||
| 378 | return self::filesystem()->deleteDirectory(self::absolutePath($directory), $preserve); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** @inheritDoc */ |
||
| 382 | public static function deleteDirectories(string $directory): bool |
||
| 383 | { |
||
| 384 | return self::filesystem()->deleteDirectories(self::absolutePath($directory)); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** @inheritDoc */ |
||
| 388 | public static function cleanDirectory(string $directory): bool |
||
| 389 | { |
||
| 390 | return self::filesystem()->cleanDirectory(self::absolutePath($directory)); |
||
| 391 | } |
||
| 392 | |||
| 393 | protected static function kernel(): HydeKernel |
||
| 394 | { |
||
| 395 | return HydeKernel::getInstance(); |
||
| 396 | } |
||
| 397 | |||
| 398 | protected static function filesystem(): \Illuminate\Filesystem\Filesystem |
||
| 399 | { |
||
| 400 | return File::getFacadeRoot(); |
||
| 401 | } |
||
| 402 | |||
| 403 | protected static function qualifyPossiblePathArray(array|string $paths): array|string |
||
| 410 | } |
||
| 411 | } |
||
| 412 |