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.5.2', |
||
| 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', |
||
| 29 | '/vendor/laravel-admin/bootstrap-fileinput/js/fileinput.min.js?v=4.5.2', |
||
| 30 | '/vendor/laravel-admin/bootstrap-fileinput/js/plugins/sortable.min.js?v=4.5.2', |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new File instance. |
||
| 35 | * |
||
| 36 | * @param string $column |
||
| 37 | * @param array $arguments |
||
| 38 | */ |
||
| 39 | public function __construct($column, $arguments = []) |
||
| 40 | { |
||
| 41 | $this->initStorage(); |
||
| 42 | |||
| 43 | parent::__construct($column, $arguments); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Default directory for file to upload. |
||
| 48 | * |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | public function defaultDirectory() |
||
| 52 | { |
||
| 53 | return config('admin.upload.directory.file'); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | public function getValidator(array $input) |
||
| 60 | { |
||
| 61 | if (request()->has(static::FILE_DELETE_FLAG)) { |
||
| 62 | return false; |
||
| 63 | } |
||
| 64 | |||
| 65 | if ( |
||
| 66 | request()->has(static::FILE_SORT_FLAG) && |
||
| 67 | ! array_filter(request()->input(static::FILE_SORT_FLAG)) |
||
| 68 | ) { |
||
| 69 | return false; |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($this->validator) { |
||
| 73 | return $this->validator->call($this, $input); |
||
| 74 | } |
||
| 75 | |||
| 76 | $attributes = []; |
||
| 77 | |||
| 78 | if (!$fieldRules = $this->getRules()) { |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | $attributes[$this->column] = $this->label; |
||
| 83 | |||
| 84 | list($rules, $input) = $this->hydrateFiles(Arr::get($input, $this->column, [])); |
||
|
|
|||
| 85 | |||
| 86 | return \validator($input, $rules, $this->getValidationMessages(), $attributes); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Hydrate the files array. |
||
| 91 | * |
||
| 92 | * @param array $value |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | protected function hydrateFiles(array $value) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Sort files. |
||
| 114 | * |
||
| 115 | * @param string $order |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | protected function sortFiles($order) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Prepare for saving. |
||
| 135 | * |
||
| 136 | * @param UploadedFile|array $files |
||
| 137 | * |
||
| 138 | * @return mixed|string |
||
| 139 | */ |
||
| 140 | public function prepare($files) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return array|mixed |
||
| 157 | */ |
||
| 158 | public function original() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Prepare for each file. |
||
| 169 | * |
||
| 170 | * @param UploadedFile $file |
||
| 171 | * |
||
| 172 | * @return mixed|string |
||
| 173 | */ |
||
| 174 | View Code Duplication | protected function prepareForeach(UploadedFile $file = null) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Preview html for file-upload plugin. |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | protected function preview() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Initialize the caption. |
||
| 197 | * |
||
| 198 | * @param array $caption |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | protected function initialCaption($caption) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | protected function initialPreviewConfig() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Allow to sort files. |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function sortable() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $options |
||
| 248 | */ |
||
| 249 | protected function setupScripts($options) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Render file upload field. |
||
| 311 | * |
||
| 312 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 313 | */ |
||
| 314 | public function render() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Destroy original files. |
||
| 334 | * |
||
| 335 | * @param string $key |
||
| 336 | * |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | public function destroy($key) |
||
| 353 | } |
||
| 354 |
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.