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 | 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 | * @return array |
||
115 | */ |
||
116 | protected function sortFiles($order) |
||
129 | |||
130 | /** |
||
131 | * Prepare for saving. |
||
132 | * |
||
133 | * @param UploadedFile|array $files |
||
134 | * |
||
135 | * @return mixed|string |
||
136 | */ |
||
137 | public function prepare($files) |
||
151 | |||
152 | /** |
||
153 | * @return array|mixed |
||
154 | */ |
||
155 | public function original() |
||
163 | |||
164 | /** |
||
165 | * Prepare for each file. |
||
166 | * |
||
167 | * @param UploadedFile $file |
||
168 | * |
||
169 | * @return mixed|string |
||
170 | */ |
||
171 | View Code Duplication | protected function prepareForeach(UploadedFile $file = null) |
|
179 | |||
180 | /** |
||
181 | * Preview html for file-upload plugin. |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function preview() |
||
191 | |||
192 | /** |
||
193 | * Initialize the caption. |
||
194 | * |
||
195 | * @param array $caption |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function initialCaption($caption) |
||
209 | |||
210 | /** |
||
211 | * @return array |
||
212 | */ |
||
213 | protected function initialPreviewConfig() |
||
228 | |||
229 | /** |
||
230 | * Allow to sort files. |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function sortable() |
||
240 | |||
241 | /** |
||
242 | * @param array $options |
||
243 | * @param array $options |
||
244 | */ |
||
245 | protected function setupScripts($options) |
||
306 | |||
307 | /** |
||
308 | * Render file upload field. |
||
309 | * |
||
310 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
311 | */ |
||
312 | public function render() |
||
329 | |||
330 | /** |
||
331 | * Destroy original files. |
||
332 | * |
||
333 | * @return string. |
||
334 | */ |
||
335 | public function destroy($key) |
||
349 | } |
||
350 |
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.