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) |
||
82 | |||
83 | /** |
||
84 | * Hydrate the files array. |
||
85 | * |
||
86 | * @param array $value |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | protected function hydrateFiles(array $value) |
||
105 | |||
106 | /** |
||
107 | * Sort files. |
||
108 | * |
||
109 | * @param string $order |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | protected function sortFiles($order) |
||
126 | |||
127 | /** |
||
128 | * Prepare for saving. |
||
129 | * |
||
130 | * @param UploadedFile|array $files |
||
131 | * |
||
132 | * @return mixed|string |
||
133 | */ |
||
134 | public function prepare($files) |
||
159 | |||
160 | /** |
||
161 | * @return array|mixed |
||
162 | */ |
||
163 | public function original() |
||
171 | |||
172 | /** |
||
173 | * Prepare for each file. |
||
174 | * |
||
175 | * @param UploadedFile $file |
||
176 | * |
||
177 | * @return mixed|string |
||
178 | */ |
||
179 | protected function prepareForeach(UploadedFile $file = null) |
||
180 | { |
||
181 | $this->name = $this->getStoreName($file); |
||
182 | |||
183 | return tap($this->upload($file), function () { |
||
184 | $this->name = null; |
||
185 | }); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Preview html for file-upload plugin. |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | protected function preview() |
||
199 | |||
200 | /** |
||
201 | * Initialize the caption. |
||
202 | * |
||
203 | * @param array $caption |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | protected function initialCaption($caption) |
||
217 | |||
218 | /** |
||
219 | * @return array |
||
220 | */ |
||
221 | protected function initialPreviewConfig() |
||
243 | |||
244 | /** |
||
245 | * Get related model key name. |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | protected function getRelatedKeyName() |
||
257 | |||
258 | /** |
||
259 | * Allow to sort files. |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function sortable() |
||
269 | |||
270 | /** |
||
271 | * @param string $options |
||
272 | */ |
||
273 | protected function setupScripts($options) |
||
332 | |||
333 | /** |
||
334 | * Render file upload field. |
||
335 | * |
||
336 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
337 | */ |
||
338 | public function render() |
||
355 | |||
356 | /** |
||
357 | * Destroy original files. |
||
358 | * |
||
359 | * @param string $key |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | public function destroy($key) |
||
385 | |||
386 | /** |
||
387 | * Destroy original files from hasmany related model. |
||
388 | * |
||
389 | * @param int $key |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | public function destroyFromHasMany($key) |
||
407 | } |
||
408 |
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.