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:
1 | <?php |
||
10 | abstract class BaseFile extends NamedElement |
||
11 | { |
||
12 | use UploadStorageTrait; |
||
13 | |||
14 | protected $actionUrl; |
||
15 | |||
16 | protected $withCredentials = false; |
||
17 | |||
18 | protected $maxFileSize; |
||
19 | |||
20 | protected $fileExtensions; |
||
21 | |||
22 | protected $uploadValidationRules = []; |
||
23 | |||
24 | protected $uploadValidationMessages = []; |
||
25 | |||
26 | abstract protected function getDefaultExtensions(); |
||
27 | |||
28 | public function getActionUrl() |
||
46 | |||
47 | public function setActionUrl($value) |
||
53 | |||
54 | /** |
||
55 | * Indicates whether or not cross-site Access-Control requests |
||
56 | * should be made using credentials |
||
57 | * |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function withCredentials() |
||
66 | |||
67 | /** |
||
68 | * The maximum size of an uploaded file in kilobytes |
||
69 | * |
||
70 | * @return int |
||
71 | */ |
||
72 | public function getMaxFileSize() |
||
80 | |||
81 | protected function getDefaultMaxFileSize() |
||
85 | |||
86 | /** |
||
87 | * The maximum size allowed for an uploaded file in kilobytes |
||
88 | * |
||
89 | * @param int $value |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setMaxFileSize(int $value) |
||
101 | |||
102 | public function getFileExtensions() |
||
110 | |||
111 | /** |
||
112 | * A list of allowable extensions that can be uploaded. |
||
113 | * |
||
114 | * @param string $value |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function setFileExtensions(string $value) |
||
126 | |||
127 | public function toArray() |
||
135 | |||
136 | /** |
||
137 | * Save file to storage |
||
138 | * |
||
139 | * @param \Illuminate\Http\UploadedFile $file |
||
140 | * |
||
141 | * @return array |
||
142 | * @throws \Illuminate\Validation\ValidationException |
||
143 | */ |
||
144 | public function saveFile(UploadedFile $file) |
||
161 | |||
162 | /** |
||
163 | * Get file info(name,path,url) |
||
164 | * |
||
165 | * @param $path |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | protected function getFileInfo($path) |
||
177 | |||
178 | public function addValidationRule($rule, $message = null) |
||
197 | |||
198 | View Code Duplication | protected function addUploadValidationRule($rule, $message = null) |
|
208 | |||
209 | protected function addUploadValidationMessage($rule, $message) |
||
217 | |||
218 | protected function getUploadValidationRules() |
||
227 | |||
228 | /** |
||
229 | * Get default validation rules |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | protected function getDefaultUploadValidationRules() |
||
242 | |||
243 | /** |
||
244 | * Get validation messages |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | protected function getUploadValidationMessages() |
||
252 | |||
253 | /** |
||
254 | * Get validation custom attributes |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | protected function getUploadValidationTitles() |
||
262 | } |
||
263 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.