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 EditableFormField 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 EditableFormField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class EditableFormField extends DataObject |
||
63 | { |
||
64 | /** |
||
65 | * Set to true to hide from class selector |
||
66 | * |
||
67 | * @config |
||
68 | * @var bool |
||
69 | */ |
||
70 | private static $hidden = false; |
||
|
|||
71 | |||
72 | /** |
||
73 | * Define this field as abstract (not inherited) |
||
74 | * |
||
75 | * @config |
||
76 | * @var bool |
||
77 | */ |
||
78 | private static $abstract = true; |
||
79 | |||
80 | /** |
||
81 | * Flag this field type as non-data (e.g. literal, header, html) |
||
82 | * |
||
83 | * @config |
||
84 | * @var bool |
||
85 | */ |
||
86 | private static $literal = false; |
||
87 | |||
88 | /** |
||
89 | * Default sort order |
||
90 | * |
||
91 | * @config |
||
92 | * @var string |
||
93 | */ |
||
94 | private static $default_sort = '"Sort"'; |
||
95 | |||
96 | /** |
||
97 | * A list of CSS classes that can be added |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | public static $allowed_css = []; |
||
102 | |||
103 | /** |
||
104 | * Set this to true to enable placeholder field for any given class |
||
105 | * @config |
||
106 | * @var bool |
||
107 | */ |
||
108 | private static $has_placeholder = false; |
||
109 | |||
110 | /** |
||
111 | * @config |
||
112 | * @var array |
||
113 | */ |
||
114 | private static $summary_fields = [ |
||
115 | 'Title' |
||
116 | ]; |
||
117 | |||
118 | /** |
||
119 | * @config |
||
120 | * @var array |
||
121 | */ |
||
122 | private static $db = [ |
||
123 | 'Name' => 'Varchar', |
||
124 | 'Title' => 'Varchar(255)', |
||
125 | 'Default' => 'Varchar(255)', |
||
126 | 'Sort' => 'Int', |
||
127 | 'Required' => 'Boolean', |
||
128 | 'CustomErrorMessage' => 'Varchar(255)', |
||
129 | 'ExtraClass' => 'Text', |
||
130 | 'RightTitle' => 'Varchar(255)', |
||
131 | 'ShowOnLoad' => 'Boolean(1)', |
||
132 | 'ShowInSummary' => 'Boolean', |
||
133 | 'Placeholder' => 'Varchar(255)', |
||
134 | 'DisplayRulesConjunction' => 'Enum("And,Or","Or")', |
||
135 | ]; |
||
136 | |||
137 | private static $table_name = 'EditableFormField'; |
||
138 | |||
139 | |||
140 | private static $defaults = [ |
||
141 | 'ShowOnLoad' => true, |
||
142 | ]; |
||
143 | |||
144 | |||
145 | /** |
||
146 | * @config |
||
147 | * @var array |
||
148 | */ |
||
149 | private static $has_one = [ |
||
150 | 'Parent' => UserDefinedForm::class, |
||
151 | ]; |
||
152 | |||
153 | /** |
||
154 | * Built in extensions required |
||
155 | * |
||
156 | * @config |
||
157 | * @var array |
||
158 | */ |
||
159 | private static $extensions = [ |
||
160 | Versioned::class . "('Stage', 'Live')" |
||
161 | ]; |
||
162 | |||
163 | /** |
||
164 | * @config |
||
165 | * @var array |
||
166 | */ |
||
167 | private static $has_many = [ |
||
168 | 'DisplayRules' => EditableCustomRule::class . '.Parent' |
||
169 | ]; |
||
170 | |||
171 | private static $owns = [ |
||
172 | 'DisplayRules', |
||
173 | ]; |
||
174 | |||
175 | private static $cascade_deletes = [ |
||
176 | 'DisplayRules', |
||
177 | ]; |
||
178 | |||
179 | /** |
||
180 | * @var bool |
||
181 | */ |
||
182 | protected $readonly; |
||
183 | |||
184 | /** |
||
185 | * Property holds the JS event which gets fired for this type of element |
||
186 | * |
||
187 | * @var string |
||
188 | */ |
||
189 | protected $jsEventHandler = 'change'; |
||
190 | |||
191 | /** |
||
192 | * Returns the jsEventHandler property for the current object. Bearing in mind it could've been overridden. |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getJsEventHandler() |
||
199 | |||
200 | /** |
||
201 | * Set the visibility of an individual form field |
||
202 | * |
||
203 | * @param bool |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function setReadonly($readonly = true) |
||
211 | |||
212 | /** |
||
213 | * Returns whether this field is readonly |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | private function isReadonly() |
||
221 | |||
222 | /** |
||
223 | * @return FieldList |
||
224 | */ |
||
225 | public function getCMSFields() |
||
325 | |||
326 | /** |
||
327 | * Return fields to display on the 'Display Rules' tab |
||
328 | * |
||
329 | * @return FieldList |
||
330 | */ |
||
331 | protected function getDisplayRuleFields() |
||
402 | |||
403 | public function onBeforeWrite() |
||
422 | |||
423 | /** |
||
424 | * Generate a new non-conflicting Name value |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | protected function generateName() |
||
442 | |||
443 | /** |
||
444 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
445 | * |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function getSetsOwnError() |
||
452 | |||
453 | /** |
||
454 | * Return whether a user can delete this form field |
||
455 | * based on whether they can edit the page |
||
456 | * |
||
457 | * @param Member $member |
||
458 | * @return bool |
||
459 | */ |
||
460 | public function canDelete($member = null) |
||
464 | |||
465 | /** |
||
466 | * Return whether a user can edit this form field |
||
467 | * based on whether they can edit the page |
||
468 | * |
||
469 | * @param Member $member |
||
470 | * @return bool |
||
471 | */ |
||
472 | public function canEdit($member = null) |
||
496 | |||
497 | /** |
||
498 | * Return whether a user can view this form field |
||
499 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
500 | * |
||
501 | * @param Member $member |
||
502 | * @return bool |
||
503 | */ |
||
504 | public function canView($member = null) |
||
513 | |||
514 | /** |
||
515 | * Return whether a user can create an object of this type |
||
516 | * |
||
517 | * @param Member $member |
||
518 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
519 | * @return bool |
||
520 | */ |
||
521 | View Code Duplication | public function canCreate($member = null, $context = []) |
|
532 | |||
533 | /** |
||
534 | * Helper method to check the parent for this object |
||
535 | * |
||
536 | * @param array $args List of arguments passed to canCreate |
||
537 | * @return SiteTree Parent page instance |
||
538 | */ |
||
539 | View Code Duplication | protected function getCanCreateContext($args) |
|
553 | |||
554 | /** |
||
555 | * checks whether record is new, copied from SiteTree |
||
556 | */ |
||
557 | public function isNew() |
||
569 | |||
570 | /** |
||
571 | * @deprecated since version 4.0 |
||
572 | */ |
||
573 | public function getSettings() |
||
578 | |||
579 | /** |
||
580 | * @deprecated since version 4.0 |
||
581 | */ |
||
582 | public function setSettings($settings = array()) |
||
587 | |||
588 | /** |
||
589 | * @deprecated since version 4.0 |
||
590 | */ |
||
591 | public function setSetting($key, $value) |
||
599 | |||
600 | /** |
||
601 | * Set the allowed css classes for the extraClass custom setting |
||
602 | * |
||
603 | * @param array $allowed The permissible CSS classes to add |
||
604 | */ |
||
605 | public function setAllowedCss(array $allowed) |
||
613 | |||
614 | /** |
||
615 | * @deprecated since version 4.0 |
||
616 | */ |
||
617 | public function getSetting($setting) |
||
629 | |||
630 | /** |
||
631 | * Get the path to the icon for this field type, relative to the site root. |
||
632 | * |
||
633 | * @return string |
||
634 | */ |
||
635 | public function getIcon() |
||
640 | |||
641 | /** |
||
642 | * Return whether or not this field has addable options |
||
643 | * such as a dropdown field or radio set |
||
644 | * |
||
645 | * @return bool |
||
646 | */ |
||
647 | public function getHasAddableOptions() |
||
651 | |||
652 | /** |
||
653 | * Return whether or not this field needs to show the extra |
||
654 | * options dropdown list |
||
655 | * |
||
656 | * @return bool |
||
657 | */ |
||
658 | public function showExtraOptions() |
||
662 | |||
663 | /** |
||
664 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
665 | * |
||
666 | * @return string |
||
667 | */ |
||
668 | public function getEscapedTitle() |
||
672 | |||
673 | /** |
||
674 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
675 | * |
||
676 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
677 | * or groups |
||
678 | * |
||
679 | * @return string |
||
680 | */ |
||
681 | public function getFieldNumber() |
||
712 | |||
713 | public function getCMSTitle() |
||
717 | |||
718 | /** |
||
719 | * @deprecated since version 4.0 |
||
720 | */ |
||
721 | public function getFieldName($field = false) |
||
726 | |||
727 | /** |
||
728 | * @deprecated since version 4.0 |
||
729 | */ |
||
730 | public function getSettingName($field) |
||
737 | |||
738 | /** |
||
739 | * Append custom validation fields to the default 'Validation' |
||
740 | * section in the editable options view |
||
741 | * |
||
742 | * @return FieldList |
||
743 | */ |
||
744 | public function getFieldValidationOptions() |
||
756 | |||
757 | /** |
||
758 | * Return a FormField to appear on the front end. Implement on |
||
759 | * your subclass. |
||
760 | * |
||
761 | * @return FormField |
||
762 | */ |
||
763 | public function getFormField() |
||
767 | |||
768 | /** |
||
769 | * Updates a formfield with extensions |
||
770 | * |
||
771 | * @param FormField $field |
||
772 | */ |
||
773 | public function doUpdateFormField($field) |
||
779 | |||
780 | /** |
||
781 | * Updates a formfield with the additional metadata specified by this field |
||
782 | * |
||
783 | * @param FormField $field |
||
784 | */ |
||
785 | protected function updateFormField($field) |
||
825 | |||
826 | /** |
||
827 | * Return the instance of the submission field class |
||
828 | * |
||
829 | * @return SubmittedFormField |
||
830 | */ |
||
831 | public function getSubmittedFormField() |
||
835 | |||
836 | |||
837 | /** |
||
838 | * Show this form field (and its related value) in the reports and in emails. |
||
839 | * |
||
840 | * @return bool |
||
841 | */ |
||
842 | public function showInReports() |
||
846 | |||
847 | /** |
||
848 | * Return the error message for this field. Either uses the custom |
||
849 | * one (if provided) or the default SilverStripe message |
||
850 | * |
||
851 | * @return Varchar |
||
852 | */ |
||
853 | public function getErrorMessage() |
||
863 | |||
864 | /** |
||
865 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
866 | * to the field proper |
||
867 | * |
||
868 | * @param array $data Unserialised data |
||
869 | */ |
||
870 | public function migrateSettings($data) |
||
885 | |||
886 | /** |
||
887 | * Get the formfield to use when editing this inline in gridfield |
||
888 | * |
||
889 | * @param string $column name of column |
||
890 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
891 | * @return FormField |
||
892 | */ |
||
893 | public function getInlineClassnameField($column, $fieldClasses) |
||
897 | |||
898 | /** |
||
899 | * Get the formfield to use when editing the title inline |
||
900 | * |
||
901 | * @param string $column |
||
902 | * @return FormField |
||
903 | */ |
||
904 | public function getInlineTitleField($column) |
||
910 | |||
911 | /** |
||
912 | * Get the JS expression for selecting the holder for this field |
||
913 | * |
||
914 | * @return string |
||
915 | */ |
||
916 | public function getSelectorHolder() |
||
920 | |||
921 | /** |
||
922 | * Returns only the JS identifier of a string, less the $(), which can be inserted elsewhere, for example when you |
||
923 | * want to perform selections on multiple selectors |
||
924 | * @return string |
||
925 | */ |
||
926 | public function getSelectorOnly() |
||
930 | |||
931 | /** |
||
932 | * Gets the JS expression for selecting the value for this field |
||
933 | * |
||
934 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
935 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
936 | * |
||
937 | * @return string |
||
938 | */ |
||
939 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
943 | |||
944 | /** |
||
945 | * @return string |
||
946 | */ |
||
947 | public function getSelectorFieldOnly() |
||
951 | |||
952 | |||
953 | /** |
||
954 | * Get the list of classes that can be selected and used as data-values |
||
955 | * |
||
956 | * @param $includeLiterals Set to false to exclude non-data fields |
||
957 | * @return array |
||
958 | */ |
||
959 | public function getEditableFieldClasses($includeLiterals = true) |
||
988 | |||
989 | /** |
||
990 | * @return EditableFormField\Validator |
||
991 | */ |
||
992 | public function getCMSValidator() |
||
997 | |||
998 | /** |
||
999 | * Determine effective display rules for this field. |
||
1000 | * |
||
1001 | * @return SS_List |
||
1002 | */ |
||
1003 | public function EffectiveDisplayRules() |
||
1010 | |||
1011 | /** |
||
1012 | * Extracts info from DisplayRules into array so UserDefinedForm->buildWatchJS can run through it. |
||
1013 | * @return array|null |
||
1014 | */ |
||
1015 | public function formatDisplayRules() |
||
1058 | |||
1059 | /** |
||
1060 | * Replaces the set DisplayRulesConjunction with their JS logical operators |
||
1061 | * @return string |
||
1062 | */ |
||
1063 | public function DisplayRulesConjunctionNice() |
||
1067 | |||
1068 | /** |
||
1069 | * Replaces boolean ShowOnLoad with its JS string equivalent |
||
1070 | * @return string |
||
1071 | */ |
||
1072 | public function ShowOnLoadNice() |
||
1076 | |||
1077 | /** |
||
1078 | * Returns whether this is of type EditableCheckBoxField |
||
1079 | * @return bool |
||
1080 | */ |
||
1081 | public function isCheckBoxField() |
||
1085 | |||
1086 | /** |
||
1087 | * Returns whether this is of type EditableRadioField |
||
1088 | * @return bool |
||
1089 | */ |
||
1090 | public function isRadioField() |
||
1094 | |||
1095 | /** |
||
1096 | * Determined is this is of type EditableCheckboxGroupField |
||
1097 | * @return bool |
||
1098 | */ |
||
1099 | public function isCheckBoxGroupField() |
||
1103 | } |
||
1104 |
This check marks private properties in classes that are never used. Those properties can be removed.