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 |
||
23 | class EditableFormField extends DataObject |
||
|
|||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Set to true to hide from class selector |
||
28 | * |
||
29 | * @config |
||
30 | * @var bool |
||
31 | */ |
||
32 | private static $hidden = false; |
||
33 | |||
34 | /** |
||
35 | * Define this field as abstract (not inherited) |
||
36 | * |
||
37 | * @config |
||
38 | * @var bool |
||
39 | */ |
||
40 | private static $abstract = true; |
||
41 | |||
42 | /** |
||
43 | * Flag this field type as non-data (e.g. literal, header, html) |
||
44 | * |
||
45 | * @config |
||
46 | * @var bool |
||
47 | */ |
||
48 | private static $literal = false; |
||
49 | |||
50 | /** |
||
51 | * Default sort order |
||
52 | * |
||
53 | * @config |
||
54 | * @var string |
||
55 | */ |
||
56 | private static $default_sort = '"Sort"'; |
||
57 | |||
58 | /** |
||
59 | * A list of CSS classes that can be added |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | public static $allowed_css = array(); |
||
64 | |||
65 | /** |
||
66 | * Set this to true to enable placeholder field for any given class |
||
67 | * @config |
||
68 | * @var bool |
||
69 | */ |
||
70 | private static $has_placeholder = false; |
||
71 | |||
72 | /** |
||
73 | * @config |
||
74 | * @var array |
||
75 | */ |
||
76 | private static $summary_fields = array( |
||
77 | 'Title' |
||
78 | ); |
||
79 | |||
80 | /** |
||
81 | * @config |
||
82 | * @var array |
||
83 | */ |
||
84 | private static $db = array( |
||
85 | "Name" => "Varchar", |
||
86 | "Title" => "Varchar(255)", |
||
87 | "Default" => "Varchar(255)", |
||
88 | "Sort" => "Int", |
||
89 | "Required" => "Boolean", |
||
90 | "CustomErrorMessage" => "Varchar(255)", |
||
91 | |||
92 | "CustomRules" => "Text", // @deprecated from 2.0 |
||
93 | "CustomSettings" => "Text", // @deprecated from 2.0 |
||
94 | "Migrated" => "Boolean", // set to true when migrated |
||
95 | |||
96 | "ExtraClass" => "Text", // from CustomSettings |
||
97 | "RightTitle" => "Varchar(255)", // from CustomSettings |
||
98 | "ShowOnLoad" => "Boolean(1)", // from CustomSettings |
||
99 | "ShowInSummary" => "Boolean", |
||
100 | "Placeholder" => "Varchar(255)", |
||
101 | 'DisplayRulesConjunction' => 'Enum("And,Or","Or")', |
||
102 | ); |
||
103 | |||
104 | |||
105 | private static $defaults = array( |
||
106 | 'ShowOnLoad' => true, |
||
107 | ); |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @config |
||
112 | * @var array |
||
113 | */ |
||
114 | private static $has_one = array( |
||
115 | "Parent" => "UserDefinedForm", |
||
116 | ); |
||
117 | |||
118 | /** |
||
119 | * Built in extensions required |
||
120 | * |
||
121 | * @config |
||
122 | * @var array |
||
123 | */ |
||
124 | private static $extensions = array( |
||
125 | "Versioned('Stage', 'Live')" |
||
126 | ); |
||
127 | |||
128 | /** |
||
129 | * @config |
||
130 | * @var array |
||
131 | */ |
||
132 | private static $has_many = array( |
||
133 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
134 | ); |
||
135 | |||
136 | /** |
||
137 | * @var bool |
||
138 | */ |
||
139 | protected $readonly; |
||
140 | |||
141 | /** |
||
142 | * Property holds the JS event which gets fired for this type of element |
||
143 | * |
||
144 | * @var string |
||
145 | */ |
||
146 | protected $jsEventHandler = 'change'; |
||
147 | |||
148 | /** |
||
149 | * Returns the jsEventHandler property for the current object. Bearing in mind it could've been overridden. |
||
150 | * @return string |
||
151 | */ |
||
152 | 10 | public function getJsEventHandler() |
|
156 | |||
157 | /** |
||
158 | * Set the visibility of an individual form field |
||
159 | * |
||
160 | * @param bool |
||
161 | */ |
||
162 | 1 | public function setReadonly($readonly = true) |
|
166 | |||
167 | /** |
||
168 | * Returns whether this field is readonly |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | 1 | private function isReadonly() |
|
176 | |||
177 | /** |
||
178 | * @return FieldList |
||
179 | */ |
||
180 | 1 | public function getCMSFields() |
|
278 | |||
279 | /** |
||
280 | * Return fields to display on the 'Display Rules' tab |
||
281 | * |
||
282 | * @return FieldList |
||
283 | */ |
||
284 | protected function getDisplayRuleFields() |
||
350 | |||
351 | public function onBeforeWrite() |
||
370 | 41 | ||
371 | 41 | /** |
|
372 | 45 | * Generate a new non-conflicting Name value |
|
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | protected function generateName() |
||
389 | 28 | ||
390 | 28 | /** |
|
391 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
392 | * |
||
393 | * @return bool |
||
394 | */ |
||
395 | public function getSetsOwnError() |
||
399 | |||
400 | /** |
||
401 | * Return whether a user can delete this form field |
||
402 | * based on whether they can edit the page |
||
403 | * |
||
404 | * @param Member $member |
||
405 | * @return bool |
||
406 | */ |
||
407 | public function canDelete($member = null) |
||
411 | |||
412 | 1 | /** |
|
413 | * Return whether a user can edit this form field |
||
414 | * based on whether they can edit the page |
||
415 | * |
||
416 | * @param Member $member |
||
417 | * @return bool |
||
418 | */ |
||
419 | public function canEdit($member = null) |
||
443 | |||
444 | /** |
||
445 | * Return whether a user can view this form field |
||
446 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
447 | * |
||
448 | * @param Member $member |
||
449 | * @return bool |
||
450 | */ |
||
451 | public function canView($member = null) |
||
460 | |||
461 | /** |
||
462 | * Return whether a user can create an object of this type |
||
463 | * |
||
464 | * @param Member $member |
||
465 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
466 | * @return bool |
||
467 | */ |
||
468 | View Code Duplication | public function canCreate($member = null) |
|
479 | |||
480 | 3 | /** |
|
481 | * Helper method to check the parent for this object |
||
482 | * |
||
483 | * @param array $args List of arguments passed to canCreate |
||
484 | * @return SiteTree Parent page instance |
||
485 | */ |
||
486 | View Code Duplication | protected function getCanCreateContext($args) |
|
500 | |||
501 | 3 | /** |
|
502 | * Check if can publish |
||
503 | * |
||
504 | * @param Member $member |
||
505 | * @return bool |
||
506 | */ |
||
507 | public function canPublish($member = null) |
||
511 | |||
512 | /** |
||
513 | * Check if can unpublish |
||
514 | * |
||
515 | * @param Member $member |
||
516 | * @return bool |
||
517 | */ |
||
518 | public function canUnpublish($member = null) |
||
522 | |||
523 | /** |
||
524 | * Publish this Form Field to the live site |
||
525 | * |
||
526 | * Wrapper for the {@link Versioned} publish function |
||
527 | * |
||
528 | * @param string $fromStage |
||
529 | * @param string $toStage |
||
530 | * @param bool $createNewVersion |
||
531 | */ |
||
532 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
||
537 | 10 | ||
538 | 10 | /** |
|
539 | 10 | * Publish all field rules |
|
540 | * |
||
541 | * @param string $fromStage |
||
542 | * @param string $toStage |
||
543 | * @param bool $createNewVersion |
||
544 | */ |
||
545 | View Code Duplication | protected function publishRules($fromStage, $toStage, $createNewVersion) |
|
568 | 1 | ||
569 | 10 | /** |
|
570 | 10 | * Delete this field from a given stage |
|
571 | * |
||
572 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
573 | */ |
||
574 | public function doDeleteFromStage($stage) |
||
586 | |||
587 | 1 | /** |
|
588 | 1 | * checks whether record is new, copied from SiteTree |
|
589 | */ |
||
590 | public function isNew() |
||
602 | |||
603 | /** |
||
604 | * checks if records is changed on stage |
||
605 | * @return boolean |
||
606 | */ |
||
607 | public function getIsModifiedOnStage() |
||
619 | |||
620 | /** |
||
621 | * @deprecated since version 4.0 |
||
622 | */ |
||
623 | public function getSettings() |
||
628 | |||
629 | /** |
||
630 | * @deprecated since version 4.0 |
||
631 | */ |
||
632 | public function setSettings($settings = array()) |
||
637 | |||
638 | /** |
||
639 | * @deprecated since version 4.0 |
||
640 | */ |
||
641 | public function setSetting($key, $value) |
||
649 | |||
650 | /** |
||
651 | * Set the allowed css classes for the extraClass custom setting |
||
652 | * |
||
653 | * @param array $allowed The permissible CSS classes to add |
||
654 | */ |
||
655 | public function setAllowedCss(array $allowed) |
||
663 | |||
664 | /** |
||
665 | * @deprecated since version 4.0 |
||
666 | */ |
||
667 | public function getSetting($setting) |
||
679 | |||
680 | /** |
||
681 | * Get the path to the icon for this field type, relative to the site root. |
||
682 | * |
||
683 | * @return string |
||
684 | */ |
||
685 | public function getIcon() |
||
689 | |||
690 | /** |
||
691 | * Return whether or not this field has addable options |
||
692 | * such as a dropdown field or radio set |
||
693 | * |
||
694 | * @return bool |
||
695 | */ |
||
696 | public function getHasAddableOptions() |
||
700 | |||
701 | /** |
||
702 | * Return whether or not this field needs to show the extra |
||
703 | * options dropdown list |
||
704 | * |
||
705 | * @return bool |
||
706 | */ |
||
707 | public function showExtraOptions() |
||
711 | |||
712 | /** |
||
713 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
714 | * |
||
715 | * @return string |
||
716 | */ |
||
717 | public function getEscapedTitle() |
||
721 | |||
722 | 18 | /** |
|
723 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
724 | * |
||
725 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
726 | * or groups |
||
727 | * |
||
728 | * @return string |
||
729 | */ |
||
730 | public function getFieldNumber() |
||
761 | |||
762 | public function getCMSTitle() |
||
766 | |||
767 | /** |
||
768 | * @deprecated since version 4.0 |
||
769 | */ |
||
770 | public function getFieldName($field = false) |
||
775 | |||
776 | /** |
||
777 | * @deprecated since version 4.0 |
||
778 | */ |
||
779 | public function getSettingName($field) |
||
786 | |||
787 | /** |
||
788 | * Append custom validation fields to the default 'Validation' |
||
789 | * section in the editable options view |
||
790 | * |
||
791 | * @return FieldList |
||
792 | */ |
||
793 | public function getFieldValidationOptions() |
||
805 | |||
806 | /** |
||
807 | * Return a FormField to appear on the front end. Implement on |
||
808 | * your subclass. |
||
809 | * |
||
810 | * @return FormField |
||
811 | */ |
||
812 | public function getFormField() |
||
816 | |||
817 | /** |
||
818 | * Updates a formfield with extensions |
||
819 | * |
||
820 | * @param FormField $field |
||
821 | */ |
||
822 | public function doUpdateFormField($field) |
||
828 | 17 | ||
829 | 17 | /** |
|
830 | 17 | * Updates a formfield with the additional metadata specified by this field |
|
831 | * |
||
832 | * @param FormField $field |
||
833 | */ |
||
834 | protected function updateFormField($field) |
||
874 | |||
875 | /** |
||
876 | * Return the instance of the submission field class |
||
877 | * |
||
878 | 2 | * @return SubmittedFormField |
|
879 | */ |
||
880 | 2 | public function getSubmittedFormField() |
|
884 | |||
885 | |||
886 | /** |
||
887 | * Show this form field (and its related value) in the reports and in emails. |
||
888 | * |
||
889 | 2 | * @return bool |
|
890 | */ |
||
891 | 2 | public function showInReports() |
|
895 | |||
896 | /** |
||
897 | * Return the error message for this field. Either uses the custom |
||
898 | * one (if provided) or the default SilverStripe message |
||
899 | * |
||
900 | 17 | * @return Varchar |
|
901 | */ |
||
902 | 17 | public function getErrorMessage() |
|
912 | |||
913 | /** |
||
914 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
915 | * to the field proper |
||
916 | * |
||
917 | 2 | * @param array $data Unserialised data |
|
918 | */ |
||
919 | public function migrateSettings($data) |
||
934 | |||
935 | /** |
||
936 | * Get the formfield to use when editing this inline in gridfield |
||
937 | * |
||
938 | * @param string $column name of column |
||
939 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
940 | * @return FormField |
||
941 | */ |
||
942 | public function getInlineClassnameField($column, $fieldClasses) |
||
946 | |||
947 | /** |
||
948 | * Get the formfield to use when editing the title inline |
||
949 | * |
||
950 | * @param string $column |
||
951 | * @return FormField |
||
952 | */ |
||
953 | public function getInlineTitleField($column) |
||
959 | |||
960 | /** |
||
961 | * Get the JS expression for selecting the holder for this field |
||
962 | * |
||
963 | * @return string |
||
964 | */ |
||
965 | public function getSelectorHolder() |
||
969 | |||
970 | /** |
||
971 | * Returns only the JS identifier of a string, less the $(), which can be inserted elsewhere, for example when you |
||
972 | * want to perform selections on multiple selectors |
||
973 | 9 | * @return string |
|
974 | */ |
||
975 | 9 | public function getSelectorOnly() |
|
979 | |||
980 | /** |
||
981 | * Gets the JS expression for selecting the value for this field |
||
982 | * |
||
983 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
984 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
985 | * |
||
986 | * @return string |
||
987 | */ |
||
988 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
992 | |||
993 | /** |
||
994 | 2 | * @return string |
|
995 | */ |
||
996 | 2 | public function getSelectorFieldOnly() |
|
1000 | |||
1001 | |||
1002 | /** |
||
1003 | * Get the list of classes that can be selected and used as data-values |
||
1004 | * |
||
1005 | * @param $includeLiterals Set to false to exclude non-data fields |
||
1006 | 2 | * @return array |
|
1007 | */ |
||
1008 | 2 | public function getEditableFieldClasses($includeLiterals = true) |
|
1036 | |||
1037 | /** |
||
1038 | * @return EditableFormFieldValidator |
||
1039 | */ |
||
1040 | public function getCMSValidator() |
||
1045 | |||
1046 | /** |
||
1047 | * Determine effective display rules for this field. |
||
1048 | * |
||
1049 | 11 | * @return SS_List |
|
1050 | */ |
||
1051 | 11 | public function EffectiveDisplayRules() |
|
1058 | |||
1059 | /** |
||
1060 | * Extracts info from DisplayRules into array so UserDefinedForm->buildWatchJS can run through it. |
||
1061 | 10 | * @return array|null |
|
1062 | 1 | */ |
|
1063 | 9 | public function formatDisplayRules() |
|
1105 | |||
1106 | /** |
||
1107 | 9 | * Replaces the set DisplayRulesConjunction with their JS logical operators |
|
1108 | * @return string |
||
1109 | 9 | */ |
|
1110 | public function DisplayRulesConjunctionNice() |
||
1114 | |||
1115 | /** |
||
1116 | 9 | * Replaces boolean ShowOnLoad with its JS string equivalent |
|
1117 | * @return string |
||
1118 | 9 | */ |
|
1119 | public function ShowOnLoadNice() |
||
1123 | |||
1124 | /** |
||
1125 | 2 | * Returns whether this is of type EditableCheckBoxField |
|
1126 | * @return bool |
||
1127 | 2 | */ |
|
1128 | public function isCheckBoxField() |
||
1132 | |||
1133 | /** |
||
1134 | 2 | * Returns whether this is of type EditableRadioField |
|
1135 | * @return bool |
||
1136 | 2 | */ |
|
1137 | public function isRadioField() |
||
1141 | |||
1142 | /** |
||
1143 | * Determined is this is of type EditableCheckboxGroupField |
||
1144 | * @return bool |
||
1145 | */ |
||
1146 | public function isCheckBoxGroupField() |
||
1150 | } |
||
1151 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.