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 | * Set the field as readonly mode. |
||
719 | * |
||
720 | * @return Field |
||
721 | */ |
||
722 | public function readOnly() |
||
726 | |||
727 | /** |
||
728 | * Set field as disabled. |
||
729 | * |
||
730 | * @return Field |
||
731 | */ |
||
732 | public function disable() |
||
736 | |||
737 | /** |
||
738 | * Set field placeholder. |
||
739 | * |
||
740 | * @param string $placeholder |
||
741 | * |
||
742 | * @return Field |
||
743 | */ |
||
744 | public function placeholder($placeholder = '') |
||
750 | |||
751 | /** |
||
752 | * Get placeholder. |
||
753 | * |
||
754 | * @return string |
||
755 | */ |
||
756 | public function getPlaceholder() |
||
760 | |||
761 | /** |
||
762 | * Prepare for a field value before update or insert. |
||
763 | * |
||
764 | * @param $value |
||
765 | * |
||
766 | * @return mixed |
||
767 | */ |
||
768 | public function prepare($value) |
||
772 | |||
773 | /** |
||
774 | * Format the field attributes. |
||
775 | * |
||
776 | * @return string |
||
777 | */ |
||
778 | protected function formatAttributes() |
||
788 | |||
789 | /** |
||
790 | * @return $this |
||
791 | */ |
||
792 | public function disableHorizontal() |
||
798 | |||
799 | /** |
||
800 | * @return array |
||
801 | */ |
||
802 | public function getViewElementClasses() |
||
814 | |||
815 | /** |
||
816 | * Set form element class. |
||
817 | * |
||
818 | * @param string|array $class |
||
819 | * |
||
820 | * @return $this |
||
821 | */ |
||
822 | public function setElementClass($class) |
||
828 | |||
829 | /** |
||
830 | * Get element class. |
||
831 | * |
||
832 | * @return array |
||
833 | */ |
||
834 | protected function getElementClass() |
||
844 | |||
845 | /** |
||
846 | * Get element class string. |
||
847 | * |
||
848 | * @return mixed |
||
849 | */ |
||
850 | View Code Duplication | protected function getElementClassString() |
|
866 | |||
867 | /** |
||
868 | * Get element class selector. |
||
869 | * |
||
870 | * @return string |
||
871 | */ |
||
872 | View Code Duplication | protected function getElementClassSelector() |
|
888 | |||
889 | /** |
||
890 | * Add the element class. |
||
891 | * |
||
892 | * @param $class |
||
893 | * |
||
894 | * @return $this |
||
895 | */ |
||
896 | public function addElementClass($class) |
||
906 | |||
907 | /** |
||
908 | * Remove element class. |
||
909 | * |
||
910 | * @param $class |
||
911 | * |
||
912 | * @return $this |
||
913 | */ |
||
914 | public function removeElementClass($class) |
||
930 | |||
931 | /** |
||
932 | * Add variables to field view. |
||
933 | * |
||
934 | * @param array $variables |
||
935 | * |
||
936 | * @return $this |
||
937 | */ |
||
938 | protected function addVariables(array $variables = []) |
||
944 | |||
945 | /** |
||
946 | * @return string |
||
947 | */ |
||
948 | public function getLabelClass() |
||
953 | |||
954 | /** |
||
955 | * @param array $labelClass |
||
956 | * |
||
957 | * @return self |
||
958 | */ |
||
959 | public function setLabelClass(array $labelClass) |
||
966 | |||
967 | /** |
||
968 | * Get the view variables of this field. |
||
969 | * |
||
970 | * @return array |
||
971 | */ |
||
972 | public function variables() |
||
988 | |||
989 | /** |
||
990 | * Get view of this field. |
||
991 | * |
||
992 | * @return string |
||
993 | */ |
||
994 | public function getView() |
||
1004 | |||
1005 | /** |
||
1006 | * Get script of current field. |
||
1007 | * |
||
1008 | * @return string |
||
1009 | */ |
||
1010 | public function getScript() |
||
1014 | |||
1015 | /** |
||
1016 | * Render this filed. |
||
1017 | * |
||
1018 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
1019 | */ |
||
1020 | public function render() |
||
1030 | |||
1031 | /** |
||
1032 | * @return string |
||
1033 | */ |
||
1034 | public function __toString() |
||
1038 | } |
||
1039 |
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.