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 | |||
130 | 'CustomRules' => 'Text', // @deprecated from 2.0 |
||
131 | 'CustomSettings' => 'Text', // @deprecated from 2.0 |
||
132 | 'Migrated' => 'Boolean', // set to true when migrated |
||
133 | |||
134 | 'ExtraClass' => 'Text', // from CustomSettings |
||
135 | 'RightTitle' => 'Varchar(255)', // from CustomSettings |
||
136 | 'ShowOnLoad' => 'Boolean(1)', // from CustomSettings |
||
137 | 'ShowInSummary' => 'Boolean', |
||
138 | 'Placeholder' => 'Varchar(255)', |
||
139 | 'DisplayRulesConjunction' => 'Enum("And,Or","Or")', |
||
140 | ]; |
||
141 | |||
142 | private static $table_name = 'EditableFormField'; |
||
143 | |||
144 | |||
145 | private static $defaults = [ |
||
146 | 'ShowOnLoad' => true, |
||
147 | ]; |
||
148 | |||
149 | |||
150 | /** |
||
151 | * @config |
||
152 | * @var array |
||
153 | */ |
||
154 | private static $has_one = [ |
||
155 | 'Parent' => UserDefinedForm::class, |
||
156 | ]; |
||
157 | |||
158 | /** |
||
159 | * Built in extensions required |
||
160 | * |
||
161 | * @config |
||
162 | * @var array |
||
163 | */ |
||
164 | private static $extensions = [ |
||
165 | Versioned::class . "('Stage', 'Live')" |
||
166 | ]; |
||
167 | |||
168 | /** |
||
169 | * @config |
||
170 | * @var array |
||
171 | */ |
||
172 | private static $has_many = [ |
||
173 | 'DisplayRules' => EditableCustomRule::class . '.Parent' |
||
174 | ]; |
||
175 | |||
176 | private static $owns = [ |
||
177 | 'DisplayRules', |
||
178 | ]; |
||
179 | |||
180 | private static $cascade_deletes = [ |
||
181 | 'DisplayRules', |
||
182 | ]; |
||
183 | |||
184 | /** |
||
185 | * @var bool |
||
186 | */ |
||
187 | protected $readonly; |
||
188 | |||
189 | /** |
||
190 | * Property holds the JS event which gets fired for this type of element |
||
191 | * |
||
192 | * @var string |
||
193 | */ |
||
194 | protected $jsEventHandler = 'change'; |
||
195 | |||
196 | /** |
||
197 | * Returns the jsEventHandler property for the current object. Bearing in mind it could've been overridden. |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getJsEventHandler() |
||
204 | |||
205 | /** |
||
206 | * Set the visibility of an individual form field |
||
207 | * |
||
208 | * @param bool |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function setReadonly($readonly = true) |
||
216 | |||
217 | /** |
||
218 | * Returns whether this field is readonly |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | private function isReadonly() |
||
226 | |||
227 | /** |
||
228 | * @return FieldList |
||
229 | */ |
||
230 | public function getCMSFields() |
||
330 | |||
331 | /** |
||
332 | * Return fields to display on the 'Display Rules' tab |
||
333 | * |
||
334 | * @return FieldList |
||
335 | */ |
||
336 | protected function getDisplayRuleFields() |
||
407 | |||
408 | public function onBeforeWrite() |
||
427 | |||
428 | /** |
||
429 | * Generate a new non-conflicting Name value |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | protected function generateName() |
||
447 | |||
448 | /** |
||
449 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
450 | * |
||
451 | * @return bool |
||
452 | */ |
||
453 | public function getSetsOwnError() |
||
457 | |||
458 | /** |
||
459 | * Return whether a user can delete this form field |
||
460 | * based on whether they can edit the page |
||
461 | * |
||
462 | * @param Member $member |
||
463 | * @return bool |
||
464 | */ |
||
465 | public function canDelete($member = null) |
||
469 | |||
470 | /** |
||
471 | * Return whether a user can edit this form field |
||
472 | * based on whether they can edit the page |
||
473 | * |
||
474 | * @param Member $member |
||
475 | * @return bool |
||
476 | */ |
||
477 | public function canEdit($member = null) |
||
501 | |||
502 | /** |
||
503 | * Return whether a user can view this form field |
||
504 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
505 | * |
||
506 | * @param Member $member |
||
507 | * @return bool |
||
508 | */ |
||
509 | public function canView($member = null) |
||
518 | |||
519 | /** |
||
520 | * Return whether a user can create an object of this type |
||
521 | * |
||
522 | * @param Member $member |
||
523 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
524 | * @return bool |
||
525 | */ |
||
526 | View Code Duplication | public function canCreate($member = null, $context = []) |
|
537 | |||
538 | /** |
||
539 | * Helper method to check the parent for this object |
||
540 | * |
||
541 | * @param array $args List of arguments passed to canCreate |
||
542 | * @return SiteTree Parent page instance |
||
543 | */ |
||
544 | View Code Duplication | protected function getCanCreateContext($args) |
|
558 | |||
559 | /** |
||
560 | * checks whether record is new, copied from SiteTree |
||
561 | */ |
||
562 | public function isNew() |
||
574 | |||
575 | /** |
||
576 | * @deprecated since version 4.0 |
||
577 | */ |
||
578 | public function getSettings() |
||
583 | |||
584 | /** |
||
585 | * @deprecated since version 4.0 |
||
586 | */ |
||
587 | public function setSettings($settings = array()) |
||
592 | |||
593 | /** |
||
594 | * @deprecated since version 4.0 |
||
595 | */ |
||
596 | public function setSetting($key, $value) |
||
604 | |||
605 | /** |
||
606 | * Set the allowed css classes for the extraClass custom setting |
||
607 | * |
||
608 | * @param array $allowed The permissible CSS classes to add |
||
609 | */ |
||
610 | public function setAllowedCss(array $allowed) |
||
618 | |||
619 | /** |
||
620 | * @deprecated since version 4.0 |
||
621 | */ |
||
622 | public function getSetting($setting) |
||
634 | |||
635 | /** |
||
636 | * Get the path to the icon for this field type, relative to the site root. |
||
637 | * |
||
638 | * @return string |
||
639 | */ |
||
640 | public function getIcon() |
||
645 | |||
646 | /** |
||
647 | * Return whether or not this field has addable options |
||
648 | * such as a dropdown field or radio set |
||
649 | * |
||
650 | * @return bool |
||
651 | */ |
||
652 | public function getHasAddableOptions() |
||
656 | |||
657 | /** |
||
658 | * Return whether or not this field needs to show the extra |
||
659 | * options dropdown list |
||
660 | * |
||
661 | * @return bool |
||
662 | */ |
||
663 | public function showExtraOptions() |
||
667 | |||
668 | /** |
||
669 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
670 | * |
||
671 | * @return string |
||
672 | */ |
||
673 | public function getEscapedTitle() |
||
677 | |||
678 | /** |
||
679 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
680 | * |
||
681 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
682 | * or groups |
||
683 | * |
||
684 | * @return string |
||
685 | */ |
||
686 | public function getFieldNumber() |
||
717 | |||
718 | public function getCMSTitle() |
||
722 | |||
723 | /** |
||
724 | * @deprecated since version 4.0 |
||
725 | */ |
||
726 | public function getFieldName($field = false) |
||
731 | |||
732 | /** |
||
733 | * @deprecated since version 4.0 |
||
734 | */ |
||
735 | public function getSettingName($field) |
||
742 | |||
743 | /** |
||
744 | * Append custom validation fields to the default 'Validation' |
||
745 | * section in the editable options view |
||
746 | * |
||
747 | * @return FieldList |
||
748 | */ |
||
749 | public function getFieldValidationOptions() |
||
761 | |||
762 | /** |
||
763 | * Return a FormField to appear on the front end. Implement on |
||
764 | * your subclass. |
||
765 | * |
||
766 | * @return FormField |
||
767 | */ |
||
768 | public function getFormField() |
||
772 | |||
773 | /** |
||
774 | * Updates a formfield with extensions |
||
775 | * |
||
776 | * @param FormField $field |
||
777 | */ |
||
778 | public function doUpdateFormField($field) |
||
784 | |||
785 | /** |
||
786 | * Updates a formfield with the additional metadata specified by this field |
||
787 | * |
||
788 | * @param FormField $field |
||
789 | */ |
||
790 | protected function updateFormField($field) |
||
830 | |||
831 | /** |
||
832 | * Return the instance of the submission field class |
||
833 | * |
||
834 | * @return SubmittedFormField |
||
835 | */ |
||
836 | public function getSubmittedFormField() |
||
840 | |||
841 | |||
842 | /** |
||
843 | * Show this form field (and its related value) in the reports and in emails. |
||
844 | * |
||
845 | * @return bool |
||
846 | */ |
||
847 | public function showInReports() |
||
851 | |||
852 | /** |
||
853 | * Return the error message for this field. Either uses the custom |
||
854 | * one (if provided) or the default SilverStripe message |
||
855 | * |
||
856 | * @return Varchar |
||
857 | */ |
||
858 | public function getErrorMessage() |
||
868 | |||
869 | /** |
||
870 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
871 | * to the field proper |
||
872 | * |
||
873 | * @param array $data Unserialised data |
||
874 | */ |
||
875 | public function migrateSettings($data) |
||
890 | |||
891 | /** |
||
892 | * Get the formfield to use when editing this inline in gridfield |
||
893 | * |
||
894 | * @param string $column name of column |
||
895 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
896 | * @return FormField |
||
897 | */ |
||
898 | public function getInlineClassnameField($column, $fieldClasses) |
||
902 | |||
903 | /** |
||
904 | * Get the formfield to use when editing the title inline |
||
905 | * |
||
906 | * @param string $column |
||
907 | * @return FormField |
||
908 | */ |
||
909 | public function getInlineTitleField($column) |
||
915 | |||
916 | /** |
||
917 | * Get the JS expression for selecting the holder for this field |
||
918 | * |
||
919 | * @return string |
||
920 | */ |
||
921 | public function getSelectorHolder() |
||
925 | |||
926 | /** |
||
927 | * Returns only the JS identifier of a string, less the $(), which can be inserted elsewhere, for example when you |
||
928 | * want to perform selections on multiple selectors |
||
929 | * @return string |
||
930 | */ |
||
931 | public function getSelectorOnly() |
||
935 | |||
936 | /** |
||
937 | * Gets the JS expression for selecting the value for this field |
||
938 | * |
||
939 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
940 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
941 | * |
||
942 | * @return string |
||
943 | */ |
||
944 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
948 | |||
949 | /** |
||
950 | * @return string |
||
951 | */ |
||
952 | public function getSelectorFieldOnly() |
||
956 | |||
957 | |||
958 | /** |
||
959 | * Get the list of classes that can be selected and used as data-values |
||
960 | * |
||
961 | * @param $includeLiterals Set to false to exclude non-data fields |
||
962 | * @return array |
||
963 | */ |
||
964 | public function getEditableFieldClasses($includeLiterals = true) |
||
993 | |||
994 | /** |
||
995 | * @return EditableFormFieldValidator |
||
996 | */ |
||
997 | public function getCMSValidator() |
||
1002 | |||
1003 | /** |
||
1004 | * Determine effective display rules for this field. |
||
1005 | * |
||
1006 | * @return SS_List |
||
1007 | */ |
||
1008 | public function EffectiveDisplayRules() |
||
1015 | |||
1016 | /** |
||
1017 | * Extracts info from DisplayRules into array so UserDefinedForm->buildWatchJS can run through it. |
||
1018 | * @return array|null |
||
1019 | */ |
||
1020 | public function formatDisplayRules() |
||
1063 | |||
1064 | /** |
||
1065 | * Replaces the set DisplayRulesConjunction with their JS logical operators |
||
1066 | * @return string |
||
1067 | */ |
||
1068 | public function DisplayRulesConjunctionNice() |
||
1072 | |||
1073 | /** |
||
1074 | * Replaces boolean ShowOnLoad with its JS string equivalent |
||
1075 | * @return string |
||
1076 | */ |
||
1077 | public function ShowOnLoadNice() |
||
1081 | |||
1082 | /** |
||
1083 | * Returns whether this is of type EditableCheckBoxField |
||
1084 | * @return bool |
||
1085 | */ |
||
1086 | public function isCheckBoxField() |
||
1090 | |||
1091 | /** |
||
1092 | * Returns whether this is of type EditableRadioField |
||
1093 | * @return bool |
||
1094 | */ |
||
1095 | public function isRadioField() |
||
1099 | |||
1100 | /** |
||
1101 | * Determined is this is of type EditableCheckboxGroupField |
||
1102 | * @return bool |
||
1103 | */ |
||
1104 | public function isCheckBoxGroupField() |
||
1108 | } |
||
1109 |
This check marks private properties in classes that are never used. Those properties can be removed.