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 = []) |
||
62 | |||
63 | /** |
||
64 | * @param array $groups |
||
65 | */ |
||
66 | |||
67 | /** |
||
68 | * Set option groups. |
||
69 | * |
||
70 | * eg: $group = [ |
||
71 | * [ |
||
72 | * 'label' => 'xxxx', |
||
73 | * 'options' => [ |
||
74 | * 1 => 'foo', |
||
75 | * 2 => 'bar', |
||
76 | * ... |
||
77 | * ], |
||
78 | * ... |
||
79 | * ] |
||
80 | * |
||
81 | * @param array $groups |
||
82 | * |
||
83 | * @return $this |
||
84 | */ |
||
85 | public function groups(array $groups) |
||
91 | |||
92 | /** |
||
93 | * Load options for other select on change. |
||
94 | * |
||
95 | * @param string $field |
||
96 | * @param string $sourceUrl |
||
97 | * @param string $idField |
||
98 | * @param string $textField |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function load($field, $sourceUrl, $idField = 'id', $textField = 'text') |
||
103 | { |
||
104 | View Code Duplication | if (Str::contains($field, '.')) { |
|
105 | $field = $this->formatName($field); |
||
106 | $class = str_replace(['[', ']'], '_', $field); |
||
107 | } else { |
||
108 | $class = $field; |
||
109 | } |
||
110 | |||
111 | $script = <<<EOT |
||
112 | $(document).off('change', "{$this->getElementClassSelector()}"); |
||
113 | $(document).on('change', "{$this->getElementClassSelector()}", function () { |
||
114 | var target = $(this).closest('.fields-group').find(".$class"); |
||
115 | $.get("$sourceUrl?q="+this.value, function (data) { |
||
116 | target.find("option").remove(); |
||
117 | $(target).select2({ |
||
118 | data: $.map(data, function (d) { |
||
119 | d.id = d.$idField; |
||
120 | d.text = d.$textField; |
||
121 | return d; |
||
122 | }) |
||
123 | }).trigger('change'); |
||
124 | }); |
||
125 | }); |
||
126 | EOT; |
||
127 | |||
128 | Admin::script($script); |
||
129 | |||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Load options for other selects on change. |
||
135 | * |
||
136 | * @param string $fields |
||
137 | * @param string $sourceUrls |
||
138 | * @param string $idField |
||
139 | * @param string $textField |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function loads($fields = [], $sourceUrls = [], $idField = 'id', $textField = 'text') |
||
184 | |||
185 | /** |
||
186 | * Load options from remote. |
||
187 | * |
||
188 | * @param string $url |
||
189 | * @param array $parameters |
||
190 | * @param array $options |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | View Code Duplication | protected function loadRemoteOptions($url, $parameters = [], $options = []) |
|
212 | |||
213 | /** |
||
214 | * Load options from ajax results. |
||
215 | * |
||
216 | * @param string $url |
||
217 | * @param $idField |
||
218 | * @param $textField |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function ajax($url, $idField = 'id', $textField = 'text') |
||
263 | |||
264 | /** |
||
265 | * Set config for select2. |
||
266 | * |
||
267 | * all configurations see https://select2.org/configuration/options-api |
||
268 | * |
||
269 | * @param string $key |
||
270 | * @param mixed $val |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function config($key, $val) |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function render() |
||
314 | |||
315 | /** |
||
316 | * Select Level 2 linkage, support ajax paging. |
||
317 | * |
||
318 | * @param string $field parent field name |
||
319 | * @param string $sourceUrl resource route |
||
320 | * @param string $idField |
||
321 | * @param string $textField |
||
322 | * |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function ajaxLoad($field, $sourceUrl, $idField = 'id', $textField = 'text') |
||
382 | } |
||
383 |
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: