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 = []; |
||
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 | public function isShowFileList() |
||
104 | |||
105 | /** |
||
106 | * Disable file list |
||
107 | * |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function disableFileList() |
||
116 | |||
117 | /** |
||
118 | * Indicates whether or not cross-site Access-Control requests |
||
119 | * should be made using credentials |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function withCredentials() |
||
129 | |||
130 | /** |
||
131 | * The maximum size of an uploaded file in kilobytes |
||
132 | * |
||
133 | * @return int |
||
134 | */ |
||
135 | public function getMaxFileSize() |
||
143 | |||
144 | protected function getDefaultMaxFileSize() |
||
148 | |||
149 | /** |
||
150 | * The maximum size allowed for an uploaded file in kilobytes |
||
151 | * |
||
152 | * @param int $value |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setMaxFileSize($value) |
||
164 | |||
165 | public function getFileExtensions() |
||
172 | |||
173 | /** |
||
174 | * A list of allowable extensions that can be uploaded. |
||
175 | * |
||
176 | * @param string $value |
||
177 | * |
||
178 | * @return $this |
||
179 | */ |
||
180 | public function setFileExtensions($value) |
||
188 | |||
189 | protected function getDefaultExtensions() |
||
193 | |||
194 | public function getFileUploadsLimit() |
||
198 | |||
199 | /** |
||
200 | * The maximum number of files that can be uploaded. |
||
201 | * |
||
202 | * @param int $value |
||
203 | * |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function setFileUploadsLimit($value) |
||
212 | |||
213 | public function getListType() |
||
217 | |||
218 | public function toArray() |
||
230 | |||
231 | public function getDisk() |
||
239 | |||
240 | public function setDisk($value) |
||
246 | |||
247 | protected function getDefaultUploadPath(UploadedFile $file) |
||
251 | |||
252 | public function getUploadPath(UploadedFile $file) |
||
263 | |||
264 | /** |
||
265 | * The path of file save |
||
266 | * |
||
267 | * @param string|\Closure $value |
||
268 | * |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function setUploadPath($value) |
||
277 | |||
278 | /** |
||
279 | * Get a filename for the upload file. |
||
280 | * |
||
281 | * @param \Illuminate\Http\UploadedFile $file |
||
282 | * |
||
283 | * @return mixed|string |
||
284 | */ |
||
285 | public function getUploadFileName(UploadedFile $file) |
||
293 | |||
294 | protected function getDefaultFileName(UploadedFile $file) |
||
298 | |||
299 | /** |
||
300 | * Set the generation rule for the filename of the uploaded file |
||
301 | * |
||
302 | * @param \Closure $value |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function setUploadFileNameRule(\Closure $value) |
||
312 | |||
313 | public function saveFile(UploadedFile $file) |
||
330 | |||
331 | protected function prepareValue($value) |
||
338 | |||
339 | protected function existsFile($path) |
||
343 | |||
344 | protected function getFileUrl($path) |
||
348 | |||
349 | protected function getFileInfo($path) |
||
357 | |||
358 | public function addValidationRule($rule, $message = null) |
||
371 | |||
372 | View Code Duplication | protected function addUploadValidationRule($rule, $message = null) |
|
381 | |||
382 | protected function addUploadValidationMessage($rule, $message) |
||
390 | |||
391 | protected function getUploadValidationRules() |
||
400 | |||
401 | protected function getDefaultUploadValidationRules() |
||
410 | |||
411 | protected function getUploadValidationMessages() |
||
415 | |||
416 | protected function getUploadValidationTitles() |
||
420 | } |
||
421 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.