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 |
||
12 | class Select extends Field |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected static $css = [ |
||
18 | '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.min.css', |
||
19 | ]; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected static $js = [ |
||
25 | '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.full.min.js', |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $groups = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $config = []; |
||
37 | |||
38 | /** |
||
39 | * Set options. |
||
40 | * |
||
41 | * @param array|callable|string $options |
||
42 | * |
||
43 | * @return $this|mixed |
||
44 | */ |
||
45 | public function options($options = []) |
||
69 | |||
70 | /** |
||
71 | * @param array $groups |
||
72 | */ |
||
73 | |||
74 | /** |
||
75 | * Set option groups. |
||
76 | * |
||
77 | * eg: $group = [ |
||
78 | * [ |
||
79 | * 'label' => 'xxxx', |
||
80 | * 'options' => [ |
||
81 | * 1 => 'foo', |
||
82 | * 2 => 'bar', |
||
83 | * ... |
||
84 | * ], |
||
85 | * ... |
||
86 | * ] |
||
87 | * |
||
88 | * @param array $groups |
||
89 | * |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function groups(array $groups) |
||
98 | |||
99 | /** |
||
100 | * Load options for other select on change. |
||
101 | * |
||
102 | * @param string $field |
||
103 | * @param string $sourceUrl |
||
104 | * @param string $idField |
||
105 | * @param string $textField |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function load($field, $sourceUrl, $idField = 'id', $textField = 'text') |
||
139 | |||
140 | /** |
||
141 | * Load options for other selects on change. |
||
142 | * |
||
143 | * @param string $fields |
||
144 | * @param string $sourceUrls |
||
145 | * @param string $idField |
||
146 | * @param string $textField |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function loads($fields = [], $sourceUrls = [], $idField = 'id', $textField = 'text') |
||
191 | |||
192 | /** |
||
193 | * Load options from current selected resource(s). |
||
194 | * |
||
195 | * @param string $model |
||
196 | * @param string $idField |
||
197 | * @param string $textField |
||
198 | * |
||
199 | * @return $this |
||
200 | */ |
||
201 | View Code Duplication | public function model($model, $idField = 'id', $textField = 'name') |
|
231 | |||
232 | /** |
||
233 | * Load options from remote. |
||
234 | * |
||
235 | * @param string $url |
||
236 | * @param array $parameters |
||
237 | * @param array $options |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | protected function loadRemoteOptions($url, $parameters = [], $options = []) |
||
242 | { |
||
243 | $ajaxOptions = [ |
||
244 | 'url' => $url.'?'.http_build_query($parameters), |
||
245 | ]; |
||
246 | $configs = array_merge([ |
||
247 | 'allowClear' => true, |
||
248 | 'placeholder' => [ |
||
249 | 'id' => '', |
||
250 | 'text' => trans('admin.choose'), |
||
251 | ], |
||
252 | ], $this->config); |
||
253 | |||
254 | $configs = json_encode($configs); |
||
255 | $configs = substr($configs, 1, strlen($configs) - 2); |
||
256 | |||
257 | $ajaxOptions = json_encode(array_merge($ajaxOptions, $options)); |
||
258 | |||
259 | $this->script = <<<EOT |
||
260 | |||
261 | $.ajax($ajaxOptions).done(function(data) { |
||
262 | |||
263 | var select = $("{$this->getElementClassSelector()}"); |
||
264 | |||
265 | select.select2({ |
||
266 | data: data, |
||
267 | $configs |
||
268 | }); |
||
269 | |||
270 | var value = select.data('value') + ''; |
||
271 | |||
272 | if (value) { |
||
273 | value = value.split(','); |
||
274 | select.select2('val', value); |
||
275 | } |
||
276 | }); |
||
277 | |||
278 | EOT; |
||
279 | |||
280 | return $this; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Load options from ajax results. |
||
285 | * |
||
286 | * @param string $url |
||
287 | * @param $idField |
||
288 | * @param $textField |
||
289 | * |
||
290 | * @return $this |
||
291 | */ |
||
292 | View Code Duplication | public function ajax($url, $idField = 'id', $textField = 'text') |
|
342 | |||
343 | /** |
||
344 | * Set config for select2. |
||
345 | * |
||
346 | * all configurations see https://select2.org/configuration/options-api |
||
347 | * |
||
348 | * @param string $key |
||
349 | * @param mixed $val |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function config($key, $val) |
||
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | public function readOnly() |
||
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | public function render() |
||
415 | } |
||
416 |
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: