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 Select extends Field { |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected static $css = [ |
||
| 15 | '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.min.css', |
||
| 16 | ]; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected static $js = [ |
||
| 22 | '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.full.min.js', |
||
| 23 | ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $groups = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $config = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Set options. |
||
| 37 | * |
||
| 38 | * @param array|callable|string $options |
||
| 39 | * |
||
| 40 | * @return $this|mixed |
||
| 41 | */ |
||
| 42 | public function options($options = []) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param array $groups |
||
| 63 | */ |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set option groups. |
||
| 67 | * |
||
| 68 | * eg: $group = [ |
||
| 69 | * [ |
||
| 70 | * 'label' => 'xxxx', |
||
| 71 | * 'options' => [ |
||
| 72 | * 1 => 'foo', |
||
| 73 | * 2 => 'bar', |
||
| 74 | * ... |
||
| 75 | * ], |
||
| 76 | * ... |
||
| 77 | * ] |
||
| 78 | * |
||
| 79 | * @param array $groups |
||
| 80 | * |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | public function groups(array $groups) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Load options for other select on change. |
||
| 91 | * |
||
| 92 | * @param string $field |
||
| 93 | * @param string $sourceUrl |
||
| 94 | * @param string $idField |
||
| 95 | * @param string $textField |
||
| 96 | * |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function load($field, $sourceUrl, $idField = 'id', $textField = 'text') { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Load options for other selects on change. |
||
| 131 | * |
||
| 132 | * @param string $fields |
||
| 133 | * @param string $sourceUrls |
||
| 134 | * @param string $idField |
||
| 135 | * @param string $textField |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function loads($fields = [], $sourceUrls = [], $idField = 'id', $textField = 'text') { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Load options from remote. |
||
| 182 | * |
||
| 183 | * @param string $url |
||
| 184 | * @param array $parameters |
||
| 185 | * @param array $options |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | View Code Duplication | protected function loadRemoteOptions($url, $parameters = [], $options = []) { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Load options from ajax results. |
||
| 209 | * |
||
| 210 | * @param string $url |
||
| 211 | * @param $idField |
||
| 212 | * @param $textField |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | public function ajax($url, $idField = 'id', $textField = 'text') { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set config for select2. |
||
| 259 | * |
||
| 260 | * all configurations see https://select2.org/configuration/options-api |
||
| 261 | * |
||
| 262 | * @param string $key |
||
| 263 | * @param mixed $val |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function config($key, $val) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function render() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Select Level 2 linkage, support ajax paging. |
||
| 306 | * |
||
| 307 | * @param string $field parent field name |
||
| 308 | * @param string $sourceUrl resource route |
||
| 309 | * @param string $idField |
||
| 310 | * @param string $textField |
||
| 311 | * |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function ajaxLoad($field, $sourceUrl, $idField = 'id', $textField = 'text') { |
||
| 370 | |||
| 371 | } |
||
| 372 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: