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 Form 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 Form, and based on these observations, apply Extract Interface, too.
1 | <?php /** MicroForm */ |
||
19 | class Form |
||
20 | { |
||
21 | /** |
||
22 | * Render label tag |
||
23 | * |
||
24 | * @access public |
||
25 | * |
||
26 | * @param IFormModel $model model |
||
27 | * @param string $property model property |
||
28 | * @param array $options attributes array |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | public function label(IFormModel $model, $property, array $options = []) |
||
38 | |||
39 | /** |
||
40 | * Get model field data |
||
41 | * |
||
42 | * @access private |
||
43 | * |
||
44 | * @param IFormModel $model model |
||
45 | * @param string $property model property |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | protected function getField(IFormModel $model, $property) |
||
60 | |||
61 | /** |
||
62 | * Render hidden field tag |
||
63 | * |
||
64 | * @access public |
||
65 | * |
||
66 | * @param IFormModel $model model |
||
67 | * @param string $property model property |
||
68 | * @param array $options attributes array |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function hiddenField(IFormModel $model, $property, array $options = []) |
||
78 | |||
79 | /** |
||
80 | * Render button field tag |
||
81 | * |
||
82 | * @access public |
||
83 | * |
||
84 | * @param IFormModel $model model |
||
85 | * @param string $property model property |
||
86 | * @param array $options attributes array |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | View Code Duplication | public function buttonField(IFormModel $model, $property, array $options = []) |
|
97 | |||
98 | /** |
||
99 | * Render radio field tag |
||
100 | * |
||
101 | * @access public |
||
102 | * |
||
103 | * @param IFormModel $model model |
||
104 | * @param string $property model property |
||
105 | * @param array $options attributes array |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | View Code Duplication | public function radioField(IFormModel $model, $property, array $options = []) |
|
116 | |||
117 | /** |
||
118 | * Render text field row |
||
119 | * |
||
120 | * @access public |
||
121 | * |
||
122 | * @param IFormModel $model model |
||
123 | * @param string $property model property |
||
124 | * @param array $options attribute array |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | View Code Duplication | public function textFieldRow(IFormModel $model, $property, array $options = []) |
|
138 | |||
139 | // Fields |
||
140 | |||
141 | /** |
||
142 | * Get block from options |
||
143 | * |
||
144 | * @access protected |
||
145 | * |
||
146 | * @param string $name Block name |
||
147 | * @param array $options Options array |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | protected function getBlock($name, array &$options) |
||
161 | |||
162 | /** |
||
163 | * Render text field tag |
||
164 | * |
||
165 | * @access public |
||
166 | * |
||
167 | * @param IFormModel $model model |
||
168 | * @param string $property model property |
||
169 | * @param array $options attributes array |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | View Code Duplication | public function textField(IFormModel $model, $property, array $options = []) |
|
180 | |||
181 | /** |
||
182 | * Render file field row |
||
183 | * |
||
184 | * @access public |
||
185 | * |
||
186 | * @param IFormModel $model model |
||
187 | * @param string $property model property |
||
188 | * @param array $options attribute array |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | View Code Duplication | public function fileFieldRow(IFormModel $model, $property, array $options = []) |
|
202 | |||
203 | /** |
||
204 | * Render file field tag |
||
205 | * |
||
206 | * @access public |
||
207 | * |
||
208 | * @param IFormModel $model model |
||
209 | * @param string $property model property |
||
210 | * @param array $options attributes array |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | View Code Duplication | public function fileField(IFormModel $model, $property, array $options = []) |
|
221 | |||
222 | /** |
||
223 | * Render image field row |
||
224 | * |
||
225 | * @access public |
||
226 | * |
||
227 | * @param IFormModel $model model |
||
228 | * @param string $property model property |
||
229 | * @param array $options attribute array |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | View Code Duplication | public function imageFieldRow(IFormModel $model, $property, array $options = []) |
|
243 | |||
244 | /** |
||
245 | * Render image field tag |
||
246 | * |
||
247 | * @access public |
||
248 | * |
||
249 | * @param IFormModel $model model |
||
250 | * @param string $property model property |
||
251 | * @param array $options attributes array |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function imageField(IFormModel $model, $property, array $options = []) |
||
263 | |||
264 | /** |
||
265 | * Render password field row |
||
266 | * |
||
267 | * @access public |
||
268 | * |
||
269 | * @param IFormModel $model model |
||
270 | * @param string $property model property |
||
271 | * @param array $options attribute array |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | View Code Duplication | public function passwordFieldRow(IFormModel $model, $property, array $options = []) |
|
285 | |||
286 | /** |
||
287 | * Render password field tag |
||
288 | * |
||
289 | * @access public |
||
290 | * |
||
291 | * @param IFormModel $model model |
||
292 | * @param string $property model property |
||
293 | * @param array $options attributes array |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | View Code Duplication | public function passwordField(IFormModel $model, $property, array $options = []) |
|
304 | |||
305 | /** |
||
306 | * Render textArea field row |
||
307 | * |
||
308 | * @access public |
||
309 | * |
||
310 | * @param IFormModel $model model |
||
311 | * @param string $property model property |
||
312 | * @param array $options attribute array |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | View Code Duplication | public function textAreaFieldRow(IFormModel $model, $property, array $options = []) |
|
326 | |||
327 | /** |
||
328 | * Render textarea tag |
||
329 | * |
||
330 | * @access public |
||
331 | * |
||
332 | * @param IFormModel $model model |
||
333 | * @param string $property model property |
||
334 | * @param array $options attributes array |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | View Code Duplication | public function textAreaField(IFormModel $model, $property, array $options = []) |
|
345 | |||
346 | /** |
||
347 | * Render checkbox field row |
||
348 | * |
||
349 | * @access public |
||
350 | * |
||
351 | * @param IFormModel $model model |
||
352 | * @param string $property model property |
||
353 | * @param array $options attribute array |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | View Code Duplication | public function checkboxFieldRow(IFormModel $model, $property, array $options = []) |
|
367 | |||
368 | /** |
||
369 | * Render check box field tag |
||
370 | * |
||
371 | * @access public |
||
372 | * |
||
373 | * @param IFormModel $model model |
||
374 | * @param string $property model property |
||
375 | * @param array $options attributes array |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | View Code Duplication | public function checkBoxField(IFormModel $model, $property, array $options = []) |
|
386 | |||
387 | // Lists |
||
388 | |||
389 | /** |
||
390 | * Render listBox field row |
||
391 | * |
||
392 | * @access public |
||
393 | * |
||
394 | * @param IFormModel $model model |
||
395 | * @param string $property model property |
||
396 | * @param array $options attribute array |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | View Code Duplication | public function listBoxFieldRow(IFormModel $model, $property, array $options = []) |
|
410 | |||
411 | /** |
||
412 | * Render list box tag |
||
413 | * |
||
414 | * @access public |
||
415 | * |
||
416 | * @param IFormModel $model model |
||
417 | * @param string $property model property |
||
418 | * @param array $options attributes array |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | View Code Duplication | public function listBoxField(IFormModel $model, $property, array $options = []) |
|
434 | |||
435 | /** |
||
436 | * Render text field row |
||
437 | * |
||
438 | * @access public |
||
439 | * |
||
440 | * @param IFormModel $model model |
||
441 | * @param string $property model property |
||
442 | * @param array $options attribute array |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | View Code Duplication | public function dropDownListFieldRow(IFormModel $model, $property, array $options = []) |
|
456 | |||
457 | /** |
||
458 | * Render drop down list tag |
||
459 | * |
||
460 | * @access public |
||
461 | * |
||
462 | * @param IFormModel $model model |
||
463 | * @param string $property model property |
||
464 | * @param array $options attribute array |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | View Code Duplication | public function dropDownListField(IFormModel $model, $property, array $options = []) |
|
479 | |||
480 | /** |
||
481 | * Render text field row |
||
482 | * |
||
483 | * @access public |
||
484 | * |
||
485 | * @param IFormModel $model model |
||
486 | * @param string $property model property |
||
487 | * @param array $options options array |
||
488 | * |
||
489 | * @return string |
||
490 | */ |
||
491 | View Code Duplication | public function checkBoxListFieldRow(IFormModel $model, $property, array $options = []) |
|
501 | |||
502 | /** |
||
503 | * Render check box list tag |
||
504 | * |
||
505 | * @access public |
||
506 | * |
||
507 | * @param IFormModel $model model |
||
508 | * @param string $property property model |
||
509 | * @param array $options options array |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | public function checkBoxListField(IFormModel $model, $property, array $options = []) |
||
521 | |||
522 | /** |
||
523 | * Render text field row |
||
524 | * |
||
525 | * @access public |
||
526 | * |
||
527 | * @param IFormModel $model model |
||
528 | * @param string $property model property |
||
529 | * @param array $options options array |
||
530 | * |
||
531 | * @return string |
||
532 | */ |
||
533 | View Code Duplication | public function radioButtonListFieldRow(IFormModel $model, $property, array $options = []) |
|
543 | |||
544 | /** |
||
545 | * Render radio button list tag |
||
546 | * |
||
547 | * @access public |
||
548 | * |
||
549 | * @param IFormModel $model model |
||
550 | * @param string $property model property |
||
551 | * @param array $options options array |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | public function radioButtonListField(IFormModel $model, $property, array $options = []) |
||
563 | |||
564 | // HTML5 Fields |
||
565 | |||
566 | /** |
||
567 | * Render color field row |
||
568 | * |
||
569 | * @access public |
||
570 | * |
||
571 | * @param IFormModel $model model |
||
572 | * @param string $property model property |
||
573 | * @param array $options attribute array |
||
574 | * |
||
575 | * @return string |
||
576 | */ |
||
577 | View Code Duplication | public function colorFieldRow(IFormModel $model, $property, array $options = []) |
|
587 | |||
588 | /** |
||
589 | * Render color field tag |
||
590 | * |
||
591 | * @access public |
||
592 | * |
||
593 | * @param IFormModel $model model |
||
594 | * @param string $property model property |
||
595 | * @param array $options attributes array |
||
596 | * |
||
597 | * @return string |
||
598 | */ |
||
599 | View Code Duplication | public function colorField(IFormModel $model, $property, array $options = []) |
|
606 | |||
607 | /** |
||
608 | * Render number field row |
||
609 | * |
||
610 | * @access public |
||
611 | * |
||
612 | * @param IFormModel $model model |
||
613 | * @param string $property model property |
||
614 | * @param array $options attribute array |
||
615 | * |
||
616 | * @return string |
||
617 | */ |
||
618 | View Code Duplication | public function dateFieldRow(IFormModel $model, $property, array $options = []) |
|
628 | |||
629 | /** |
||
630 | * Render date field tag |
||
631 | * |
||
632 | * @access public |
||
633 | * |
||
634 | * @param IFormModel $model model |
||
635 | * @param string $property model property |
||
636 | * @param array $options attributes array |
||
637 | * |
||
638 | * @return string |
||
639 | */ |
||
640 | View Code Duplication | public function dateField(IFormModel $model, $property, array $options = []) |
|
647 | |||
648 | /** |
||
649 | * Render datetime field row |
||
650 | * |
||
651 | * @access public |
||
652 | * |
||
653 | * @param IFormModel $model model |
||
654 | * @param string $property model property |
||
655 | * @param array $options attribute array |
||
656 | * |
||
657 | * @return string |
||
658 | */ |
||
659 | View Code Duplication | public function dateTimeFieldRow(IFormModel $model, $property, array $options = []) |
|
669 | |||
670 | /** |
||
671 | * Render datetime field tag |
||
672 | * |
||
673 | * @access public |
||
674 | * |
||
675 | * @param IFormModel $model model |
||
676 | * @param string $property model property |
||
677 | * @param array $options attributes array |
||
678 | * |
||
679 | * @return string |
||
680 | */ |
||
681 | View Code Duplication | public function dateTimeField(IFormModel $model, $property, array $options = []) |
|
688 | |||
689 | /** |
||
690 | * Render datetime-local field row |
||
691 | * |
||
692 | * @access public |
||
693 | * |
||
694 | * @param IFormModel $model model |
||
695 | * @param string $property model property |
||
696 | * @param array $options attribute array |
||
697 | * |
||
698 | * @return string |
||
699 | */ |
||
700 | View Code Duplication | public function dateTimeLocalFieldRow(IFormModel $model, $property, array $options = []) |
|
710 | |||
711 | /** |
||
712 | * Render datetime-local field tag |
||
713 | * |
||
714 | * @access public |
||
715 | * |
||
716 | * @param IFormModel $model model |
||
717 | * @param string $property model property |
||
718 | * @param array $options attributes array |
||
719 | * |
||
720 | * @return string |
||
721 | */ |
||
722 | View Code Duplication | public function dateTimeLocalField(IFormModel $model, $property, array $options = []) |
|
729 | |||
730 | /** |
||
731 | * Render email field row |
||
732 | * |
||
733 | * @access public |
||
734 | * |
||
735 | * @param IFormModel $model model |
||
736 | * @param string $property model property |
||
737 | * @param array $options attribute array |
||
738 | * |
||
739 | * @return string |
||
740 | */ |
||
741 | View Code Duplication | public function emailFieldRow(IFormModel $model, $property, array $options = []) |
|
751 | |||
752 | /** |
||
753 | * Render email field tag |
||
754 | * |
||
755 | * @access public |
||
756 | * |
||
757 | * @param IFormModel $model model |
||
758 | * @param string $property model property |
||
759 | * @param array $options attributes array |
||
760 | * |
||
761 | * @return string |
||
762 | */ |
||
763 | View Code Duplication | public function emailField(IFormModel $model, $property, array $options = []) |
|
770 | |||
771 | /** |
||
772 | * Render number field row |
||
773 | * |
||
774 | * @access public |
||
775 | * |
||
776 | * @param IFormModel $model model |
||
777 | * @param string $property model property |
||
778 | * @param array $options attribute array |
||
779 | * |
||
780 | * @return string |
||
781 | */ |
||
782 | View Code Duplication | public function numberFieldRow(IFormModel $model, $property, array $options = []) |
|
792 | |||
793 | /** |
||
794 | * Render number field tag |
||
795 | * |
||
796 | * @access public |
||
797 | * |
||
798 | * @param IFormModel $model model |
||
799 | * @param string $property model property |
||
800 | * @param array $options attributes array |
||
801 | * |
||
802 | * @return string |
||
803 | */ |
||
804 | View Code Duplication | public function numberField(IFormModel $model, $property, array $options = []) |
|
811 | |||
812 | /** |
||
813 | * Render range field row |
||
814 | * |
||
815 | * @access public |
||
816 | * |
||
817 | * @param IFormModel $model model |
||
818 | * @param string $property model property |
||
819 | * @param array $options attribute array |
||
820 | * |
||
821 | * @return string |
||
822 | */ |
||
823 | View Code Duplication | public function rangeFieldRow(IFormModel $model, $property, array $options = []) |
|
833 | |||
834 | /** |
||
835 | * Render range field tag |
||
836 | * |
||
837 | * @access public |
||
838 | * |
||
839 | * @param IFormModel $model model |
||
840 | * @param string $property model property |
||
841 | * @param array $options attributes array |
||
842 | * |
||
843 | * @return string |
||
844 | */ |
||
845 | View Code Duplication | public function rangeField(IFormModel $model, $property, array $options = []) |
|
852 | |||
853 | /** |
||
854 | * Render search field row |
||
855 | * |
||
856 | * @access public |
||
857 | * |
||
858 | * @param IFormModel $model model |
||
859 | * @param string $property model property |
||
860 | * @param array $options attribute array |
||
861 | * |
||
862 | * @return string |
||
863 | */ |
||
864 | View Code Duplication | public function searchFieldRow(IFormModel $model, $property, array $options = []) |
|
874 | |||
875 | /** |
||
876 | * Render search field tag |
||
877 | * |
||
878 | * @access public |
||
879 | * |
||
880 | * @param IFormModel $model model |
||
881 | * @param string $property model property |
||
882 | * @param array $options attributes array |
||
883 | * |
||
884 | * @return string |
||
885 | */ |
||
886 | View Code Duplication | public function searchField(IFormModel $model, $property, array $options = []) |
|
893 | |||
894 | /** |
||
895 | * Render telephone field row |
||
896 | * |
||
897 | * @access public |
||
898 | * |
||
899 | * @param IFormModel $model model |
||
900 | * @param string $property model property |
||
901 | * @param array $options attribute array |
||
902 | * |
||
903 | * @return string |
||
904 | */ |
||
905 | View Code Duplication | public function telFieldRow(IFormModel $model, $property, array $options = []) |
|
915 | |||
916 | /** |
||
917 | * Render telephone tag |
||
918 | * |
||
919 | * @access public |
||
920 | * |
||
921 | * @param IFormModel $model model |
||
922 | * @param string $property model property |
||
923 | * @param array $options attributes array |
||
924 | * |
||
925 | * @return string |
||
926 | */ |
||
927 | View Code Duplication | public function telField(IFormModel $model, $property, array $options = []) |
|
934 | |||
935 | /** |
||
936 | * Render time field row |
||
937 | * |
||
938 | * @access public |
||
939 | * |
||
940 | * @param IFormModel $model model |
||
941 | * @param string $property model property |
||
942 | * @param array $options attribute array |
||
943 | * |
||
944 | * @return string |
||
945 | */ |
||
946 | View Code Duplication | public function timeFieldRow(IFormModel $model, $property, array $options = []) |
|
956 | |||
957 | /** |
||
958 | * Render time field tag |
||
959 | * |
||
960 | * @access public |
||
961 | * |
||
962 | * @param IFormModel $model model |
||
963 | * @param string $property model property |
||
964 | * @param array $options attributes array |
||
965 | * |
||
966 | * @return string |
||
967 | */ |
||
968 | View Code Duplication | public function timeField(IFormModel $model, $property, array $options = []) |
|
975 | |||
976 | /** |
||
977 | * Render url field row |
||
978 | * |
||
979 | * @access public |
||
980 | * |
||
981 | * @param IFormModel $model model |
||
982 | * @param string $property model property |
||
983 | * @param array $options attribute array |
||
984 | * |
||
985 | * @return string |
||
986 | */ |
||
987 | View Code Duplication | public function urlFieldRow(IFormModel $model, $property, array $options = []) |
|
997 | |||
998 | /** |
||
999 | * Render url field tag |
||
1000 | * |
||
1001 | * @access public |
||
1002 | * |
||
1003 | * @param IFormModel $model model |
||
1004 | * @param string $property model property |
||
1005 | * @param array $options attributes array |
||
1006 | * |
||
1007 | * @return string |
||
1008 | */ |
||
1009 | View Code Duplication | public function urlField(IFormModel $model, $property, array $options = []) |
|
1016 | |||
1017 | /** |
||
1018 | * Render month field row |
||
1019 | * |
||
1020 | * @access public |
||
1021 | * |
||
1022 | * @param IFormModel $model model |
||
1023 | * @param string $property model property |
||
1024 | * @param array $options attribute array |
||
1025 | * |
||
1026 | * @return string |
||
1027 | */ |
||
1028 | View Code Duplication | public function monthFieldRow(IFormModel $model, $property, array $options = []) |
|
1038 | |||
1039 | /** |
||
1040 | * Render moth field tag |
||
1041 | * |
||
1042 | * @access public |
||
1043 | * |
||
1044 | * @param IFormModel $model model |
||
1045 | * @param string $property model property |
||
1046 | * @param array $options attributes array |
||
1047 | * |
||
1048 | * @return string |
||
1049 | */ |
||
1050 | View Code Duplication | public function monthField(IFormModel $model, $property, array $options = []) |
|
1057 | |||
1058 | /** |
||
1059 | * Render week field row |
||
1060 | * |
||
1061 | * @access public |
||
1062 | * |
||
1063 | * @param IFormModel $model model |
||
1064 | * @param string $property model property |
||
1065 | * @param array $options attribute array |
||
1066 | * |
||
1067 | * @return string |
||
1068 | */ |
||
1069 | View Code Duplication | public function weekFieldRow(IFormModel $model, $property, array $options = []) |
|
1079 | |||
1080 | /** |
||
1081 | * Render week field tag |
||
1082 | * |
||
1083 | * @access public |
||
1084 | * |
||
1085 | * @param IFormModel $model model |
||
1086 | * @param string $property model property |
||
1087 | * @param array $options attributes array |
||
1088 | * |
||
1089 | * @return string |
||
1090 | */ |
||
1091 | View Code Duplication | public function weekField(IFormModel $model, $property, array $options = []) |
|
1098 | } |
||
1099 |
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.