Complex classes like TbInput 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 TbInput, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class TbInput extends CInputWidget |
||
18 | { |
||
19 | // The different input types. |
||
20 | const TYPE_CHECKBOX = 'checkbox'; |
||
21 | const TYPE_CHECKBOXLIST = 'checkboxlist'; |
||
22 | const TYPE_CHECKBOXLIST_INLINE = 'checkboxlist_inline'; |
||
23 | const TYPE_CHECKBOXGROUPSLIST = 'checkboxgroupslist'; |
||
24 | const TYPE_DROPDOWN = 'dropdownlist'; |
||
25 | const TYPE_FILE = 'filefield'; |
||
26 | const TYPE_PASSWORD = 'password'; |
||
27 | const TYPE_PASSFIELD = 'passfield'; |
||
28 | const TYPE_RADIO = 'radiobutton'; |
||
29 | const TYPE_RADIOLIST = 'radiobuttonlist'; |
||
30 | const TYPE_RADIOLIST_INLINE = 'radiobuttonlist_inline'; |
||
31 | const TYPE_RADIOBUTTONGROUPSLIST = 'radiobuttongroupslist'; |
||
32 | const TYPE_TEXTAREA = 'textarea'; |
||
33 | const TYPE_TEXT = 'text'; |
||
34 | const TYPE_MASKEDTEXT = 'maskedtextfield'; |
||
35 | const TYPE_CAPTCHA = 'captcha'; |
||
36 | const TYPE_UNEDITABLE = 'uneditable'; |
||
37 | const TYPE_DATEPICKER = 'datepicker'; |
||
38 | const TYPE_DATETIMEPICKER = 'datetimepicker'; |
||
39 | const TYPE_REDACTOR = 'redactor'; |
||
40 | const TYPE_MARKDOWNEDITOR = 'markdowneditor'; |
||
41 | const TYPE_HTML5EDITOR = 'wysihtml5'; |
||
42 | const TYPE_DATERANGEPICKER = 'daterangepicker'; |
||
43 | const TYPE_TOGGLEBUTTON = 'togglebutton'; |
||
44 | const TYPE_COLORPICKER = 'colorpicker'; |
||
45 | const TYPE_CKEDITOR = 'ckeditor'; |
||
46 | const TYPE_TIMEPICKER = 'timepicker'; |
||
47 | const TYPE_SELECT2 = 'select2'; |
||
48 | const TYPE_TYPEAHEAD = 'typeahead'; |
||
49 | const TYPE_NUMBER = 'numberfield'; |
||
50 | const TYPE_CUSTOM = 'custom'; |
||
51 | |||
52 | /** |
||
53 | * @var TbActiveForm the associated form widget. |
||
54 | */ |
||
55 | public $form; |
||
56 | |||
57 | /** |
||
58 | * @var string the input label text. |
||
59 | */ |
||
60 | public $label; |
||
61 | |||
62 | /** |
||
63 | * @var string the input type. |
||
64 | * |
||
65 | * Following types are supported: checkbox, checkboxlist, dropdownlist, filefield, password, |
||
66 | * radiobutton, radiobuttonlist, textarea, textfield, captcha and uneditable. |
||
67 | */ |
||
68 | public $type; |
||
69 | |||
70 | /** |
||
71 | * @var array the data for list inputs. |
||
72 | */ |
||
73 | public $data = array(); |
||
74 | |||
75 | /** |
||
76 | * @var string text to prepend. |
||
77 | */ |
||
78 | public $prependText; |
||
79 | |||
80 | /** |
||
81 | * @var string text to append. |
||
82 | */ |
||
83 | public $appendText; |
||
84 | |||
85 | /** |
||
86 | * @var string the hint text. |
||
87 | */ |
||
88 | public $hintText; |
||
89 | |||
90 | /** |
||
91 | * @var array label html attributes. |
||
92 | */ |
||
93 | public $labelOptions = array(); |
||
94 | |||
95 | /** |
||
96 | * @var array prepend html attributes. |
||
97 | */ |
||
98 | public $prependOptions = array(); |
||
99 | |||
100 | /** |
||
101 | * @var array append html attributes. |
||
102 | */ |
||
103 | public $appendOptions = array(); |
||
104 | |||
105 | /** |
||
106 | * @var array hint html attributes. |
||
107 | */ |
||
108 | public $hintOptions = array(); |
||
109 | |||
110 | /** |
||
111 | * @var array error html attributes. |
||
112 | */ |
||
113 | public $errorOptions = array(); |
||
114 | |||
115 | /** |
||
116 | * @var array captcha html attributes. |
||
117 | */ |
||
118 | public $captchaOptions = array(); |
||
119 | |||
120 | /** |
||
121 | * This property allows you to disable AJAX valiadtion for certain fields within a form. |
||
122 | * @var boolean the value to be set as fourth parameter to {@link CActiveForm::error}. |
||
123 | * @see http://www.yiiframework.com/doc/api/1.1/CActiveForm#error-detail |
||
124 | */ |
||
125 | public $enableAjaxValidation = true; |
||
126 | |||
127 | /** |
||
128 | * This property allows you to disable client valiadtion for certain fields within a form. |
||
129 | * @var boolean the value to be set as fifth parameter to {@link CActiveForm::error}. |
||
130 | * @see http://www.yiiframework.com/doc/api/1.1/CActiveForm#error-detail |
||
131 | */ |
||
132 | public $enableClientValidation = true; |
||
133 | |||
134 | /** |
||
135 | *### .init() |
||
136 | * |
||
137 | * Initializes the widget. |
||
138 | * |
||
139 | * @throws CException if the widget could not be initialized. |
||
140 | */ |
||
141 | public function init() |
||
166 | |||
167 | /** |
||
168 | *### .processHtmlOptions() |
||
169 | * |
||
170 | * Processes the html options. |
||
171 | */ |
||
172 | protected function processHtmlOptions() |
||
231 | |||
232 | /** |
||
233 | *### .run() |
||
234 | * |
||
235 | * Runs the widget. |
||
236 | * |
||
237 | * @throws CException if the widget type is invalid. |
||
238 | */ |
||
239 | public function run() |
||
371 | |||
372 | /** |
||
373 | *### .getLabel() |
||
374 | * |
||
375 | * Returns the label for the input. |
||
376 | * |
||
377 | * @return string the label |
||
378 | */ |
||
379 | protected function getLabel() |
||
389 | |||
390 | /** |
||
391 | *### .getPrepend() |
||
392 | * |
||
393 | * Returns the prepend element for the input. |
||
394 | * |
||
395 | * @return string the element |
||
396 | */ |
||
397 | protected function getPrepend() |
||
423 | |||
424 | /** |
||
425 | *### .getAppend() |
||
426 | * |
||
427 | * Returns the append element for the input. |
||
428 | * |
||
429 | * @return string the element |
||
430 | */ |
||
431 | protected function getAppend() |
||
457 | |||
458 | /** |
||
459 | *### .getAppend() |
||
460 | * |
||
461 | * Returns the id that should be used for the specified attribute |
||
462 | * |
||
463 | * @param string $attribute the attribute |
||
464 | * |
||
465 | * @return string the id |
||
466 | */ |
||
467 | protected function getAttributeId($attribute) |
||
473 | |||
474 | /** |
||
475 | *### .getError() |
||
476 | * |
||
477 | * Returns the error text for the input. |
||
478 | * |
||
479 | * @return string the error text |
||
480 | */ |
||
481 | protected function getError() |
||
491 | |||
492 | /** |
||
493 | *### .getHint() |
||
494 | * |
||
495 | * Returns the hint text for the input. |
||
496 | * |
||
497 | * @return string the hint text |
||
498 | */ |
||
499 | protected function getHint() |
||
515 | |||
516 | /** |
||
517 | *### .getContainerCssClass() |
||
518 | * |
||
519 | * Returns the container CSS class for the input. |
||
520 | * |
||
521 | * @return string the CSS class |
||
522 | */ |
||
523 | protected function getContainerCssClass() |
||
527 | |||
528 | /** |
||
529 | *### .getAddonCssClass() |
||
530 | * |
||
531 | * Returns the input container CSS classes. |
||
532 | * |
||
533 | * @return string the CSS class |
||
534 | */ |
||
535 | protected function getAddonCssClass() |
||
547 | |||
548 | /** |
||
549 | *### .hasAddOn() |
||
550 | * |
||
551 | * Returns whether the input has an add-on (prepend and/or append). |
||
552 | * |
||
553 | * @return boolean the result |
||
554 | */ |
||
555 | protected function hasAddOn() |
||
559 | |||
560 | /** |
||
561 | *### .checkBox() |
||
562 | * |
||
563 | * Renders a checkbox. |
||
564 | * |
||
565 | * @return string the rendered content |
||
566 | * @abstract |
||
567 | */ |
||
568 | abstract protected function checkBox(); |
||
569 | |||
570 | /** |
||
571 | *### .toggleButton() |
||
572 | * |
||
573 | * Renders a toggle button. |
||
574 | * |
||
575 | * @return string the rendered content |
||
576 | * @abstract |
||
577 | */ |
||
578 | abstract protected function toggleButton(); |
||
579 | |||
580 | /** |
||
581 | *### .checkBoxList() |
||
582 | * |
||
583 | * Renders a list of checkboxes. |
||
584 | * |
||
585 | * @return string the rendered content |
||
586 | * @abstract |
||
587 | */ |
||
588 | abstract protected function checkBoxList(); |
||
589 | |||
590 | /** |
||
591 | *### .checkBoxListInline() |
||
592 | * |
||
593 | * Renders a list of inline checkboxes. |
||
594 | * |
||
595 | * @return string the rendered content |
||
596 | * @abstract |
||
597 | */ |
||
598 | abstract protected function checkBoxListInline(); |
||
599 | |||
600 | /** |
||
601 | *### .checkBoxGroupsList() |
||
602 | * |
||
603 | * Renders a list of checkboxes using Button Groups. |
||
604 | * |
||
605 | * @return string the rendered content |
||
606 | * @abstract |
||
607 | */ |
||
608 | abstract protected function checkBoxGroupsList(); |
||
609 | |||
610 | /** |
||
611 | *### .dropDownList() |
||
612 | * |
||
613 | * Renders a drop down list (select). |
||
614 | * |
||
615 | * @return string the rendered content |
||
616 | * @abstract |
||
617 | */ |
||
618 | abstract protected function dropDownList(); |
||
619 | |||
620 | /** |
||
621 | *### .fileField() |
||
622 | * |
||
623 | * Renders a file field. |
||
624 | * |
||
625 | * @return string the rendered content |
||
626 | * @abstract |
||
627 | */ |
||
628 | abstract protected function fileField(); |
||
629 | |||
630 | /** |
||
631 | *### .passwordField() |
||
632 | * |
||
633 | * Renders a password field. |
||
634 | * |
||
635 | * @return string the rendered content |
||
636 | * @abstract |
||
637 | */ |
||
638 | abstract protected function passwordField(); |
||
639 | |||
640 | /** |
||
641 | *### .passfieldField() |
||
642 | * |
||
643 | * Renders a Pass*Field field. |
||
644 | * |
||
645 | * @return string the rendered content |
||
646 | * @abstract |
||
647 | */ |
||
648 | abstract protected function passfieldField(); |
||
649 | |||
650 | /** |
||
651 | *### .radioButton() |
||
652 | * |
||
653 | * Renders a radio button. |
||
654 | * |
||
655 | * @return string the rendered content |
||
656 | * @abstract |
||
657 | */ |
||
658 | abstract protected function radioButton(); |
||
659 | |||
660 | /** |
||
661 | *### .radioButtonList() |
||
662 | * |
||
663 | * Renders a list of radio buttons. |
||
664 | * |
||
665 | * @return string the rendered content |
||
666 | * @abstract |
||
667 | */ |
||
668 | abstract protected function radioButtonList(); |
||
669 | |||
670 | /** |
||
671 | *### .radioButtonListInline() |
||
672 | * |
||
673 | * Renders a list of inline radio buttons. |
||
674 | * |
||
675 | * @return string the rendered content |
||
676 | * @abstract |
||
677 | */ |
||
678 | abstract protected function radioButtonListInline(); |
||
679 | |||
680 | /** |
||
681 | *### .radioButtonGroupsList() |
||
682 | * |
||
683 | * Renders a list of radio buttons using Button Groups. |
||
684 | * |
||
685 | * @return string the rendered content |
||
686 | * @abstract |
||
687 | */ |
||
688 | abstract protected function radioButtonGroupsList(); |
||
689 | |||
690 | /** |
||
691 | *### .textArea() |
||
692 | * |
||
693 | * Renders a textarea. |
||
694 | * |
||
695 | * @return string the rendered content |
||
696 | * @abstract |
||
697 | */ |
||
698 | abstract protected function textArea(); |
||
699 | |||
700 | /** |
||
701 | *### .textField() |
||
702 | * |
||
703 | * Renders a text field. |
||
704 | * |
||
705 | * @return string the rendered content |
||
706 | * @abstract |
||
707 | */ |
||
708 | abstract protected function textField(); |
||
709 | |||
710 | /** |
||
711 | *### .maskedTextField() |
||
712 | * |
||
713 | * Renders a masked text field. |
||
714 | * |
||
715 | * @return string the rendered content |
||
716 | * @abstract |
||
717 | */ |
||
718 | abstract protected function maskedTextField(); |
||
719 | |||
720 | /** |
||
721 | *### .captcha() |
||
722 | * |
||
723 | * Renders a CAPTCHA. |
||
724 | * |
||
725 | * @return string the rendered content |
||
726 | * @abstract |
||
727 | */ |
||
728 | abstract protected function captcha(); |
||
729 | |||
730 | /** |
||
731 | *### .uneditableField() |
||
732 | * |
||
733 | * Renders an uneditable field. |
||
734 | * |
||
735 | * @return string the rendered content |
||
736 | * @abstract |
||
737 | */ |
||
738 | abstract protected function uneditableField(); |
||
739 | |||
740 | /** |
||
741 | *### .datepicketField() |
||
742 | * |
||
743 | * Renders a datepicker field. |
||
744 | * |
||
745 | * @return string the rendered content |
||
746 | * @abstract |
||
747 | */ |
||
748 | abstract protected function datepickerField(); |
||
749 | |||
750 | /** |
||
751 | *### .datetimepicketField() |
||
752 | * |
||
753 | * Renders a datetimepicker field. |
||
754 | * |
||
755 | * @return string the rendered content |
||
756 | * @abstract |
||
757 | */ |
||
758 | abstract protected function datetimepickerField(); |
||
759 | |||
760 | /** |
||
761 | *### .redactorJs() |
||
762 | * |
||
763 | * Renders a redactorJS wysiwyg field. |
||
764 | * |
||
765 | * @abstract |
||
766 | * @return mixed |
||
767 | */ |
||
768 | abstract protected function redactorJs(); |
||
769 | |||
770 | /** |
||
771 | *### .markdownEditorJs() |
||
772 | * |
||
773 | * Renders a markdownEditorJS wysiwyg field. |
||
774 | * |
||
775 | * @abstract |
||
776 | * @return mixed |
||
777 | */ |
||
778 | abstract protected function markdownEditorJs(); |
||
779 | |||
780 | /** |
||
781 | *### .ckEditor() |
||
782 | * |
||
783 | * Renders a bootstrap CKEditor wysiwyg editor. |
||
784 | * |
||
785 | * @abstract |
||
786 | * @return mixed |
||
787 | */ |
||
788 | abstract protected function ckEditor(); |
||
789 | |||
790 | /** |
||
791 | *### .html5Editor() |
||
792 | * |
||
793 | * Renders a bootstrap wysihtml5 editor. |
||
794 | * |
||
795 | * @abstract |
||
796 | * @return mixed |
||
797 | */ |
||
798 | abstract protected function html5Editor(); |
||
799 | |||
800 | /** |
||
801 | *### .dateRangeField() |
||
802 | * |
||
803 | * Renders a daterange picker field |
||
804 | * |
||
805 | * @abstract |
||
806 | * @return mixed |
||
807 | */ |
||
808 | abstract protected function dateRangeField(); |
||
809 | |||
810 | /** |
||
811 | *### .colorpickerField() |
||
812 | * |
||
813 | * Renders a colorpicker field. |
||
814 | * |
||
815 | * @return string the rendered content |
||
816 | * @abstract |
||
817 | */ |
||
818 | abstract protected function colorpickerField(); |
||
819 | |||
820 | /** |
||
821 | *### .timepickerField() |
||
822 | * |
||
823 | * Renders a timepicker field. |
||
824 | * |
||
825 | * @return string the rendered content |
||
826 | * @abstract |
||
827 | */ |
||
828 | abstract protected function timepickerField(); |
||
829 | |||
830 | /** |
||
831 | *### .select2Field() |
||
832 | * |
||
833 | * Renders a select2 field. |
||
834 | * |
||
835 | * @return mixed |
||
836 | */ |
||
837 | abstract protected function select2Field(); |
||
838 | |||
839 | /** |
||
840 | * Renders a typeAhead field. |
||
841 | * @return mixed |
||
842 | */ |
||
843 | abstract protected function typeAheadField(); |
||
844 | |||
845 | /** |
||
846 | *### . numberField() |
||
847 | * |
||
848 | * Renders a number field. |
||
849 | * |
||
850 | * @return string the rendered content |
||
851 | * @abstract |
||
852 | */ |
||
853 | abstract protected function numberField(); |
||
854 | |||
855 | /** |
||
856 | *### . customField() |
||
857 | * |
||
858 | * Renders a pre-rendered custom field. |
||
859 | * |
||
860 | * @return string the rendered content |
||
861 | * @abstract |
||
862 | */ |
||
863 | abstract protected function customField(); |
||
864 | |||
865 | /** |
||
866 | * Obtain separately hidden and visible field |
||
867 | * @see TbInputVertical::checkBox |
||
868 | * @see TbInputHorizontal::checkBox |
||
869 | * @see TbInputVertical::radioButton |
||
870 | * @see TbInputHorizontal::radioButton |
||
871 | * @throws CException |
||
872 | * @return array |
||
873 | */ |
||
874 | protected function getSeparatedSelectableInput() |
||
903 | } |
||
904 |