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 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected static $css = [ |
||
16 | '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.min.css', |
||
17 | ]; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected static $js = [ |
||
23 | '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.full.min.js', |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $groups = []; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $config = []; |
||
35 | |||
36 | /** |
||
37 | * Set options. |
||
38 | * |
||
39 | * @param array|callable|string $options |
||
40 | * |
||
41 | * @return $this|mixed |
||
42 | */ |
||
43 | public function options($options = []) |
||
67 | |||
68 | /** |
||
69 | * @param array $groups |
||
70 | */ |
||
71 | |||
72 | /** |
||
73 | * Set option groups. |
||
74 | * |
||
75 | * eg: $group = [ |
||
76 | * [ |
||
77 | * 'label' => 'xxxx', |
||
78 | * 'options' => [ |
||
79 | * 1 => 'foo', |
||
80 | * 2 => 'bar', |
||
81 | * ... |
||
82 | * ], |
||
83 | * ... |
||
84 | * ] |
||
85 | * |
||
86 | * @param array $groups |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function groups(array $groups) |
||
96 | |||
97 | /** |
||
98 | * Load options for other select on change. |
||
99 | * |
||
100 | * @param string $field |
||
101 | * @param string $sourceUrl |
||
102 | * @param string $idField |
||
103 | * @param string $textField |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function load($field, $sourceUrl, $idField = 'id', $textField = 'text') |
||
137 | |||
138 | /** |
||
139 | * Load options for other selects on change. |
||
140 | * |
||
141 | * @param string $fields |
||
142 | * @param string $sourceUrls |
||
143 | * @param string $idField |
||
144 | * @param string $textField |
||
145 | * |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function loads($fields = [], $sourceUrls = [], $idField = 'id', $textField = 'text') |
||
189 | |||
190 | /** |
||
191 | * Load options from current selected resource(s). |
||
192 | * |
||
193 | * @param Illuminate\Database\Eloquent\Model $model |
||
194 | * @param string $idField |
||
195 | * @param string $textField |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | View Code Duplication | public function model($model, $idField = 'id', $textField = 'name') |
|
200 | { |
||
201 | $this->options = function ($resource) use ($model, $idField, $textField) { |
||
202 | if (null == $resource) { |
||
203 | return []; |
||
204 | } |
||
205 | |||
206 | if (is_array($resource) && !empty($resource) && isset($resource[0]['id'])) { |
||
207 | $resource = array_map(function ($res) { |
||
208 | return $res['id']; |
||
209 | }, $resource); |
||
210 | } elseif (is_array($resource) && !empty($resource) && isset($resource['id'])) { |
||
211 | $resource = $resource['id']; |
||
212 | } |
||
213 | |||
214 | $model = $model::find($resource); |
||
215 | |||
216 | if ($model) { |
||
217 | if ($model instanceof \Illuminate\Support\Collection) { |
||
218 | $results = []; |
||
219 | |||
220 | foreach ($model as $result) { |
||
221 | $results[$result->{$idField}] = $result->{$textField}; |
||
222 | } |
||
223 | |||
224 | return $results; |
||
225 | } |
||
226 | |||
227 | return [$model->{$idField} => $model->{$textField}]; |
||
228 | } |
||
229 | |||
230 | return []; |
||
231 | }; |
||
232 | |||
233 | return $this; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Load options from remote. |
||
238 | * |
||
239 | * @param string $url |
||
240 | * @param array $parameters |
||
241 | * @param array $options |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | View Code Duplication | protected function loadRemoteOptions($url, $parameters = [], $options = []) |
|
273 | |||
274 | /** |
||
275 | * Load options from ajax results. |
||
276 | * |
||
277 | * @param string $url |
||
278 | * @param $idField |
||
279 | * @param $textField |
||
280 | * |
||
281 | * @return $this |
||
282 | */ |
||
283 | View Code Duplication | public function ajax($url, $idField = 'id', $textField = 'text') |
|
284 | { |
||
285 | $configs = array_merge([ |
||
286 | 'allowClear' => true, |
||
287 | 'placeholder' => $this->label, |
||
288 | 'minimumInputLength' => 1, |
||
289 | ], $this->config); |
||
290 | |||
291 | $configs = json_encode($configs); |
||
292 | $configs = substr($configs, 1, strlen($configs) - 2); |
||
293 | |||
294 | $this->script = <<<EOT |
||
295 | |||
296 | $("{$this->getElementClassSelector()}").select2({ |
||
297 | ajax: { |
||
298 | url: "$url", |
||
299 | dataType: 'json', |
||
300 | delay: 250, |
||
301 | data: function (params) { |
||
302 | return { |
||
303 | q: params.term, |
||
304 | page: params.page |
||
305 | }; |
||
306 | }, |
||
307 | processResults: function (data, params) { |
||
308 | params.page = params.page || 1; |
||
309 | |||
310 | return { |
||
311 | results: $.map(data.data, function (d) { |
||
312 | d.id = d.$idField; |
||
313 | d.text = d.$textField; |
||
314 | return d; |
||
315 | }), |
||
316 | pagination: { |
||
317 | more: data.next_page_url |
||
318 | } |
||
319 | }; |
||
320 | }, |
||
321 | cache: true |
||
322 | }, |
||
323 | $configs, |
||
324 | escapeMarkup: function (markup) { |
||
325 | return markup; |
||
326 | } |
||
327 | }); |
||
328 | |||
329 | EOT; |
||
330 | |||
331 | return $this; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Set config for select2. |
||
336 | * |
||
337 | * all configurations see https://select2.org/configuration/options-api |
||
338 | * |
||
339 | * @param string $key |
||
340 | * @param mixed $val |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function config($key, $val) |
||
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | public function render() |
||
386 | } |
||
387 |
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: