Complex classes like UploadField 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 UploadField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | trait UploadField |
||
12 | { |
||
13 | /** |
||
14 | * Upload directory. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $directory = ''; |
||
19 | |||
20 | /** |
||
21 | * File name. |
||
22 | * |
||
23 | * @var null |
||
24 | */ |
||
25 | protected $name = null; |
||
26 | |||
27 | /** |
||
28 | * Storage instance. |
||
29 | * |
||
30 | * @var \Illuminate\Filesystem\Filesystem |
||
31 | */ |
||
32 | protected $storage = ''; |
||
33 | |||
34 | /** |
||
35 | * If use unique name to store upload file. |
||
36 | * |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $useUniqueName = false; |
||
40 | |||
41 | /** |
||
42 | * If use sequence name to store upload file. |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $useSequenceName = false; |
||
47 | |||
48 | /** |
||
49 | * Retain file when delete record from DB. |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $retainable = false; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $downloadable = true; |
||
59 | |||
60 | /** |
||
61 | * Configuration for setting up file actions for newly selected file thumbnails in the preview window. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $fileActionSettings = [ |
||
66 | 'showRemove' => false, |
||
67 | 'showDrag' => false, |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * Controls the storage permission. Could be 'private' or 'public'. |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $storagePermission; |
||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $fileTypes = [ |
||
81 | 'image' => '/^(gif|png|jpe?g|svg|webp)$/i', |
||
82 | 'html' => '/^(htm|html)$/i', |
||
83 | 'office' => '/^(docx?|xlsx?|pptx?|pps|potx?)$/i', |
||
84 | 'gdocs' => '/^(docx?|xlsx?|pptx?|pps|potx?|rtf|ods|odt|pages|ai|dxf|ttf|tiff?|wmf|e?ps)$/i', |
||
85 | 'text' => '/^(txt|md|csv|nfo|ini|json|php|js|css|ts|sql)$/i', |
||
86 | 'video' => '/^(og?|mp4|webm|mp?g|mov|3gp)$/i', |
||
87 | 'audio' => '/^(og?|mp3|mp?g|wav)$/i', |
||
88 | 'pdf' => '/^(pdf)$/i', |
||
89 | 'flash' => '/^(swf)$/i', |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $pathColumn; |
||
96 | |||
97 | /** |
||
98 | * Initialize the storage instance. |
||
99 | * |
||
100 | * @return void. |
||
101 | */ |
||
102 | protected function initStorage() |
||
106 | |||
107 | /** |
||
108 | * Set default options form image field. |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | protected function setupDefaultOptions() |
||
140 | |||
141 | /** |
||
142 | * Set preview options form image field. |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | protected function setupPreviewOptions() |
||
152 | |||
153 | /** |
||
154 | * @return array|bool |
||
155 | */ |
||
156 | protected function guessPreviewType($file) |
||
184 | |||
185 | /** |
||
186 | * Indicates if the underlying field is downloadable. |
||
187 | * |
||
188 | * @param bool $downloadable |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function downloadable($downloadable = true) |
||
198 | |||
199 | /** |
||
200 | * Allow use to remove file. |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function removable() |
||
210 | |||
211 | /** |
||
212 | * Indicates if the underlying field is retainable. |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function retainable($retainable = true) |
||
222 | |||
223 | /** |
||
224 | * Set options for file-upload plugin. |
||
225 | * |
||
226 | * @param array $options |
||
227 | * |
||
228 | * @return $this |
||
229 | */ |
||
230 | public function options($options = []) |
||
236 | |||
237 | /** |
||
238 | * Set disk for storage. |
||
239 | * |
||
240 | * @param string $disk Disks defined in `config/filesystems.php`. |
||
241 | * |
||
242 | * @throws \Exception |
||
243 | * |
||
244 | * @return $this |
||
245 | */ |
||
246 | public function disk($disk) |
||
265 | |||
266 | /** |
||
267 | * Specify the directory and name for upload file. |
||
268 | * |
||
269 | * @param string $directory |
||
270 | * @param null|string $name |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function move($directory, $name = null) |
||
282 | |||
283 | /** |
||
284 | * Specify the directory upload file. |
||
285 | * |
||
286 | * @param string $dir |
||
287 | * |
||
288 | * @return $this |
||
289 | */ |
||
290 | public function dir($dir) |
||
298 | |||
299 | /** |
||
300 | * Set name of store name. |
||
301 | * |
||
302 | * @param string|callable $name |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function name($name) |
||
314 | |||
315 | /** |
||
316 | * Use unique name for store upload file. |
||
317 | * |
||
318 | * @return $this |
||
319 | */ |
||
320 | public function uniqueName() |
||
326 | |||
327 | /** |
||
328 | * Use sequence name for store upload file. |
||
329 | * |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function sequenceName() |
||
338 | |||
339 | /** |
||
340 | * Get store name of upload file. |
||
341 | * |
||
342 | * @param UploadedFile $file |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | protected function getStoreName(UploadedFile $file) |
||
366 | |||
367 | /** |
||
368 | * Get directory for store file. |
||
369 | * |
||
370 | * @return mixed|string |
||
371 | */ |
||
372 | public function getDirectory() |
||
380 | |||
381 | /** |
||
382 | * Set path column in has-many related model. |
||
383 | * |
||
384 | * @param string $column |
||
385 | * @return $this |
||
386 | */ |
||
387 | public function pathColumn($column = 'path') |
||
393 | |||
394 | /** |
||
395 | * Upload file and delete original file. |
||
396 | * |
||
397 | * @param UploadedFile $file |
||
398 | * |
||
399 | * @return mixed |
||
400 | */ |
||
401 | protected function upload(UploadedFile $file) |
||
411 | |||
412 | /** |
||
413 | * If name already exists, rename it. |
||
414 | * |
||
415 | * @param $file |
||
416 | * |
||
417 | * @return void |
||
418 | */ |
||
419 | public function renameIfExists(UploadedFile $file) |
||
425 | |||
426 | /** |
||
427 | * Get file visit url. |
||
428 | * |
||
429 | * @param $path |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | public function objectUrl($path) |
||
449 | |||
450 | /** |
||
451 | * Generate a unique name for uploaded file. |
||
452 | * |
||
453 | * @param UploadedFile $file |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | protected function generateUniqueName(UploadedFile $file) |
||
461 | |||
462 | /** |
||
463 | * Generate a sequence name for uploaded file. |
||
464 | * |
||
465 | * @param UploadedFile $file |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | protected function generateSequenceName(UploadedFile $file) |
||
483 | |||
484 | /** |
||
485 | * Destroy original files. |
||
486 | * |
||
487 | * @return void. |
||
488 | */ |
||
489 | public function destroy() |
||
503 | |||
504 | /** |
||
505 | * Set file permission when stored into storage. |
||
506 | * |
||
507 | * @param string $permission |
||
508 | * |
||
509 | * @return $this |
||
510 | */ |
||
511 | public function storagePermission($permission) |
||
517 | } |
||
518 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: