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