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 |
||
| 10 | class File extends NamedElement |
||
| 11 | { |
||
| 12 | protected $type = 'file'; |
||
| 13 | |||
| 14 | protected $actionUrl; |
||
| 15 | |||
| 16 | protected $multiSelect = false; |
||
| 17 | |||
| 18 | protected $multiFile = true; |
||
| 19 | |||
| 20 | protected $showFileList = true; |
||
| 21 | |||
| 22 | protected $withCredentials = false; |
||
| 23 | |||
| 24 | protected $maxFileSize; |
||
| 25 | |||
| 26 | protected $fileUploadsLimit = 0; |
||
| 27 | |||
| 28 | protected $fileExtensions; |
||
| 29 | |||
| 30 | protected $listType = 'text'; |
||
| 31 | |||
| 32 | protected $disk; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string|\Closure|null |
||
| 36 | */ |
||
| 37 | protected $uploadPath; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \Closure|null |
||
| 41 | */ |
||
| 42 | protected $uploadFileNameRule; |
||
| 43 | |||
| 44 | protected $uploadValidationRules = ['bail', 'file']; |
||
| 45 | |||
| 46 | protected $uploadValidationMessages = []; |
||
| 47 | |||
| 48 | public function getValue() |
||
| 60 | |||
| 61 | public function getActionUrl() |
||
| 80 | |||
| 81 | public function setActionUrl($value) |
||
| 87 | |||
| 88 | public function isMultiSelect() |
||
| 92 | |||
| 93 | public function enableMultiSelect() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Show file list |
||
| 102 | * |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function disableFileList() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Indicates whether or not cross-site Access-Control requests |
||
| 114 | * should be made using credentials |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function withCredentials() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The maximum size of an uploaded file in bytes |
||
| 127 | * If didn't set maximum size, return maximum size as configured in php.ini. |
||
| 128 | * |
||
| 129 | * @return int |
||
| 130 | */ |
||
| 131 | public function getMaxFileSize() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * The maximum size allowed for an uploaded file in bytes |
||
| 143 | * |
||
| 144 | * @param int $value |
||
| 145 | * |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function setMaxFileSize($value) |
||
| 154 | |||
| 155 | public function getFileExtensions() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * A list of allowable extensions that can be uploaded. |
||
| 167 | * |
||
| 168 | * @param string $value |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function setFileExtensions($value) |
||
| 178 | |||
| 179 | protected function getDefaultExtensions() |
||
| 183 | |||
| 184 | public function getFileUploadsLimit() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * The maximum number of files that can be uploaded. |
||
| 191 | * |
||
| 192 | * @param int $value |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function setFileUploadsLimit($value) |
||
| 202 | |||
| 203 | public function getListType() |
||
| 207 | |||
| 208 | public function toArray() |
||
| 220 | |||
| 221 | public function getDisk() |
||
| 229 | |||
| 230 | public function setDisk($value) |
||
| 236 | |||
| 237 | protected function getDefaultUploadPath(UploadedFile $file) |
||
| 241 | |||
| 242 | public function getUploadPath(UploadedFile $file) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * The path of file save |
||
| 256 | * |
||
| 257 | * @param string|\Closure $value |
||
| 258 | * |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function setUploadPath($value) |
||
| 267 | |||
| 268 | public function getUploadFileName(UploadedFile $file) |
||
| 276 | |||
| 277 | protected function getDefaultFileName(UploadedFile $file) |
||
| 282 | |||
| 283 | public function setUploadFileNameRule(\Closure $value) |
||
| 289 | |||
| 290 | public function saveFile(UploadedFile $file) |
||
| 307 | |||
| 308 | protected function prepareValue($value) |
||
| 315 | |||
| 316 | protected function existsFile($path) |
||
| 320 | |||
| 321 | protected function getFileUrl($path) |
||
| 325 | |||
| 326 | protected function getFileInfo($path) |
||
| 334 | |||
| 335 | public function addValidationRule($rule, $message = null) |
||
| 345 | |||
| 346 | public function addUploadValidationRule($rule, $message = null) |
||
| 355 | |||
| 356 | View Code Duplication | public function addUploadValidationMessage($rule, $message) |
|
| 366 | |||
| 367 | public function getUploadValidationRules() |
||
| 371 | |||
| 372 | public function getUploadValidationMessages() |
||
| 376 | |||
| 377 | public function getUploadValidationTitles() |
||
| 381 | } |
||
| 382 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.