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 File 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 | ]; |
||
| 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) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Prepare for saving. |
||
| 97 | * |
||
| 98 | * @param UploadedFile|array $file |
||
| 99 | * |
||
| 100 | * @return mixed|string |
||
| 101 | */ |
||
| 102 | public function prepare($file) |
||
| 103 | { |
||
| 104 | if (request()->has(static::FILE_DELETE_FLAG)) { |
||
| 105 | return $this->destroy(); |
||
| 106 | } |
||
| 107 | |||
| 108 | $this->name = $this->getStoreName($file); |
||
| 109 | |||
| 110 | return $this->uploadAndDeleteOriginal($file); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Upload file and delete original file. |
||
| 115 | * |
||
| 116 | * @param UploadedFile $file |
||
| 117 | * |
||
| 118 | * @return mixed |
||
| 119 | */ |
||
| 120 | protected function uploadAndDeleteOriginal(UploadedFile $file) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Preview html for file-upload plugin. |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | protected function preview() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Hides the file preview. |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function hidePreview() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Initialize the caption. |
||
| 161 | * |
||
| 162 | * @param string $caption |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | protected function initialCaption($caption) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | protected function initialPreviewConfig() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $options |
||
| 185 | */ |
||
| 186 | protected function setupScripts($options) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Render file upload field. |
||
| 228 | * |
||
| 229 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 230 | */ |
||
| 231 | public function render() |
||
| 255 | } |
||
| 256 |
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.