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 MultipleFile 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 MultipleFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class MultipleFile extends Field |
||
11 | { |
||
12 | use UploadField; |
||
13 | |||
14 | /** |
||
15 | * Css. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected static $css = [ |
||
20 | '/vendor/laravel-admin/bootstrap-fileinput/css/fileinput.min.css?v=4.5.2', |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * Js. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected static $js = [ |
||
29 | '/vendor/laravel-admin/bootstrap-fileinput/js/plugins/canvas-to-blob.min.js', |
||
30 | '/vendor/laravel-admin/bootstrap-fileinput/js/fileinput.min.js?v=4.5.2', |
||
31 | '/vendor/laravel-admin/bootstrap-fileinput/js/plugins/sortable.min.js?v=4.5.2', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * Create a new File instance. |
||
36 | * |
||
37 | * @param string $column |
||
38 | * @param array $arguments |
||
39 | */ |
||
40 | public function __construct($column, $arguments = []) |
||
46 | |||
47 | /** |
||
48 | * Default directory for file to upload. |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | public function defaultDirectory() |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function getValidator(array $input) |
||
86 | |||
87 | /** |
||
88 | * Hydrate the files array. |
||
89 | * |
||
90 | * @param array $value |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | protected function hydrateFiles(array $value) |
||
109 | |||
110 | /** |
||
111 | * Sort files. |
||
112 | * |
||
113 | * @param string $order |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | protected function sortFiles($order) |
||
130 | |||
131 | /** |
||
132 | * Prepare for saving. |
||
133 | * |
||
134 | * @param UploadedFile|array $files |
||
135 | * |
||
136 | * @return mixed|string |
||
137 | */ |
||
138 | public function prepare($files) |
||
164 | |||
165 | /** |
||
166 | * @return array|mixed |
||
167 | */ |
||
168 | public function original() |
||
176 | |||
177 | /** |
||
178 | * Prepare for each file. |
||
179 | * |
||
180 | * @param UploadedFile $file |
||
181 | * |
||
182 | * @return mixed|string |
||
183 | */ |
||
184 | View Code Duplication | protected function prepareForeach(UploadedFile $file = null) |
|
192 | |||
193 | /** |
||
194 | * Preview html for file-upload plugin. |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | protected function preview() |
||
204 | |||
205 | /** |
||
206 | * Initialize the caption. |
||
207 | * |
||
208 | * @param array $caption |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | protected function initialCaption($caption) |
||
222 | |||
223 | /** |
||
224 | * @return array |
||
225 | */ |
||
226 | protected function initialPreviewConfig() |
||
249 | |||
250 | /** |
||
251 | * Get related model key name. |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | protected function getRelatedKeyName() |
||
263 | |||
264 | /** |
||
265 | * Allow to sort files. |
||
266 | * |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function sortable() |
||
275 | |||
276 | /** |
||
277 | * @param string $options |
||
278 | */ |
||
279 | protected function setupScripts($options) |
||
338 | |||
339 | /** |
||
340 | * Render file upload field. |
||
341 | * |
||
342 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
343 | */ |
||
344 | public function render() |
||
361 | |||
362 | /** |
||
363 | * Destroy original files. |
||
364 | * |
||
365 | * @param string $key |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | public function destroy($key) |
||
383 | |||
384 | /** |
||
385 | * Destroy original files from hasmany related model. |
||
386 | * |
||
387 | * @param integer $key |
||
388 | * @return array |
||
389 | */ |
||
390 | public function destroyFromHasMany($key) |
||
404 | } |
||
405 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.