Conditions | 36 |
Paths | 80 |
Total Lines | 189 |
Lines | 142 |
Ratio | 75.13 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
97 | public function getValidator(array $input) |
||
98 | { |
||
99 | if (!array_key_exists($this->column, $input)) { |
||
100 | return false; |
||
101 | } |
||
102 | |||
103 | View Code Duplication | $array_key_attach_str = function (array $a, string $b, string $c = '.') { |
|
104 | return call_user_func_array( |
||
105 | 'array_merge', |
||
106 | array_map(function ($u, $v) use ($b, $c) { |
||
107 | return ["{$b}{$c}{$u}" => $v]; |
||
108 | }, array_keys($a), array_values($a)) |
||
109 | ); |
||
110 | }; |
||
111 | |||
112 | View Code Duplication | $array_key_clean = function (array $a) { |
|
113 | $a = count($a) ? call_user_func_array('array_merge', array_map(function ($k, $v) { |
||
114 | return [str_replace(':', '', $k) => $v]; |
||
115 | }, array_keys($a), array_values($a))) : $a; |
||
116 | |||
117 | return $a; |
||
118 | }; |
||
119 | |||
120 | $array_clean_merge = function (array $a, $b) { |
||
121 | return array_merge($a, call_user_func_array('array_merge', $b)); |
||
122 | }; |
||
123 | |||
124 | View Code Duplication | $array_key_clean_undot = function (array $a) { |
|
125 | $keys = preg_grep('/[\.\:]/', array_keys($a)); |
||
126 | if ($keys) { |
||
127 | foreach ($keys as $key) { |
||
128 | array_set($a, str_replace(':', '', $key), $a[$key]); |
||
129 | unset($a[$key]); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | return $a; |
||
134 | }; |
||
135 | $input = array_only($input, $this->column); |
||
136 | $form = $this->buildNestedForm($this->column, $this->builder); |
||
137 | $rel = $this->relationName; |
||
138 | $rules = $attributes = $messages = $newInputs = []; |
||
139 | // remove all inputs & keys marked as removed |
||
140 | $availInput = array_filter(array_map(function ($v) { |
||
141 | return $v[NestedForm::REMOVE_FLAG_NAME] ? null : $v; |
||
142 | }, $input[$rel])); |
||
143 | $keys = array_keys($availInput); |
||
144 | /* @var Field $field */ |
||
145 | foreach ($form->fields() as $field) { |
||
146 | if ($field instanceof Field\HasMany) { |
||
147 | throw new \Exception('hasMany field CAN NOT build within a HasMany field.'); |
||
148 | } |
||
149 | if (!($field instanceof Field\Embeds) && !($fieldRules = $field->getRules())) { |
||
150 | continue; |
||
151 | } |
||
152 | $column = $field->column(); |
||
153 | $columns = is_array($column) ? $column : [$column]; |
||
154 | View Code Duplication | if ($field instanceof Field\MultipleSelect || $field instanceof Field\Listbox || $field instanceof Field\Tag) { |
|
155 | foreach ($keys as $key) { |
||
156 | $availInput[$key][$column] = array_filter($availInput[$key][$column], 'strlen') ?: null; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | $newColumn = call_user_func_array('array_merge', array_map(function ($u) use ($columns, $rel) { |
||
161 | return array_map(function ($k, $v) use ($u, $rel) { |
||
162 | //Fix ResetInput Function! A Headache Implementation! |
||
163 | return $k ? "{$rel}.{$u}.{$v}:{$k}" : "{$rel}.{$u}.{$v}"; |
||
164 | }, array_keys($columns), array_values($columns)); |
||
165 | }, $keys)); |
||
166 | |||
167 | if ($field instanceof Field\Embeds) { |
||
168 | View Code Duplication | $newRules = array_map(function ($v) use ($availInput, $field, $array_key_attach_str) { |
|
169 | list($r, $k, $c) = explode('.', $v); |
||
170 | $v = "{$r}.{$k}"; |
||
171 | $embed = $field->getValidationRules([$field->column() => $availInput[$k][$c]]); |
||
172 | return $embed ? $array_key_attach_str($embed, $v) : null; |
||
173 | }, $newColumn); |
||
174 | $rules = $array_clean_merge($rules, array_filter($newRules)); |
||
175 | |||
176 | View Code Duplication | $newAttributes = array_map(function ($v) use ($availInput, $field, $array_key_attach_str) { |
|
177 | list($r, $k, $c) = explode('.', $v); |
||
178 | $v = "{$r}.{$k}"; |
||
179 | $embed = $field->getValidationAttributes([$field->column() => $availInput[$k][$c]]); |
||
180 | return $embed ? $array_key_attach_str($embed, $v) : null; |
||
181 | }, $newColumn); |
||
182 | $attributes = $array_clean_merge($attributes, array_filter($newAttributes)); |
||
183 | |||
184 | View Code Duplication | $newInput = array_map(function ($v) use ($availInput, $field, $array_key_attach_str) { |
|
185 | list($r, $k, $c) = explode('.', $v); |
||
186 | $v = "{$r}.{$k}"; |
||
187 | $embed = $field->getValidationInput([$field->column() => $availInput[$k][$c]]); |
||
188 | return $embed ? $array_key_attach_str($embed, $v) : [null => 'null']; |
||
189 | }, $newColumn); |
||
190 | $newInputs = $array_clean_merge($newInputs, array_filter($newInput, 'strlen', ARRAY_FILTER_USE_KEY)); |
||
191 | |||
192 | View Code Duplication | $newMessages = array_map(function ($v) use ($availInput, $field, $array_key_attach_str) { |
|
193 | list($r, $k, $c) = explode('.', $v); |
||
194 | $v = "{$r}.{$k}"; |
||
195 | $embed = $field->getValidationMessages([$field->column() => $availInput[$k][$c]]); |
||
196 | return $embed ? $array_key_attach_str($embed, $v) : null; |
||
197 | }, $newColumn); |
||
198 | $messages = $array_clean_merge($messages, array_filter($newMessages)); |
||
199 | } else { |
||
200 | $fieldRules = is_array($fieldRules) ? implode('|', $fieldRules) : $fieldRules; |
||
201 | View Code Duplication | $newRules = array_map(function ($v) use ($fieldRules, $availInput, $array_key_attach_str) { |
|
202 | list($r, $k, $c) = explode('.', $v); |
||
203 | //Fix ResetInput Function! A Headache Implementation! |
||
204 | $col = explode(':', $c)[0]; |
||
205 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
206 | return $array_key_attach_str(preg_replace('/.+/', $fieldRules, $availInput[$k][$col]), $v, ':'); |
||
207 | } |
||
208 | |||
209 | return [$v => $fieldRules]; |
||
210 | }, $newColumn); |
||
211 | $rules = $array_clean_merge($rules, $newRules); |
||
212 | |||
213 | View Code Duplication | $newInput = array_map(function ($v) use ($availInput, $array_key_attach_str) { |
|
214 | list($r, $k, $c) = explode('.', $v); |
||
215 | //Fix ResetInput Function! A Headache Implementation! |
||
216 | $col = explode(':', $c)[0]; |
||
217 | if (!array_key_exists($col, $availInput[$k])) { |
||
218 | |||
219 | return [$v => null]; |
||
220 | } |
||
221 | |||
222 | if (is_array($availInput[$k][$col])) { |
||
223 | return $array_key_attach_str($availInput[$k][$col], $v, ':'); |
||
224 | } |
||
225 | |||
226 | return [$v => $availInput[$k][$col]]; |
||
227 | }, $newColumn); |
||
228 | $newInputs = $array_clean_merge($newInputs, $newInput); |
||
229 | |||
230 | View Code Duplication | $newAttributes = array_map(function ($v) use ($field, $availInput) { |
|
231 | list($r, $k, $c) = explode('.', $v); |
||
232 | //Fix ResetInput Function! A Headache Implementation! |
||
233 | $col = explode(':', $c)[0]; |
||
234 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
235 | return call_user_func_array('array_merge', array_map(function ($u) use ($v, $field) { |
||
236 | $w = $field->label(); |
||
237 | //Fix ResetInput Function! A Headache Implementation! |
||
238 | $w .= is_array($field->column()) ? '['.explode(':', explode('.', $v)[2])[0].']' : ''; |
||
239 | |||
240 | return ["{$v}:{$u}" => $w]; |
||
241 | }, array_keys($availInput[$k][$col]))); |
||
242 | } |
||
243 | |||
244 | $w = $field->label(); |
||
245 | //Fix ResetInput Function! A Headache Implementation! |
||
246 | $w .= is_array($field->column()) ? '['.explode(':', explode('.', $v)[2])[0].']' : ''; |
||
247 | |||
248 | return [$v => $w]; |
||
249 | }, $newColumn); |
||
250 | $attributes = $array_clean_merge($attributes, $newAttributes); |
||
251 | } |
||
252 | |||
253 | View Code Duplication | if ($field->validationMessages) { |
|
254 | $newMessages = array_map(function ($v) use ($field, $availInput, $array_key_attach_str) { |
||
255 | list($r, $k, $c) = explode('.', $v); |
||
256 | //Fix ResetInput Function! A Headache Implementation! |
||
257 | $col = explode(':', $c)[0]; |
||
258 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
259 | return call_user_func_array('array_merge', array_map(function ($u) use ($v, $field, $array_key_attach_str) { |
||
260 | return $array_key_attach_str($field->validationMessages, "{$v}:{$u}"); |
||
261 | }, array_keys($availInput[$k][$col]))); |
||
262 | } |
||
263 | |||
264 | return $array_key_attach_str($field->validationMessages, $v); |
||
265 | }, $newColumn); |
||
266 | $messages = $array_clean_merge($messages, $newMessages); |
||
267 | } |
||
268 | } |
||
269 | |||
270 | $rules = array_filter($rules, 'strlen'); |
||
271 | if (empty($rules)) { |
||
272 | return false; |
||
273 | } |
||
274 | |||
275 | $input = $array_key_clean_undot(array_filter($newInputs, 'strlen', ARRAY_FILTER_USE_KEY)); |
||
276 | $rules = $array_key_clean($rules); |
||
277 | $attributes = $array_key_clean($attributes); |
||
278 | $messages = $array_key_clean($messages); |
||
279 | |||
280 | if (empty($input)) { |
||
281 | $input = [$rel => $availInput]; |
||
282 | } |
||
283 | |||
284 | return Validator::make($input, $rules, $messages, $attributes); |
||
285 | } |
||
286 | |||
576 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.