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 |
||
9 | class MultipleFile extends Field |
||
10 | { |
||
11 | use UploadField; |
||
12 | |||
13 | /** |
||
14 | * Css. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected static $css = [ |
||
19 | '/vendor/laravel-admin/bootstrap-fileinput/css/fileinput.min.css?v=4.3.7', |
||
20 | ]; |
||
21 | |||
22 | /** |
||
23 | * Js. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected static $js = [ |
||
28 | '/vendor/laravel-admin/bootstrap-fileinput/js/plugins/canvas-to-blob.min.js?v=4.3.7', |
||
29 | '/vendor/laravel-admin/bootstrap-fileinput/js/fileinput.min.js?v=4.3.7', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * Create a new File instance. |
||
34 | * |
||
35 | * @param string $column |
||
36 | * @param array $arguments |
||
37 | */ |
||
38 | public function __construct($column, $arguments = []) |
||
44 | |||
45 | /** |
||
46 | * Default directory for file to upload. |
||
47 | * |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function defaultDirectory() |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function getValidator(array $input) |
||
80 | |||
81 | /** |
||
82 | * Hydrate the files array. |
||
83 | * |
||
84 | * @param array $value |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | protected function hydrateFiles(array $value) |
||
103 | |||
104 | /** |
||
105 | * Prepare for saving. |
||
106 | * |
||
107 | * @param UploadedFile|array $files |
||
108 | * |
||
109 | * @return mixed|string |
||
110 | */ |
||
111 | public function prepare($files) |
||
121 | |||
122 | /** |
||
123 | * @return array|mixed |
||
124 | */ |
||
125 | public function original() |
||
133 | |||
134 | /** |
||
135 | * Prepare for each file. |
||
136 | * |
||
137 | * @param UploadedFile $file |
||
138 | * |
||
139 | * @return mixed|string |
||
140 | */ |
||
141 | View Code Duplication | protected function prepareForeach(UploadedFile $file = null) |
|
149 | |||
150 | /** |
||
151 | * Preview html for file-upload plugin. |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | protected function preview() |
||
161 | |||
162 | /** |
||
163 | * Initialize the caption. |
||
164 | * |
||
165 | * @param array $caption |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | protected function initialCaption($caption) |
||
179 | |||
180 | /** |
||
181 | * @return array |
||
182 | */ |
||
183 | protected function initialPreviewConfig() |
||
198 | |||
199 | /** |
||
200 | * Render file upload field. |
||
201 | * |
||
202 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
203 | */ |
||
204 | View Code Duplication | public function render() |
|
223 | |||
224 | /** |
||
225 | * Destroy original files. |
||
226 | * |
||
227 | * @return string. |
||
228 | */ |
||
229 | public function destroy($key) |
||
243 | } |
||
244 |
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.