Total Complexity | 49 |
Total Lines | 282 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 1 |
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 |
||
16 | class FileSystem |
||
17 | { |
||
18 | |||
19 | private $rootPath = null; |
||
20 | |||
21 | /** |
||
22 | * Constructs a new FileSystem object with the given root path. |
||
23 | * |
||
24 | * @param string $rootPath The starting point of the represented file system. |
||
25 | * @throws \InvalidArgumentException If the root path is invalid. |
||
26 | */ |
||
27 | function __construct(string $rootPath) |
||
34 | } |
||
35 | |||
36 | public function __toString() |
||
37 | { |
||
38 | return $this->root(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Returns the starting point of a filesystem's abstraction, not the machine's filesystem root. |
||
43 | * It can be a project's root, or any directory you may want to work with. |
||
44 | * |
||
45 | * @return string The root path of the file system. |
||
46 | */ |
||
47 | public function root(): string |
||
48 | { |
||
49 | return $this->rootPath; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Returns the absolute path for a given relative path. |
||
54 | * |
||
55 | * @param string $relativePath The relative path to get the absolute path for. |
||
56 | * @param bool $checkExistence Whether to check if the file exists or not. Default is false. |
||
57 | * @return string The absolute path for the given relative path. |
||
58 | * @throws \InvalidArgumentException If the relative path is empty or invalid. |
||
59 | */ |
||
60 | public function absolutePathFor(string $relativePath, bool $checkExistence = false): string |
||
61 | { |
||
62 | $absolute = sprintf('%s/%s', $this->root(), $relativePath); |
||
63 | |||
64 | if ($checkExistence) { |
||
65 | $absolute = realpath($absolute); |
||
66 | if (!$absolute) { |
||
67 | throw new \InvalidArgumentException('INVALID_PATH'); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return $absolute; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Lists the contents of a directory. |
||
76 | * |
||
77 | * @param string $relativePath The relative path of the directory to list. |
||
78 | * @return array An array of filenames in the directory. |
||
79 | * @throws \InvalidArgumentException If the specified path is not a directory. |
||
80 | */ |
||
81 | public function list(string $relativePath): array |
||
82 | { |
||
83 | $absolutePath = $this->absolutePathFor($relativePath); |
||
84 | |||
85 | if (!is_dir($absolutePath)) { |
||
86 | throw new \InvalidArgumentException('RELATIVE_PATH_NOT_A_DIRECTORY'); |
||
87 | } |
||
88 | |||
89 | return array_diff(scandir($absolutePath), ['.', '..']); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Returns an array of files in the specified directory. |
||
94 | * |
||
95 | * @param string $relativePath The relative path of the directory to search. |
||
96 | * @return array An array of file names. |
||
97 | */ |
||
98 | public function files(string $relativePath): array |
||
99 | { |
||
100 | $absolutePath = $this->absolutePathFor($relativePath); |
||
101 | // Filter the list of files to include only files (not directories). |
||
102 | $files = array_filter($this->list($relativePath), function ($filename) use ($absolutePath) { |
||
103 | return is_file($absolutePath . DIRECTORY_SEPARATOR . $filename); |
||
104 | }); |
||
105 | |||
106 | return $files; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Returns an array of directories in the specified relative path. |
||
111 | * |
||
112 | * @param string $relativePath The relative path to search for directories. |
||
113 | * @return array An array of directories in the specified relative path. |
||
114 | */ |
||
115 | public function directories(string $relativePath): array |
||
116 | { |
||
117 | $absolutePath = $this->absolutePathFor($relativePath); |
||
118 | |||
119 | // Filter the list of files to include only files (not directories). |
||
120 | $files = array_filter($this->list($relativePath), function ($filename) use ($absolutePath) { |
||
121 | return is_dir($absolutePath . DIRECTORY_SEPARATOR . $filename); |
||
122 | }); |
||
123 | |||
124 | return $files; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Ensures that the specified relative path is writable. |
||
129 | * |
||
130 | * @param string $relativePath The relative path to ensure is writable. |
||
131 | * @throws \InvalidArgumentException If the path is not inside the root path, the target directory cannot be created, or the target directory is not writable. |
||
132 | * @return bool True if the path is writable, false otherwise. |
||
133 | */ |
||
134 | |||
135 | public function ensureWritablePath(string $absoluteDirectoryPath, string $filename=null): bool |
||
162 | } |
||
163 | |||
164 | public function filenames($regex = null): array |
||
165 | { |
||
166 | if (!file_exists($this->rootPath) && mkdir($this->rootPath) === false) { |
||
167 | return []; |
||
168 | } |
||
169 | |||
170 | $filenames = self::preg_scandir($this->rootPath, $regex); // ID_SEQUENCENUMBER.ext |
||
171 | if (!is_null($filenames)) { |
||
172 | sort($filenames); |
||
173 | } |
||
174 | |||
175 | return $filenames; |
||
176 | } |
||
177 | |||
178 | // previous implementation, to update |
||
179 | public function filepathes($regex = null): array |
||
180 | { |
||
181 | $filenames = $this->filenames($regex); |
||
182 | $filepathes = []; |
||
183 | foreach ($filenames as $filename) { |
||
184 | $filepathes[] = $this->absolutePathFor($filename); |
||
185 | } |
||
186 | |||
187 | return $filepathes; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Resolves a symbolic link to its target path. |
||
192 | * |
||
193 | * @param string $path The path to resolve. |
||
194 | * @throws \Exception If `readlink` fails to resolve the symbolic link. |
||
195 | * @return string The resolved path. |
||
196 | */ |
||
197 | public static function resolve_symlink($path) |
||
198 | { |
||
199 | if (is_link($path)) { |
||
200 | if (($path = readlink($path)) === false) { |
||
201 | throw new \Exception('Failed to resolve symbolic link'); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return $path; |
||
206 | } |
||
207 | /** |
||
208 | * Copies a file from the source path to the destination path. |
||
209 | * |
||
210 | * @param string $sourcePath The path of the source file. |
||
211 | * @param string $destinationPath The path of the destination file. |
||
212 | * @return bool Returns TRUE on success or FALSE on failure. |
||
213 | */ |
||
214 | public static function copy($sourcePath, $destinationPath) |
||
215 | { |
||
216 | if (file_exists($sourcePath) && is_file($sourcePath)) { |
||
217 | $destination = new FilePath($destinationPath); |
||
218 | if (file_exists($destination->dir()) && is_dir($destination->dir())) { |
||
219 | return copy($sourcePath, $destinationPath); |
||
220 | } |
||
221 | } |
||
222 | return false; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Moves a file from the source path to the destination path. |
||
227 | * |
||
228 | * @param string $sourcePath The path of the source file. |
||
229 | * @param string $destinationPath The path of the destination file. |
||
230 | * @return bool Returns TRUE on success or FALSE on failure. |
||
231 | */ |
||
232 | public static function move($sourcePath, $destinationPath) |
||
233 | { |
||
234 | if (file_exists($sourcePath) && is_file($sourcePath)) { |
||
235 | $destination = new FilePath($destinationPath); |
||
236 | if (file_exists($destination->dir()) && is_dir($destination->dir())) { |
||
237 | return rename($sourcePath, $destinationPath); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | return false; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Creates a directory with the specified path and permissions. |
||
246 | * |
||
247 | * @param string $directoryPath The path of the directory to create. |
||
248 | * @param int $permission The permissions to set for the directory. |
||
249 | * @param bool $recursive Whether to create parent directories if they don't exist. |
||
250 | * @return bool Returns TRUE on success or FALSE on failure. |
||
251 | */ |
||
252 | public static function makeDirectory($directoryPath, $permission = 0777, $recursive = true): bool |
||
253 | { |
||
254 | return mkdir($directoryPath, $permission, $recursive); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Removes a file or directory with the specified path. |
||
259 | * |
||
260 | * @param string $sourcePath The path of the file or directory to remove. |
||
261 | * @param bool $followLink Whether to follow symbolic links. |
||
262 | * @return bool Returns TRUE on success or FALSE on failure. |
||
263 | */ |
||
264 | public static function remove($sourcePath, $followLink = false): bool |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Scans a directory for files and directories that match a regular expression. |
||
281 | * |
||
282 | * @param string $directoryPath The path of the directory to scan. |
||
283 | * @param string|null $regex The regular expression to match against file and directory names. |
||
284 | * @return array|null Returns an array of file and directory names that match the regular expression, or NULL if the directory doesn't exist. |
||
285 | * @throws \Exception If the directory cannot be scanned. |
||
286 | */ |
||
287 | public static function preg_scandir($directoryPath, $regex = null) |
||
288 | { |
||
300 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.