Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class File extends Field |
||
| 13 | { |
||
| 14 | const ACTION_KEEP = 0; |
||
| 15 | const ACTION_REMOVE = 1; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Upload directory. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $directory = ''; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * File name. |
||
| 26 | * |
||
| 27 | * @var null |
||
| 28 | */ |
||
| 29 | protected $name = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Options for file-upload plugin. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $options = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Storage instance. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $storage = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Css. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected static $css = [ |
||
| 51 | '/packages/admin/bootstrap-fileinput/css/fileinput.min.css', |
||
| 52 | ]; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Js. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected static $js = [ |
||
| 60 | '/packages/admin/bootstrap-fileinput/js/plugins/canvas-to-blob.min.js', |
||
| 61 | '/packages/admin/bootstrap-fileinput/js/fileinput.min.js', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * If use unique name to store upload file. |
||
| 66 | * |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | protected $useUniqueName = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Use multiple upload. |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $multiple = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Create a new File instance. |
||
| 80 | * |
||
| 81 | * @param string $column |
||
| 82 | * @param array $arguments |
||
| 83 | */ |
||
| 84 | public function __construct($column, $arguments = []) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Initialize the storage instance. |
||
| 94 | * |
||
| 95 | * @return void. |
||
|
|
|||
| 96 | */ |
||
| 97 | protected function initStorage() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Initialize file-upload plugin. |
||
| 104 | * |
||
| 105 | * @return void. |
||
| 106 | */ |
||
| 107 | protected function initOptions() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set options for file-upload plugin. |
||
| 118 | * |
||
| 119 | * @param array $options |
||
| 120 | * @return $this |
||
| 121 | */ |
||
| 122 | public function options($options = []) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set field as mulitple upload. |
||
| 131 | * |
||
| 132 | * @return $this |
||
| 133 | */ |
||
| 134 | public function multiple() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Default store path for file upload. |
||
| 145 | * |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | public function defaultStorePath() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Specify the directory and name for uplaod file. |
||
| 155 | * |
||
| 156 | * @param string $directory |
||
| 157 | * @param null|string $name |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function move($directory, $name = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function validate(array $input) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Hydrate the files array. |
||
| 196 | * |
||
| 197 | * @param array $value |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | protected function hydrateFiles(array $value) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set name of store name. |
||
| 214 | * |
||
| 215 | * @param string|callable $name |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function name($name) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Use unique name for store upload file. |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | public function uniqueName() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Prepare for saving. |
||
| 240 | * |
||
| 241 | * @param UploadedFile|array $files |
||
| 242 | * |
||
| 243 | * @return mixed|string |
||
| 244 | */ |
||
| 245 | public function prepare($files) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Prepare for single file. |
||
| 266 | * |
||
| 267 | * @param UploadedFile $file |
||
| 268 | * |
||
| 269 | * @return mixed|string |
||
| 270 | */ |
||
| 271 | protected function prepareForSingle(UploadedFile $file = null) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get store name of upload file. |
||
| 282 | * |
||
| 283 | * @param UploadedFile $file |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | protected function getStoreName(UploadedFile $file) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Upload file and delete original file. |
||
| 308 | * |
||
| 309 | * @param UploadedFile $file |
||
| 310 | * |
||
| 311 | * @return mixed |
||
| 312 | */ |
||
| 313 | protected function uploadAndDeleteOriginal(UploadedFile $file) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Preview html for file-upload plugin. |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | View Code Duplication | protected function preview() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Preview html for file-upload plugin. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | protected function buildPreviewItem($file) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get file visit url. |
||
| 365 | * |
||
| 366 | * @param $path |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function objectUrl($path) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Initialize the caption. |
||
| 380 | * |
||
| 381 | * @param string $caption |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | protected function initialCaption($caption) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Render file upload field. |
||
| 403 | * |
||
| 404 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 405 | */ |
||
| 406 | public function render() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * If is delete request then delete original image. |
||
| 431 | * |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | public function isDeleteRequest() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Generate a unique name for uploaded file. |
||
| 449 | * |
||
| 450 | * @param UploadedFile $file |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | protected function generateUniqueName(UploadedFile $file) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * If name already exists, rename it. |
||
| 461 | * |
||
| 462 | * @param $file |
||
| 463 | * |
||
| 464 | * @return void |
||
| 465 | */ |
||
| 466 | public function renameIfExists(UploadedFile $file) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Destroy original files. |
||
| 475 | * |
||
| 476 | * @return void. |
||
| 477 | */ |
||
| 478 | View Code Duplication | public function destroy() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Destroy single original file. |
||
| 491 | * |
||
| 492 | * @param string $item |
||
| 493 | */ |
||
| 494 | protected function destroyItem($item) |
||
| 500 | } |
||
| 501 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.