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 HasMany 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 HasMany, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class HasMany extends Field |
||
18 | { |
||
19 | /** |
||
20 | * Relation name. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $relationName = ''; |
||
25 | |||
26 | /** |
||
27 | * Form builder. |
||
28 | * |
||
29 | * @var \Closure |
||
30 | */ |
||
31 | protected $builder = null; |
||
32 | |||
33 | /** |
||
34 | * Form data. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $value = []; |
||
39 | |||
40 | /** |
||
41 | * View Mode. |
||
42 | * |
||
43 | * Supports `default` and `tab` currently. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $viewMode = 'default'; |
||
48 | |||
49 | /** |
||
50 | * Available views for HasMany field. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $views = [ |
||
55 | 'default' => 'admin::form.hasmany', |
||
56 | 'tab' => 'admin::form.hasmanytab', |
||
57 | 'table' => 'admin::form.hasmanytable', |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * Options for template. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $options = [ |
||
66 | 'allowCreate' => true, |
||
67 | 'allowDelete' => true, |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * Distinct fields. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $distinctFields = []; |
||
76 | |||
77 | /** |
||
78 | * Create a new HasMany field instance. |
||
79 | * |
||
80 | * @param $relationName |
||
81 | * @param array $arguments |
||
82 | */ |
||
83 | View Code Duplication | public function __construct($relationName, $arguments = []) |
|
98 | |||
99 | /** |
||
100 | * Get validator for this field. |
||
101 | * |
||
102 | * @param array $input |
||
103 | * |
||
104 | * @return bool|\Illuminate\Contracts\Validation\Validator |
||
105 | */ |
||
106 | public function getValidator(array $input) |
||
171 | |||
172 | /** |
||
173 | * Set distinct fields. |
||
174 | * |
||
175 | * @param array $fields |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function distinctFields(array $fields) |
||
185 | |||
186 | /** |
||
187 | * Append distinct rules. |
||
188 | * |
||
189 | * @param array $rules |
||
190 | */ |
||
191 | protected function appendDistinctRules(array &$rules) |
||
197 | |||
198 | /** |
||
199 | * Format validation attributes. |
||
200 | * |
||
201 | * @param array $input |
||
202 | * @param string $label |
||
203 | * @param string $column |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | View Code Duplication | protected function formatValidationAttribute($input, $label, $column) |
|
233 | |||
234 | /** |
||
235 | * Reset input key for validation. |
||
236 | * |
||
237 | * @param array $input |
||
238 | * @param array $column $column is the column name array set |
||
239 | * |
||
240 | * @return void. |
||
241 | */ |
||
242 | protected function resetInputKey(array &$input, array $column) |
||
297 | |||
298 | /** |
||
299 | * Prepare input data for insert or update. |
||
300 | * |
||
301 | * @param array $input |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | public function prepare($input) |
||
311 | |||
312 | /** |
||
313 | * Build a Nested form. |
||
314 | * |
||
315 | * @param string $column |
||
316 | * @param \Closure $builder |
||
317 | * @param null $model |
||
318 | * |
||
319 | * @return NestedForm |
||
320 | */ |
||
321 | View Code Duplication | protected function buildNestedForm($column, \Closure $builder, $model = null) |
|
335 | |||
336 | /** |
||
337 | * Get the HasMany relation key name. |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | protected function getKeyName() |
||
349 | |||
350 | /** |
||
351 | * Set view mode. |
||
352 | * |
||
353 | * @param string $mode currently support `tab` mode. |
||
354 | * |
||
355 | * @return $this |
||
356 | * |
||
357 | * @author Edwin Hui |
||
358 | */ |
||
359 | public function mode($mode) |
||
365 | |||
366 | /** |
||
367 | * Use tab mode to showing hasmany field. |
||
368 | * |
||
369 | * @return HasMany |
||
370 | */ |
||
371 | public function useTab() |
||
375 | |||
376 | /** |
||
377 | * Use table mode to showing hasmany field. |
||
378 | * |
||
379 | * @return HasMany |
||
380 | */ |
||
381 | public function useTable() |
||
385 | |||
386 | /** |
||
387 | * Build Nested form for related data. |
||
388 | * |
||
389 | * @throws \Exception |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | protected function buildRelatedForms() |
||
444 | |||
445 | /** |
||
446 | * Setup script for this field in different view mode. |
||
447 | * |
||
448 | * @param string $script |
||
449 | * |
||
450 | * @return void |
||
451 | */ |
||
452 | protected function setupScript($script) |
||
458 | |||
459 | /** |
||
460 | * Setup default template script. |
||
461 | * |
||
462 | * @param string $templateScript |
||
463 | * |
||
464 | * @return void |
||
465 | */ |
||
466 | View Code Duplication | protected function setupScriptForDefaultView($templateScript) |
|
502 | |||
503 | /** |
||
504 | * Setup tab template script. |
||
505 | * |
||
506 | * @param string $templateScript |
||
507 | * |
||
508 | * @return void |
||
509 | */ |
||
510 | View Code Duplication | protected function setupScriptForTabView($templateScript) |
|
557 | |||
558 | /** |
||
559 | * Setup default template script. |
||
560 | * |
||
561 | * @param string $templateScript |
||
562 | * |
||
563 | * @return void |
||
564 | */ |
||
565 | View Code Duplication | protected function setupScriptForTableView($templateScript) |
|
601 | |||
602 | /** |
||
603 | * Disable create button. |
||
604 | * |
||
605 | * @return $this |
||
606 | */ |
||
607 | public function disableCreate() |
||
613 | |||
614 | /** |
||
615 | * Disable delete button. |
||
616 | * |
||
617 | * @return $this |
||
618 | */ |
||
619 | public function disableDelete() |
||
625 | |||
626 | /** |
||
627 | * Render the `HasMany` field. |
||
628 | * |
||
629 | * @throws \Exception |
||
630 | * |
||
631 | * @return \Illuminate\View\View |
||
632 | */ |
||
633 | public function render() |
||
654 | |||
655 | /** |
||
656 | * Render the `HasMany` field for table style. |
||
657 | * |
||
658 | * @throws \Exception |
||
659 | * |
||
660 | * @return mixed |
||
661 | */ |
||
662 | protected function renderTable() |
||
712 | } |
||
713 |
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.