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 |
||
| 10 | trait UploadField |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Upload directory. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $directory = ''; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * File name. |
||
| 21 | * |
||
| 22 | * @var null |
||
| 23 | */ |
||
| 24 | protected $name = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Storage instance. |
||
| 28 | * |
||
| 29 | * @var \Illuminate\Filesystem\Filesystem |
||
| 30 | */ |
||
| 31 | protected $storage = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * If use unique name to store upload file. |
||
| 35 | * |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $useUniqueName = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * If use sequence name to store upload file. |
||
| 42 | * |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $useSequenceName = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Retain file when delete record from DB. |
||
| 49 | * |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $retainable = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $downloadable = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Configuration for setting up file actions for newly selected file thumbnails in the preview window. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $fileActionSettings = [ |
||
| 65 | 'showRemove' => false, |
||
| 66 | 'showDrag' => false, |
||
| 67 | ]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Controls the storage permission. Could be 'private' or 'public'. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $storagePermission; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $fileTypes = [ |
||
| 80 | 'image' => '/^(gif|png|jpe?g|svg)$/i', |
||
| 81 | 'html' => '/^(htm|html)$/i', |
||
| 82 | 'office' => '/^(docx?|xlsx?|pptx?|pps|potx?)$/i', |
||
| 83 | 'gdocs' => '/^(docx?|xlsx?|pptx?|pps|potx?|rtf|ods|odt|pages|ai|dxf|ttf|tiff?|wmf|e?ps)$/i', |
||
| 84 | 'text' => '/^(txt|md|csv|nfo|ini|json|php|js|css|ts|sql)$/i', |
||
| 85 | 'video' => '/^(og?|mp4|webm|mp?g|mov|3gp)$/i', |
||
| 86 | 'audio' => '/^(og?|mp3|mp?g|wav)$/i', |
||
| 87 | 'pdf' => '/^(pdf)$/i', |
||
| 88 | 'flash' => '/^(swf)$/i', |
||
| 89 | ]; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Initialize the storage instance. |
||
| 93 | * |
||
| 94 | * @return void. |
||
| 95 | */ |
||
| 96 | protected function initStorage() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Set default options form image field. |
||
| 103 | * |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | protected function setupDefaultOptions() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set preview options form image field. |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | protected function setupPreviewOptions() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return array|bool |
||
| 148 | */ |
||
| 149 | protected function guessPreviewType($file) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Indicates if the underlying field is downloadable. |
||
| 176 | * |
||
| 177 | * @param bool $downloadable |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | public function downloadable($downloadable = true) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Allow use to remove file. |
||
| 189 | * |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | public function removable() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Indicates if the underlying field is retainable. |
||
| 201 | * |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | public function retainable($retainable = true) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set options for file-upload plugin. |
||
| 213 | * |
||
| 214 | * @param array $options |
||
| 215 | * |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function options($options = []) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set disk for storage. |
||
| 227 | * |
||
| 228 | * @param string $disk Disks defined in `config/filesystems.php`. |
||
| 229 | * |
||
| 230 | * @throws \Exception |
||
| 231 | * |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function disk($disk) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Specify the directory and name for upload file. |
||
| 256 | * |
||
| 257 | * @param string $directory |
||
| 258 | * @param null|string $name |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function move($directory, $name = null) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Specify the directory upload file. |
||
| 273 | * |
||
| 274 | * @param string $dir |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function dir($dir) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set name of store name. |
||
| 289 | * |
||
| 290 | * @param string|callable $name |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function name($name) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Use unique name for store upload file. |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function uniqueName() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Use sequence name for store upload file. |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function sequenceName() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get store name of upload file. |
||
| 329 | * |
||
| 330 | * @param UploadedFile $file |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | protected function getStoreName(UploadedFile $file) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get directory for store file. |
||
| 357 | * |
||
| 358 | * @return mixed|string |
||
| 359 | */ |
||
| 360 | public function getDirectory() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Upload file and delete original file. |
||
| 371 | * |
||
| 372 | * @param UploadedFile $file |
||
| 373 | * |
||
| 374 | * @return mixed |
||
| 375 | */ |
||
| 376 | protected function upload(UploadedFile $file) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * If name already exists, rename it. |
||
| 389 | * |
||
| 390 | * @param $file |
||
| 391 | * |
||
| 392 | * @return void |
||
| 393 | */ |
||
| 394 | public function renameIfExists(UploadedFile $file) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get file visit url. |
||
| 403 | * |
||
| 404 | * @param $path |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function objectUrl($path) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Generate a unique name for uploaded file. |
||
| 423 | * |
||
| 424 | * @param UploadedFile $file |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | protected function generateUniqueName(UploadedFile $file) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Generate a sequence name for uploaded file. |
||
| 435 | * |
||
| 436 | * @param UploadedFile $file |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | protected function generateSequenceName(UploadedFile $file) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Destroy original files. |
||
| 457 | * |
||
| 458 | * @return void. |
||
| 459 | */ |
||
| 460 | public function destroy() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Set file permission when stored into storage. |
||
| 469 | * |
||
| 470 | * @param string $permission |
||
| 471 | * |
||
| 472 | * @return $this |
||
| 473 | */ |
||
| 474 | public function storagePermission($permission) |
||
| 480 | } |
||
| 481 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: