Complex classes like FileManager 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 FileManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class FileManager { |
||
12 | |||
13 | protected $storage; |
||
14 | protected $options = []; |
||
15 | |||
16 | protected $storage_path = ''; |
||
17 | protected $public_path = ''; |
||
18 | |||
19 | protected $publishers = []; |
||
20 | |||
21 | /** |
||
22 | * |
||
23 | */ |
||
24 | public function __construct(FileManagerStorage $storage, array $options = []) { |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | */ |
||
35 | public function addPublisher($name, callable $callback) { |
||
38 | |||
39 | /** |
||
40 | * |
||
41 | */ |
||
42 | public function publish($publisher, ManagedFile ...$files) { |
||
58 | |||
59 | /** |
||
60 | * |
||
61 | */ |
||
62 | public function resolveWebPath($publisher, $path) { |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | */ |
||
71 | public function resolvePublicPath($publisher, $path) { |
||
74 | |||
75 | /** |
||
76 | * |
||
77 | */ |
||
78 | public function resolveStoragePath($path) { |
||
81 | |||
82 | /** |
||
83 | * |
||
84 | */ |
||
85 | public function getFilePath($filename, $destination = null) { |
||
88 | |||
89 | /** |
||
90 | * |
||
91 | */ |
||
92 | public function makeNewFilePath($filename, $destination = null) { |
||
103 | |||
104 | /** |
||
105 | * |
||
106 | */ |
||
107 | public function appendFilePathUniqueness($filepath, $index) { |
||
111 | |||
112 | /** |
||
113 | * |
||
114 | */ |
||
115 | public function cleanFileName($filename) { |
||
125 | |||
126 | /** |
||
127 | * |
||
128 | */ |
||
129 | public function takeExtension(&$filename) { |
||
136 | |||
137 | /** |
||
138 | * |
||
139 | */ |
||
140 | public function getExtension($filename) { |
||
150 | |||
151 | /** |
||
152 | * |
||
153 | */ |
||
154 | public function saveFile(UploadedFile $uploaded, $destination = null) { |
||
163 | |||
164 | /** |
||
165 | * |
||
166 | */ |
||
167 | protected function persistFile(ManagedFile $managed, UploadedFile $uploaded) { |
||
178 | |||
179 | /** |
||
180 | * |
||
181 | */ |
||
182 | public function chmodFile($path) { |
||
185 | |||
186 | /** |
||
187 | * |
||
188 | */ |
||
189 | public function chmodDir($path) { |
||
192 | |||
193 | /** |
||
194 | * |
||
195 | */ |
||
196 | public function prepPublicDir($path) { |
||
199 | |||
200 | /** |
||
201 | * |
||
202 | */ |
||
203 | public function prepDir($root, $path) { |
||
214 | |||
215 | /** |
||
216 | * |
||
217 | */ |
||
218 | protected function createManagedFileFromUpload(UploadedFile $file, $destination) { |
||
224 | |||
225 | /** |
||
226 | * |
||
227 | */ |
||
228 | protected function createManagedFileFromStorage(array $params) { |
||
231 | |||
232 | /** |
||
233 | * |
||
234 | */ |
||
235 | public function getUsageCount(ManagedFile $file) { |
||
238 | |||
239 | /** |
||
240 | * |
||
241 | */ |
||
242 | public function getUsages(ManagedFile $file) { |
||
248 | |||
249 | /** |
||
250 | * |
||
251 | */ |
||
252 | public function findByPath($path) { |
||
258 | |||
259 | /** |
||
260 | * |
||
261 | */ |
||
262 | public function findByPathOrFail($path) { |
||
269 | |||
270 | /** |
||
271 | * |
||
272 | */ |
||
273 | public function find($id) { |
||
279 | |||
280 | /** |
||
281 | * |
||
282 | */ |
||
283 | public function findOrFail($id) { |
||
290 | |||
291 | /** |
||
292 | * |
||
293 | */ |
||
294 | public function findAll(array $conditions = []) { |
||
300 | |||
301 | /** |
||
302 | * |
||
303 | */ |
||
304 | public function addUsage(FileUsageContract $usage, ManagedFile $file) { |
||
308 | |||
309 | /** |
||
310 | * |
||
311 | */ |
||
312 | public function removeUsage(FileUsageContract $usage, ManagedFile $file) { |
||
316 | |||
317 | /** |
||
318 | * |
||
319 | */ |
||
320 | public function replaceUsage(FileUsageContract $usage, ManagedFile $file) { |
||
326 | |||
327 | /** |
||
328 | * |
||
329 | */ |
||
330 | public function cleanUsage() { |
||
333 | |||
334 | /** |
||
335 | * |
||
336 | */ |
||
337 | public function addUsages(FileUsageContract $usage, ManagedFile ...$files) { |
||
340 | |||
341 | /** |
||
342 | * |
||
343 | */ |
||
344 | public function removeUsages(FileUsageContract $usage, ManagedFile ...$files) { |
||
347 | |||
348 | /** |
||
349 | * |
||
350 | */ |
||
351 | public function replaceUsages(FileUsageContract $usage, ManagedFile ...$files) { |
||
354 | |||
355 | } |
||
356 |
If you suppress an error, we recommend checking for the error condition explicitly: