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:
Complex classes like Embeds often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Embeds, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class Embeds extends Field |
||
56 | { |
||
57 | /** |
||
58 | * @var \Closure |
||
59 | */ |
||
60 | protected $builder = null; |
||
61 | |||
62 | /** |
||
63 | * Create a new HasMany field instance. |
||
64 | * |
||
65 | * @param string $column |
||
66 | * @param array $arguments |
||
67 | */ |
||
68 | View Code Duplication | public function __construct($column, $arguments = []) |
|
69 | { |
||
70 | $this->column = $column; |
||
71 | |||
72 | if (count($arguments) == 1) { |
||
73 | $this->label = $this->formatLabel(); |
||
74 | $this->builder = $arguments[0]; |
||
75 | } |
||
76 | |||
77 | if (count($arguments) == 2) { |
||
78 | list($this->label, $this->builder) = $arguments; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Prepare input data for insert or update. |
||
84 | * |
||
85 | * @param array $input |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | public function prepare($input) |
||
90 | { |
||
91 | $form = $this->buildEmbeddedForm(); |
||
92 | |||
93 | return $form->setOriginal($this->original)->prepare($input); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Prepare validation rules based on input data. |
||
98 | * |
||
99 | * @param array $input |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | public function getValidationRules(array $input) |
||
104 | { |
||
105 | if (!array_key_exists($this->column, $input)) { |
||
106 | return false; |
||
107 | } |
||
108 | |||
109 | $input = array_only($input, $this->column); |
||
110 | $rules = []; |
||
111 | $rel = $this->column; |
||
112 | $availInput = $input; |
||
113 | |||
114 | /** @var Field $field */ |
||
115 | foreach ($this->buildEmbeddedForm()->fields() as $field) { |
||
116 | if (!$fieldRules = $field->getRules()) { |
||
117 | continue; |
||
118 | } |
||
119 | |||
120 | $column = $field->column(); |
||
121 | $columns = is_array($column) ? $column : [$column]; |
||
122 | View Code Duplication | if ( |
|
123 | $field instanceof Field\MultipleSelect |
||
124 | || $field instanceof Field\Listbox |
||
125 | || $field instanceof Field\Checkbox |
||
126 | || $field instanceof Field\Tags |
||
127 | || $field instanceof Field\MultipleImage |
||
128 | || $field instanceof Field\MultipleFile |
||
129 | ) { |
||
130 | $availInput[$column] = array_filter($availInput[$column], 'strlen'); |
||
131 | $availInput[$column] = $availInput[$column] ?: null; |
||
132 | } |
||
133 | /* |
||
134 | * |
||
135 | * For single column field format rules to: |
||
136 | * [ |
||
137 | * 'extra.name' => 'required' |
||
138 | * 'extra.email' => 'required' |
||
139 | * ] |
||
140 | * |
||
141 | * For multiple column field with rules like 'required': |
||
142 | * 'extra' => [ |
||
143 | * 'start' => 'start_at' |
||
144 | * 'end' => 'end_at', |
||
145 | * ] |
||
146 | * |
||
147 | * format rules to: |
||
148 | * [ |
||
149 | * 'extra.start_atstart' => 'required' |
||
150 | * 'extra.end_atend' => 'required' |
||
151 | * ] |
||
152 | * |
||
153 | * |
||
154 | * format attributes to: |
||
155 | * [ |
||
156 | * 'extra.start_atstart' => "$label[start_at]" |
||
157 | * 'extra.end_atend' => "$label[end_at]" |
||
158 | * ] |
||
159 | */ |
||
160 | $newColumn = array_map(function ($k, $v) use ($rel) { |
||
161 | //Fix ResetInput Function! A Headache Implementation! |
||
162 | return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}"; |
||
163 | }, array_keys($columns), array_values($columns)); |
||
164 | |||
165 | $fieldRules = is_array($fieldRules) ? implode('|', $fieldRules) : $fieldRules; |
||
166 | View Code Duplication | $newRules = array_map(function ($v) use ($fieldRules, $availInput) { |
|
167 | list($k, $c) = explode('.', $v); |
||
168 | //Fix ResetInput Function! A Headache Implementation! |
||
169 | $col = explode(':', $c)[0]; |
||
170 | |||
171 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
172 | return array_key_attach_str(preg_replace('/./', $fieldRules, $availInput[$k][$col]), $v, ':'); |
||
173 | } |
||
174 | |||
175 | //May Have Problem in Dealing with File Upload in Edit Mode |
||
176 | return [$v => $fieldRules]; |
||
177 | }, $newColumn); |
||
178 | $rules = array_clean_merge($rules, $newRules); |
||
179 | } |
||
180 | |||
181 | return $rules; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Prepare validation attributes based on input data. |
||
186 | * |
||
187 | * @param array $input |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getValidationAttributes(array $input) |
||
192 | { |
||
193 | if (!array_key_exists($this->column, $input)) { |
||
194 | return false; |
||
195 | } |
||
196 | |||
197 | $input = array_only($input, $this->column); |
||
198 | $attributes = []; |
||
199 | $rel = $this->column; |
||
200 | $availInput = $input; |
||
201 | /** @var Field $field */ |
||
202 | foreach ($this->buildEmbeddedForm()->fields() as $field) { |
||
203 | if (!$field->getRules()) { |
||
204 | continue; |
||
205 | } |
||
206 | |||
207 | $column = $field->column(); |
||
208 | $columns = is_array($column) ? $column : [$column]; |
||
209 | View Code Duplication | if ( |
|
210 | $field instanceof Field\MultipleSelect |
||
211 | || $field instanceof Field\Listbox |
||
212 | || $field instanceof Field\Checkbox |
||
213 | || $field instanceof Field\Tags |
||
214 | || $field instanceof Field\MultipleImage |
||
215 | || $field instanceof Field\MultipleFile |
||
216 | ) { |
||
217 | $availInput[$column] = array_filter($availInput[$column], 'strlen'); |
||
218 | $availInput[$column] = $availInput[$column] ?: null; |
||
219 | } |
||
220 | /* |
||
221 | * |
||
222 | * For single column field format rules to: |
||
223 | * [ |
||
224 | * 'extra.name' => 'required' |
||
225 | * 'extra.email' => 'required' |
||
226 | * ] |
||
227 | * |
||
228 | * For multiple column field with rules like 'required': |
||
229 | * 'extra' => [ |
||
230 | * 'start' => 'start_at' |
||
231 | * 'end' => 'end_at', |
||
232 | * ] |
||
233 | * |
||
234 | * format rules to: |
||
235 | * [ |
||
236 | * 'extra.start_atstart' => 'required' |
||
237 | * 'extra.end_atend' => 'required' |
||
238 | * ] |
||
239 | * |
||
240 | * |
||
241 | * format attributes to: |
||
242 | * [ |
||
243 | * 'extra.start_atstart' => "$label[start_at]" |
||
244 | * 'extra.end_atend' => "$label[end_at]" |
||
245 | * ] |
||
246 | */ |
||
247 | $newColumn = array_map(function ($k, $v) use ($rel) { |
||
248 | //Fix ResetInput Function! A Headache Implementation! |
||
249 | return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}"; |
||
250 | }, array_keys($columns), array_values($columns)); |
||
251 | |||
252 | View Code Duplication | $newAttributes = array_map(function ($v) use ($field, $availInput) { |
|
253 | list($k, $c) = explode('.', $v); |
||
254 | //Fix ResetInput Function! A Headache Implementation! |
||
255 | $col = explode(':', $c)[0]; |
||
256 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
257 | return call_user_func_array('array_merge', array_map(function ($u) use ($v, $field) { |
||
258 | $w = $field->label(); |
||
259 | //Fix ResetInput Function! A Headache Implementation! |
||
260 | $w .= is_array($field->column()) ? '[' . explode(':', explode('.', $v)[2])[0] . ']' : ''; |
||
261 | |||
262 | return ["{$v}:{$u}" => $w]; |
||
263 | }, array_keys($availInput[$k][$col]))); |
||
264 | } |
||
265 | |||
266 | //May Have Problem in Dealing with File Upload in Edit Mode |
||
267 | $w = $field->label(); |
||
268 | //Fix ResetInput Function! A Headache Implementation! |
||
269 | $w .= is_array($field->column()) ? '[' . explode(':', explode('.', $v)[2])[0] . ']' : ''; |
||
270 | |||
271 | return [$v => $w]; |
||
272 | }, $newColumn); |
||
273 | $attributes = array_clean_merge($attributes, $newAttributes); |
||
274 | } |
||
275 | |||
276 | return $attributes; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Prepare input data for insert or update. |
||
281 | * |
||
282 | * @param array $input |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | public function getValidationInput(array $input) |
||
287 | { |
||
288 | if (!array_key_exists($this->column, $input)) { |
||
289 | return false; |
||
290 | } |
||
291 | |||
292 | $input = array_only($input, $this->column); |
||
293 | $newInputs = []; |
||
294 | $rel = $this->column; |
||
295 | $availInput = $input; |
||
296 | |||
297 | /** @var Field $field */ |
||
298 | foreach ($this->buildEmbeddedForm()->fields() as $field) { |
||
299 | if (!$field->getRules()) { |
||
300 | continue; |
||
301 | } |
||
302 | |||
303 | $column = $field->column(); |
||
304 | $columns = is_array($column) ? $column : [$column]; |
||
305 | View Code Duplication | if ( |
|
306 | $field instanceof Field\MultipleSelect |
||
307 | || $field instanceof Field\Listbox |
||
308 | || $field instanceof Field\Checkbox |
||
309 | || $field instanceof Field\Tags |
||
310 | || $field instanceof Field\MultipleImage |
||
311 | || $field instanceof Field\MultipleFile |
||
312 | ) { |
||
313 | $availInput[$column] = array_filter($availInput[$column], 'strlen'); |
||
314 | $availInput[$column] = $availInput[$column] ?: null; |
||
315 | } |
||
316 | /* |
||
317 | * |
||
318 | * For single column field format rules to: |
||
319 | * [ |
||
320 | * 'extra.name' => $value |
||
321 | * 'extra.email' => $value |
||
322 | * ] |
||
323 | * |
||
324 | * For multiple column field with rules like 'required': |
||
325 | * 'extra' => [ |
||
326 | * 'start' => 'start_at' |
||
327 | * 'end' => 'end_at', |
||
328 | * ] |
||
329 | * |
||
330 | * format rules to: |
||
331 | * [ |
||
332 | * 'extra.start_atstart' => $value |
||
333 | * 'extra.end_atend' => $value |
||
334 | * ] |
||
335 | */ |
||
336 | $newColumn = array_map(function ($k, $v) use ($rel) { |
||
337 | //Fix ResetInput Function! A Headache Implementation! |
||
338 | return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}"; |
||
339 | }, array_keys($columns), array_values($columns)); |
||
340 | |||
341 | View Code Duplication | $newInput = array_map(function ($v) use ($availInput) { |
|
342 | list($k, $c) = explode('.', $v); |
||
343 | //Fix ResetInput Function! A Headache Implementation! |
||
344 | $col = explode(':', $c)[0]; |
||
345 | if (!array_key_exists($col, $availInput[$k])) { |
||
346 | return [$v => null]; |
||
347 | } |
||
348 | |||
349 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
350 | return array_key_attach_str($availInput[$k][$col], $v, ':'); |
||
351 | } |
||
352 | |||
353 | return [$v => $availInput[$k][$col]]; |
||
354 | }, $newColumn); |
||
355 | $newInputs = array_clean_merge($newInputs, $newInput); |
||
356 | } |
||
357 | |||
358 | return $newInputs; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Prepare customer validation messages based on input data. |
||
363 | * |
||
364 | * @param array $input |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | public function getValidationMessages(array $input) |
||
369 | { |
||
370 | if (!array_key_exists($this->column, $input)) { |
||
371 | return false; |
||
372 | } |
||
373 | |||
374 | $input = array_only($input, $this->column); |
||
375 | $messages = []; |
||
376 | $rel = $this->column; |
||
377 | $availInput = $input; |
||
378 | |||
379 | /** @var Field $field */ |
||
380 | foreach ($this->buildEmbeddedForm()->fields() as $field) { |
||
381 | if (!$field->getRules()) { |
||
382 | continue; |
||
383 | } |
||
384 | |||
385 | $column = $field->column(); |
||
386 | $columns = is_array($column) ? $column : [$column]; |
||
387 | View Code Duplication | if ( |
|
388 | $field instanceof Field\MultipleSelect |
||
389 | || $field instanceof Field\Listbox |
||
390 | || $field instanceof Field\Checkbox |
||
391 | || $field instanceof Field\Tags |
||
392 | || $field instanceof Field\MultipleImage |
||
393 | || $field instanceof Field\MultipleFile |
||
394 | ) { |
||
395 | $availInput[$column] = array_filter($availInput[$column], 'strlen'); |
||
396 | $availInput[$column] = $availInput[$column] ?: null; |
||
397 | } |
||
398 | |||
399 | $newColumn = array_map(function ($k, $v) use ($rel) { |
||
400 | //Fix ResetInput Function! A Headache Implementation! |
||
401 | return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}"; |
||
402 | }, array_keys($columns), array_values($columns)); |
||
403 | |||
404 | View Code Duplication | if ($field->validationMessages) { |
|
405 | $newMessages = array_map(function ($v) use ($field, $availInput) { |
||
406 | list($k, $c) = explode('.', $v); |
||
407 | //Fix ResetInput Function! A Headache Implementation! |
||
408 | $col = explode(':', $c)[0]; |
||
409 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
410 | return call_user_func_array( |
||
411 | 'array_merge', |
||
412 | array_map(function ($u) use ( |
||
413 | $v, |
||
414 | $field |
||
415 | ) { |
||
416 | return array_key_attach_str($field->validationMessages, "{$v}:{$u}"); |
||
417 | }, array_keys($availInput[$k][$col])) |
||
418 | ); |
||
419 | } |
||
420 | |||
421 | //May Have Problem in Dealing with File Upload in Edit Mode |
||
422 | return array_key_attach_str($field->validationMessages, $v); |
||
423 | }, $newColumn); |
||
424 | $messages = array_clean_merge($messages, $newMessages); |
||
425 | } |
||
426 | } |
||
427 | |||
428 | return $messages; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * {@inheritdoc} |
||
433 | */ |
||
434 | public function getValidator(array $input) |
||
435 | { |
||
436 | if (!array_key_exists($this->column, $input)) { |
||
437 | return false; |
||
438 | } |
||
439 | |||
440 | $rules = $this->getValidationRules($input); |
||
441 | if (empty($rules)) { |
||
442 | return false; |
||
443 | } |
||
444 | |||
445 | $attributes = $this->getValidationAttributes($input); |
||
446 | $messages = $this->getValidationMessages($input); |
||
447 | $newInputs = $this->getValidationInput($input); |
||
448 | |||
449 | $input = array_key_clean_undot(array_filter($newInputs, 'strlen', ARRAY_FILTER_USE_KEY)); |
||
450 | $rules = array_key_clean($rules); |
||
451 | $attributes = array_key_clean($attributes); |
||
452 | $messages = array_key_clean($messages); |
||
453 | |||
454 | return Validator::make($input, $rules, $messages, $attributes); |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Get data for Embedded form. |
||
459 | * |
||
460 | * Normally, data is obtained from the database. |
||
461 | * |
||
462 | * When the data validation errors, data is obtained from session flash. |
||
463 | * |
||
464 | * @return array |
||
465 | */ |
||
466 | protected function getEmbeddedData() |
||
467 | { |
||
468 | if ($old = old($this->column)) { |
||
469 | return $old; |
||
470 | } |
||
471 | |||
472 | if (empty($this->value)) { |
||
473 | return []; |
||
474 | } |
||
475 | |||
476 | if (is_string($this->value)) { |
||
477 | return json_decode($this->value, true); |
||
478 | } |
||
479 | |||
480 | return (array)$this->value; |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Build a Embedded Form and fill data. |
||
485 | * |
||
486 | * @return EmbeddedForm |
||
487 | */ |
||
488 | protected function buildEmbeddedForm() |
||
489 | { |
||
490 | $form = new EmbeddedForm($this->elementName ?: $this->column); |
||
491 | |||
492 | $form->setParent($this->form); |
||
493 | |||
494 | call_user_func($this->builder, $form); |
||
495 | |||
496 | //Fix the Bug of Embeds Fields Cannot be used within HasMany |
||
497 | if ($this->elementName) { |
||
498 | list($rel, $key, $col) = explode('.', $this->errorKey); |
||
499 | $form->fields()->each(function (Field $field) use ($rel, $key, $col) { |
||
500 | $column = $field->column(); |
||
501 | $elementName = $elementClass = $errorKey = []; |
||
502 | if (is_array($column)) { |
||
503 | foreach ($column as $k => $name) { |
||
504 | $errorKey[$k] = sprintf('%s.%s.%s.%s', $rel, $key, $col, $name); |
||
505 | $elementName[$k] = sprintf('%s[%s][%s][%s]', $rel, $key, $col, $name); |
||
506 | $elementClass[$k] = [$rel, $col, $name]; |
||
507 | } |
||
508 | } else { |
||
509 | $errorKey = sprintf('%s.%s.%s.%s', $rel, $key, $col, $column); |
||
510 | $elementName = sprintf('%s[%s][%s][%s]', $rel, $key, $col, $column); |
||
511 | $elementClass = [$rel, $col, $column]; |
||
512 | } |
||
513 | $field->setErrorKey($errorKey) |
||
514 | ->setElementName($elementName) |
||
515 | ->setElementClass($elementClass); |
||
516 | }); |
||
517 | } |
||
518 | $form->fill($this->getEmbeddedData()); |
||
519 | |||
520 | return $form; |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * Render the form. |
||
525 | * |
||
526 | * @return \Illuminate\View\View |
||
527 | */ |
||
528 | public function render() |
||
532 | } |
||
533 |
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.