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 Field 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 Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Field implements Renderable |
||
17 | { |
||
18 | use Macroable; |
||
19 | |||
20 | const FILE_DELETE_FLAG = '_file_del_'; |
||
21 | |||
22 | /** |
||
23 | * Element id. |
||
24 | * |
||
25 | * @var array|string |
||
26 | */ |
||
27 | protected $id; |
||
28 | |||
29 | /** |
||
30 | * Element value. |
||
31 | * |
||
32 | * @var mixed |
||
33 | */ |
||
34 | protected $value; |
||
35 | |||
36 | /** |
||
37 | * Field original value. |
||
38 | * |
||
39 | * @var mixed |
||
40 | */ |
||
41 | protected $original; |
||
42 | |||
43 | /** |
||
44 | * Field default value. |
||
45 | * |
||
46 | * @var mixed |
||
47 | */ |
||
48 | protected $default; |
||
49 | |||
50 | /** |
||
51 | * Element label. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $label = ''; |
||
56 | |||
57 | /** |
||
58 | * Column name. |
||
59 | * |
||
60 | * @var string|array |
||
61 | */ |
||
62 | protected $column = ''; |
||
63 | |||
64 | /** |
||
65 | * Form element name. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $elementName = []; |
||
70 | |||
71 | /** |
||
72 | * Form element classes. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $elementClass = []; |
||
77 | |||
78 | /** |
||
79 | * Variables of elements. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $variables = []; |
||
84 | |||
85 | /** |
||
86 | * Options for specify elements. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $options = []; |
||
91 | |||
92 | /** |
||
93 | * Checked for specify elements. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $checked = []; |
||
98 | |||
99 | /** |
||
100 | * Validation rules. |
||
101 | * |
||
102 | * @var string|\Closure |
||
103 | */ |
||
104 | protected $rules = ''; |
||
105 | |||
106 | /** |
||
107 | * @var callable |
||
108 | */ |
||
109 | protected $validator; |
||
110 | |||
111 | /** |
||
112 | * Validation messages. |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | protected $validationMessages = []; |
||
117 | |||
118 | /** |
||
119 | * Css required by this field. |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected static $css = []; |
||
124 | |||
125 | /** |
||
126 | * Js required by this field. |
||
127 | * |
||
128 | * @var array |
||
129 | */ |
||
130 | protected static $js = []; |
||
131 | |||
132 | /** |
||
133 | * Script for field. |
||
134 | * |
||
135 | * @var string |
||
136 | */ |
||
137 | protected $script = ''; |
||
138 | |||
139 | /** |
||
140 | * Element attributes. |
||
141 | * |
||
142 | * @var array |
||
143 | */ |
||
144 | protected $attributes = []; |
||
145 | |||
146 | /** |
||
147 | * Parent form. |
||
148 | * |
||
149 | * @var Form |
||
150 | */ |
||
151 | protected $form = null; |
||
152 | |||
153 | /** |
||
154 | * View for field to render. |
||
155 | * |
||
156 | * @var string |
||
157 | */ |
||
158 | protected $view = ''; |
||
159 | |||
160 | /** |
||
161 | * Help block. |
||
162 | * |
||
163 | * @var array |
||
164 | */ |
||
165 | protected $help = []; |
||
166 | |||
167 | /** |
||
168 | * Key for errors. |
||
169 | * |
||
170 | * @var mixed |
||
171 | */ |
||
172 | protected $errorKey; |
||
173 | |||
174 | /** |
||
175 | * Placeholder for this field. |
||
176 | * |
||
177 | * @var string|array |
||
178 | */ |
||
179 | protected $placeholder; |
||
180 | |||
181 | /** |
||
182 | * Width for label and field. |
||
183 | * |
||
184 | * @var array |
||
185 | */ |
||
186 | protected $width = [ |
||
187 | 'label' => 2, |
||
188 | 'field' => 8, |
||
189 | ]; |
||
190 | |||
191 | /** |
||
192 | * If the form horizontal layout. |
||
193 | * |
||
194 | * @var bool |
||
195 | */ |
||
196 | protected $horizontal = true; |
||
197 | |||
198 | /** |
||
199 | * column data format. |
||
200 | * |
||
201 | * @var Closure |
||
202 | */ |
||
203 | protected $customFormat = null; |
||
204 | |||
205 | /** |
||
206 | * @var bool |
||
207 | */ |
||
208 | protected $display = true; |
||
209 | |||
210 | /** |
||
211 | * @var array |
||
212 | */ |
||
213 | protected $labelClass = []; |
||
214 | |||
215 | /** |
||
216 | * Field constructor. |
||
217 | * |
||
218 | * @param $column |
||
219 | * @param array $arguments |
||
220 | */ |
||
221 | public function __construct($column, $arguments = []) |
||
227 | |||
228 | /** |
||
229 | * Get assets required by this field. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public static function getAssets() |
||
240 | |||
241 | /** |
||
242 | * Format the field element id. |
||
243 | * |
||
244 | * @param string|array $column |
||
245 | * |
||
246 | * @return string|array |
||
247 | */ |
||
248 | protected function formatId($column) |
||
252 | |||
253 | /** |
||
254 | * Format the label value. |
||
255 | * |
||
256 | * @param array $arguments |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | protected function formatLabel($arguments = []) |
||
268 | |||
269 | /** |
||
270 | * Format the name of the field. |
||
271 | * |
||
272 | * @param string $column |
||
273 | * |
||
274 | * @return array|mixed|string |
||
275 | */ |
||
276 | protected function formatName($column) |
||
304 | |||
305 | /** |
||
306 | * Set form element name. |
||
307 | * |
||
308 | * @param string $name |
||
309 | * |
||
310 | * @return $this |
||
311 | * |
||
312 | * @author Edwin Hui |
||
313 | */ |
||
314 | public function setElementName($name) |
||
320 | |||
321 | /** |
||
322 | * Fill data to the field. |
||
323 | * |
||
324 | * @param array $data |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | public function fill($data) |
||
348 | |||
349 | /** |
||
350 | * custom format form column data when edit. |
||
351 | * |
||
352 | * @param Closure $call |
||
353 | * |
||
354 | * @return [null] |
||
|
|||
355 | */ |
||
356 | public function customFormat(\Closure $call) |
||
360 | |||
361 | /** |
||
362 | * Set original value to the field. |
||
363 | * |
||
364 | * @param array $data |
||
365 | * |
||
366 | * @return void |
||
367 | */ |
||
368 | public function setOriginal($data) |
||
380 | |||
381 | /** |
||
382 | * @param Form $form |
||
383 | * |
||
384 | * @return $this |
||
385 | */ |
||
386 | public function setForm(Form $form = null) |
||
392 | |||
393 | /** |
||
394 | * Set width for field and label. |
||
395 | * |
||
396 | * @param int $field |
||
397 | * @param int $label |
||
398 | * |
||
399 | * @return $this |
||
400 | */ |
||
401 | public function setWidth($field = 8, $label = 2) |
||
410 | |||
411 | /** |
||
412 | * Set the field options. |
||
413 | * |
||
414 | * @param array $options |
||
415 | * |
||
416 | * @return $this |
||
417 | */ |
||
418 | View Code Duplication | public function options($options = []) |
|
428 | |||
429 | /** |
||
430 | * Set the field option checked. |
||
431 | * |
||
432 | * @param array $checked |
||
433 | * |
||
434 | * @return $this |
||
435 | */ |
||
436 | View Code Duplication | public function checked($checked = []) |
|
446 | |||
447 | /** |
||
448 | * Get or set rules. |
||
449 | * |
||
450 | * @param null $rules |
||
451 | * @param array $messages |
||
452 | * |
||
453 | * @return $this |
||
454 | */ |
||
455 | public function rules($rules = null, $messages = []) |
||
475 | |||
476 | /** |
||
477 | * Get field validation rules. |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | protected function getRules() |
||
489 | |||
490 | /** |
||
491 | * Remove a specific rule by keyword. |
||
492 | * |
||
493 | * @param string $rule |
||
494 | * |
||
495 | * @return void |
||
496 | */ |
||
497 | protected function removeRule($rule) |
||
506 | |||
507 | /** |
||
508 | * Set field validator. |
||
509 | * |
||
510 | * @param callable $validator |
||
511 | * |
||
512 | * @return $this |
||
513 | */ |
||
514 | public function validator(callable $validator) |
||
520 | |||
521 | /** |
||
522 | * Get key for error message. |
||
523 | * |
||
524 | * @return string |
||
525 | */ |
||
526 | public function getErrorKey() |
||
530 | |||
531 | /** |
||
532 | * Set key for error message. |
||
533 | * |
||
534 | * @param string $key |
||
535 | * |
||
536 | * @return $this |
||
537 | */ |
||
538 | public function setErrorKey($key) |
||
544 | |||
545 | /** |
||
546 | * Set or get value of the field. |
||
547 | * |
||
548 | * @param null $value |
||
549 | * |
||
550 | * @return mixed |
||
551 | */ |
||
552 | View Code Duplication | public function value($value = null) |
|
562 | |||
563 | /** |
||
564 | * Set default value for field. |
||
565 | * |
||
566 | * @param $default |
||
567 | * |
||
568 | * @return $this |
||
569 | */ |
||
570 | public function default($default) |
||
576 | |||
577 | /** |
||
578 | * Get default value. |
||
579 | * |
||
580 | * @return mixed |
||
581 | */ |
||
582 | public function getDefault() |
||
590 | |||
591 | /** |
||
592 | * Set help block for current field. |
||
593 | * |
||
594 | * @param string $text |
||
595 | * @param string $icon |
||
596 | * |
||
597 | * @return $this |
||
598 | */ |
||
599 | public function help($text = '', $icon = 'fa-info-circle') |
||
605 | |||
606 | /** |
||
607 | * Get column of the field. |
||
608 | * |
||
609 | * @return string|array |
||
610 | */ |
||
611 | public function column() |
||
615 | |||
616 | /** |
||
617 | * Get label of the field. |
||
618 | * |
||
619 | * @return string |
||
620 | */ |
||
621 | public function label() |
||
625 | |||
626 | /** |
||
627 | * Get original value of the field. |
||
628 | * |
||
629 | * @return mixed |
||
630 | */ |
||
631 | public function original() |
||
635 | |||
636 | /** |
||
637 | * Get validator for this field. |
||
638 | * |
||
639 | * @param array $input |
||
640 | * |
||
641 | * @return bool|Validator |
||
642 | */ |
||
643 | public function getValidator(array $input) |
||
679 | |||
680 | /** |
||
681 | * Sanitize input data. |
||
682 | * |
||
683 | * @param array $input |
||
684 | * @param string $column |
||
685 | * |
||
686 | * @return array |
||
687 | */ |
||
688 | protected function sanitizeInput($input, $column) |
||
697 | |||
698 | /** |
||
699 | * Add html attributes to elements. |
||
700 | * |
||
701 | * @param array|string $attribute |
||
702 | * @param mixed $value |
||
703 | * |
||
704 | * @return $this |
||
705 | */ |
||
706 | public function attribute($attribute, $value = null) |
||
716 | |||
717 | |||
718 | /** |
||
719 | * Set the field automatically get focus. |
||
720 | * |
||
721 | * @return Field |
||
722 | */ |
||
723 | public function autofocus() |
||
727 | |||
728 | |||
729 | /** |
||
730 | * Set the field as readonly mode. |
||
731 | * |
||
732 | * @return Field |
||
733 | */ |
||
734 | public function readOnly() |
||
738 | |||
739 | /** |
||
740 | * Set field as disabled. |
||
741 | * |
||
742 | * @return Field |
||
743 | */ |
||
744 | public function disable() |
||
748 | |||
749 | /** |
||
750 | * Set field placeholder. |
||
751 | * |
||
752 | * @param string $placeholder |
||
753 | * |
||
754 | * @return Field |
||
755 | */ |
||
756 | public function placeholder($placeholder = '') |
||
762 | |||
763 | /** |
||
764 | * Get placeholder. |
||
765 | * |
||
766 | * @return string |
||
767 | */ |
||
768 | public function getPlaceholder() |
||
772 | |||
773 | /** |
||
774 | * Prepare for a field value before update or insert. |
||
775 | * |
||
776 | * @param $value |
||
777 | * |
||
778 | * @return mixed |
||
779 | */ |
||
780 | public function prepare($value) |
||
784 | |||
785 | /** |
||
786 | * Format the field attributes. |
||
787 | * |
||
788 | * @return string |
||
789 | */ |
||
790 | protected function formatAttributes() |
||
800 | |||
801 | /** |
||
802 | * @return $this |
||
803 | */ |
||
804 | public function disableHorizontal() |
||
810 | |||
811 | /** |
||
812 | * @return array |
||
813 | */ |
||
814 | public function getViewElementClasses() |
||
826 | |||
827 | /** |
||
828 | * Set form element class. |
||
829 | * |
||
830 | * @param string|array $class |
||
831 | * |
||
832 | * @return $this |
||
833 | */ |
||
834 | public function setElementClass($class) |
||
840 | |||
841 | /** |
||
842 | * Get element class. |
||
843 | * |
||
844 | * @return array |
||
845 | */ |
||
846 | protected function getElementClass() |
||
856 | |||
857 | /** |
||
858 | * Get element class string. |
||
859 | * |
||
860 | * @return mixed |
||
861 | */ |
||
862 | View Code Duplication | protected function getElementClassString() |
|
878 | |||
879 | /** |
||
880 | * Get element class selector. |
||
881 | * |
||
882 | * @return string |
||
883 | */ |
||
884 | View Code Duplication | protected function getElementClassSelector() |
|
900 | |||
901 | /** |
||
902 | * Add the element class. |
||
903 | * |
||
904 | * @param $class |
||
905 | * |
||
906 | * @return $this |
||
907 | */ |
||
908 | public function addElementClass($class) |
||
918 | |||
919 | /** |
||
920 | * Remove element class. |
||
921 | * |
||
922 | * @param $class |
||
923 | * |
||
924 | * @return $this |
||
925 | */ |
||
926 | public function removeElementClass($class) |
||
942 | |||
943 | /** |
||
944 | * Add variables to field view. |
||
945 | * |
||
946 | * @param array $variables |
||
947 | * |
||
948 | * @return $this |
||
949 | */ |
||
950 | protected function addVariables(array $variables = []) |
||
956 | |||
957 | /** |
||
958 | * @return string |
||
959 | */ |
||
960 | public function getLabelClass() |
||
965 | |||
966 | /** |
||
967 | * @param array $labelClass |
||
968 | * |
||
969 | * @return self |
||
970 | */ |
||
971 | public function setLabelClass(array $labelClass) |
||
978 | |||
979 | /** |
||
980 | * Get the view variables of this field. |
||
981 | * |
||
982 | * @return array |
||
983 | */ |
||
984 | public function variables() |
||
1000 | |||
1001 | /** |
||
1002 | * Get view of this field. |
||
1003 | * |
||
1004 | * @return string |
||
1005 | */ |
||
1006 | public function getView() |
||
1016 | |||
1017 | /** |
||
1018 | * Get script of current field. |
||
1019 | * |
||
1020 | * @return string |
||
1021 | */ |
||
1022 | public function getScript() |
||
1026 | |||
1027 | /** |
||
1028 | * Render this filed. |
||
1029 | * |
||
1030 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
1031 | */ |
||
1032 | public function render() |
||
1042 | |||
1043 | /** |
||
1044 | * @return string |
||
1045 | */ |
||
1046 | public function __toString() |
||
1050 | } |
||
1051 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.