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 |
||
16 | abstract class BaseFile extends NamedElement |
||
17 | { |
||
18 | use UploadStorageTrait; |
||
19 | |||
20 | /** |
||
21 | * @var |
||
22 | */ |
||
23 | protected $actionUrl; |
||
24 | |||
25 | /** |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $withCredentials = false; |
||
29 | |||
30 | /** |
||
31 | * @var |
||
32 | */ |
||
33 | protected $maxFileSize; |
||
34 | |||
35 | /** |
||
36 | * @var |
||
37 | */ |
||
38 | protected $fileExtensions; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $uploadValidationRules = []; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $uploadValidationMessages = []; |
||
49 | |||
50 | /** |
||
51 | * @return mixed |
||
52 | */ |
||
53 | abstract protected function getDefaultExtensions(); |
||
54 | |||
55 | abstract protected function mutateValueAsPath(); |
||
56 | |||
57 | public function __construct(string $name, string $title) |
||
63 | |||
64 | /** |
||
65 | * @return string |
||
66 | */ |
||
67 | public function getActionUrl() |
||
85 | |||
86 | /** |
||
87 | * @param $value |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function setActionUrl($value) |
||
96 | |||
97 | /** |
||
98 | * Indicates whether or not cross-site Access-Control requests |
||
99 | * should be made using credentials |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function withCredentials() |
||
109 | |||
110 | /** |
||
111 | * The maximum size of an uploaded file in kilobytes |
||
112 | * |
||
113 | * @return int |
||
114 | */ |
||
115 | public function getMaxFileSize() |
||
123 | |||
124 | /** |
||
125 | * @return float|int |
||
126 | */ |
||
127 | protected function getDefaultMaxFileSize() |
||
131 | |||
132 | /** |
||
133 | * The maximum size allowed for an uploaded file in kilobytes |
||
134 | * |
||
135 | * @param int $value |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function setMaxFileSize(int $value) |
||
147 | |||
148 | /** |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function getFileExtensions() |
||
159 | |||
160 | /** |
||
161 | * A list of allowable extensions that can be uploaded. |
||
162 | * |
||
163 | * @param string $value |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function setFileExtensions(string $value) |
||
175 | |||
176 | /** |
||
177 | * @return array |
||
178 | */ |
||
179 | public function toArray() |
||
187 | |||
188 | /** |
||
189 | * Save file to storage |
||
190 | * |
||
191 | * @param \Illuminate\Http\UploadedFile $file |
||
192 | * |
||
193 | * @return array |
||
194 | * @throws \Illuminate\Validation\ValidationException |
||
195 | */ |
||
196 | public function saveFile(UploadedFile $file) |
||
213 | |||
214 | /** |
||
215 | * Get file info(name,path,url) |
||
216 | * |
||
217 | * @param $path |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | protected function getFileInfo($path) |
||
229 | |||
230 | /** |
||
231 | * @param $rule |
||
232 | * @param null $message |
||
233 | * @return $this|\Sco\Admin\Form\Elements\BaseFile |
||
234 | */ |
||
235 | public function addValidationRule($rule, $message = null) |
||
254 | |||
255 | /** |
||
256 | * @param $rule |
||
257 | * @param null $message |
||
258 | * @return $this|\Sco\Admin\Form\Elements\BaseFile |
||
259 | */ |
||
260 | View Code Duplication | protected function addUploadValidationRule($rule, $message = null) |
|
270 | |||
271 | /** |
||
272 | * @param $rule |
||
273 | * @param $message |
||
274 | * @return $this |
||
275 | */ |
||
276 | protected function addUploadValidationMessage($rule, $message) |
||
284 | |||
285 | /** |
||
286 | * @return array |
||
287 | */ |
||
288 | protected function getUploadValidationRules() |
||
297 | |||
298 | /** |
||
299 | * Get default validation rules |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | protected function getDefaultUploadValidationRules() |
||
312 | |||
313 | /** |
||
314 | * Get validation messages |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | protected function getUploadValidationMessages() |
||
322 | |||
323 | /** |
||
324 | * Get validation custom attributes |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | protected function getUploadValidationTitles() |
||
332 | } |
||
333 |
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.