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 | 1 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
|
134 | ); |
||
135 | 1 | ||
136 | 1 | /** |
|
137 | * @var bool |
||
138 | */ |
||
139 | protected $readonly; |
||
140 | |||
141 | /** |
||
142 | * Property holds the JS event which gets fired for this type of element |
||
143 | 1 | * |
|
144 | * @var string |
||
145 | 1 | */ |
|
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 | 10 | */ |
|
152 | public function getJsEventHandler() |
||
156 | |||
157 | 1 | /** |
|
158 | * Set the visibility of an individual form field |
||
159 | * |
||
160 | 10 | * @param bool |
|
161 | */ |
||
162 | public function setReadonly($readonly = true) |
||
166 | |||
167 | /** |
||
168 | * Returns whether this field is readonly |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | private function isReadonly() |
||
176 | |||
177 | /** |
||
178 | * @return FieldList |
||
179 | */ |
||
180 | public function getCMSFields() |
||
278 | |||
279 | /** |
||
280 | * Return fields to display on the 'Display Rules' tab |
||
281 | * |
||
282 | * @return FieldList |
||
283 | */ |
||
284 | protected function getDisplayRuleFields() |
||
353 | 25 | ||
354 | public function onBeforeWrite() |
||
373 | 1 | ||
374 | /** |
||
375 | 1 | * Generate a new non-conflicting Name value |
|
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | protected function generateName() |
||
392 | |||
393 | /** |
||
394 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
395 | * |
||
396 | * @return bool |
||
397 | */ |
||
398 | public function getSetsOwnError() |
||
402 | |||
403 | /** |
||
404 | * Return whether a user can delete this form field |
||
405 | * based on whether they can edit the page |
||
406 | * |
||
407 | * @param Member $member |
||
408 | * @return bool |
||
409 | */ |
||
410 | public function canDelete($member = null) |
||
414 | |||
415 | /** |
||
416 | * Return whether a user can edit this form field |
||
417 | 1 | * based on whether they can edit the page |
|
418 | * |
||
419 | 1 | * @param Member $member |
|
420 | 1 | * @return bool |
|
421 | 1 | */ |
|
422 | public function canEdit($member = null) |
||
446 | |||
447 | /** |
||
448 | * Return whether a user can view this form field |
||
449 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
450 | * |
||
451 | * @param Member $member |
||
452 | 3 | * @return bool |
|
453 | */ |
||
454 | public function canView($member = null) |
||
463 | |||
464 | 3 | /** |
|
465 | * Return whether a user can create an object of this type |
||
466 | * |
||
467 | * @param Member $member |
||
468 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
469 | * @return bool |
||
470 | */ |
||
471 | View Code Duplication | public function canCreate($member = null) |
|
482 | |||
483 | /** |
||
484 | * Helper method to check the parent for this object |
||
485 | * |
||
486 | * @param array $args List of arguments passed to canCreate |
||
487 | * @return SiteTree Parent page instance |
||
488 | */ |
||
489 | View Code Duplication | protected function getCanCreateContext($args) |
|
503 | 1 | ||
504 | 1 | /** |
|
505 | 9 | * Check if can publish |
|
506 | * |
||
507 | * @param Member $member |
||
508 | 9 | * @return bool |
|
509 | 9 | */ |
|
510 | public function canPublish($member = null) |
||
514 | |||
515 | 9 | /** |
|
516 | 1 | * Check if can unpublish |
|
517 | 9 | * |
|
518 | 9 | * @param Member $member |
|
519 | * @return bool |
||
520 | */ |
||
521 | public function canUnpublish($member = null) |
||
525 | 1 | ||
526 | /** |
||
527 | * Publish this Form Field to the live site |
||
528 | 1 | * |
|
529 | 1 | * Wrapper for the {@link Versioned} publish function |
|
530 | 1 | * |
|
531 | * @param string $fromStage |
||
532 | 1 | * @param string $toStage |
|
533 | * @param bool $createNewVersion |
||
534 | */ |
||
535 | 1 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
|
540 | |||
541 | /** |
||
542 | * Publish all field rules |
||
543 | * |
||
544 | * @param string $fromStage |
||
545 | * @param string $toStage |
||
546 | * @param bool $createNewVersion |
||
547 | */ |
||
548 | View Code Duplication | protected function publishRules($fromStage, $toStage, $createNewVersion) |
|
571 | |||
572 | /** |
||
573 | * Delete this field from a given stage |
||
574 | * |
||
575 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
576 | */ |
||
577 | public function doDeleteFromStage($stage) |
||
589 | |||
590 | /** |
||
591 | * checks whether record is new, copied from SiteTree |
||
592 | */ |
||
593 | public function isNew() |
||
605 | |||
606 | /** |
||
607 | * checks if records is changed on stage |
||
608 | * @return boolean |
||
609 | */ |
||
610 | public function getIsModifiedOnStage() |
||
622 | |||
623 | /** |
||
624 | * @deprecated since version 4.0 |
||
625 | */ |
||
626 | public function getSettings() |
||
631 | |||
632 | /** |
||
633 | * @deprecated since version 4.0 |
||
634 | */ |
||
635 | public function setSettings($settings = array()) |
||
640 | |||
641 | /** |
||
642 | * @deprecated since version 4.0 |
||
643 | */ |
||
644 | public function setSetting($key, $value) |
||
652 | |||
653 | /** |
||
654 | * Set the allowed css classes for the extraClass custom setting |
||
655 | * |
||
656 | * @param array $allowed The permissible CSS classes to add |
||
657 | */ |
||
658 | public function setAllowedCss(array $allowed) |
||
666 | |||
667 | /** |
||
668 | 18 | * @deprecated since version 4.0 |
|
669 | */ |
||
670 | 18 | public function getSetting($setting) |
|
682 | |||
683 | /** |
||
684 | * Get the path to the icon for this field type, relative to the site root. |
||
685 | * |
||
686 | * @return string |
||
687 | */ |
||
688 | public function getIcon() |
||
692 | |||
693 | /** |
||
694 | * Return whether or not this field has addable options |
||
695 | * such as a dropdown field or radio set |
||
696 | * |
||
697 | * @return bool |
||
698 | */ |
||
699 | public function getHasAddableOptions() |
||
703 | |||
704 | /** |
||
705 | * Return whether or not this field needs to show the extra |
||
706 | * options dropdown list |
||
707 | * |
||
708 | * @return bool |
||
709 | */ |
||
710 | public function showExtraOptions() |
||
714 | |||
715 | /** |
||
716 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
717 | * |
||
718 | * @return string |
||
719 | */ |
||
720 | public function getEscapedTitle() |
||
724 | |||
725 | /** |
||
726 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
727 | * |
||
728 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
729 | * or groups |
||
730 | * |
||
731 | * @return string |
||
732 | */ |
||
733 | public function getFieldNumber() |
||
764 | |||
765 | public function getCMSTitle() |
||
769 | |||
770 | /** |
||
771 | * @deprecated since version 4.0 |
||
772 | */ |
||
773 | 17 | public function getFieldName($field = false) |
|
778 | 17 | ||
779 | /** |
||
780 | * @deprecated since version 4.0 |
||
781 | */ |
||
782 | public function getSettingName($field) |
||
789 | |||
790 | /** |
||
791 | 17 | * Append custom validation fields to the default 'Validation' |
|
792 | * section in the editable options view |
||
793 | 1 | * |
|
794 | 1 | * @return FieldList |
|
795 | */ |
||
796 | public function getFieldValidationOptions() |
||
808 | 3 | ||
809 | /** |
||
810 | * Return a FormField to appear on the front end. Implement on |
||
811 | 17 | * your subclass. |
|
812 | * |
||
813 | * @return FormField |
||
814 | 17 | */ |
|
815 | public function getFormField() |
||
819 | |||
820 | /** |
||
821 | 2 | * Updates a formfield with extensions |
|
822 | * |
||
823 | 2 | * @param FormField $field |
|
824 | */ |
||
825 | public function doUpdateFormField($field) |
||
831 | |||
832 | 2 | /** |
|
833 | * Updates a formfield with the additional metadata specified by this field |
||
834 | 2 | * |
|
835 | * @param FormField $field |
||
836 | */ |
||
837 | protected function updateFormField($field) |
||
872 | 2 | ||
873 | 2 | /** |
|
874 | 2 | * Return the instance of the submission field class |
|
875 | * |
||
876 | * @return SubmittedFormField |
||
877 | */ |
||
878 | public function getSubmittedFormField() |
||
882 | |||
883 | |||
884 | /** |
||
885 | * Show this form field (and its related value) in the reports and in emails. |
||
886 | * |
||
887 | * @return bool |
||
888 | */ |
||
889 | public function showInReports() |
||
893 | |||
894 | /** |
||
895 | * Return the error message for this field. Either uses the custom |
||
896 | * one (if provided) or the default SilverStripe message |
||
897 | * |
||
898 | * @return Varchar |
||
899 | */ |
||
900 | public function getErrorMessage() |
||
910 | |||
911 | /** |
||
912 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
913 | * to the field proper |
||
914 | * |
||
915 | * @param array $data Unserialised data |
||
916 | */ |
||
917 | public function migrateSettings($data) |
||
932 | |||
933 | /** |
||
934 | 2 | * Get the formfield to use when editing this inline in gridfield |
|
935 | 2 | * |
|
936 | * @param string $column name of column |
||
937 | 2 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
|
938 | 2 | * @return FormField |
|
939 | 2 | */ |
|
940 | public function getInlineClassnameField($column, $fieldClasses) |
||
944 | |||
945 | /** |
||
946 | 2 | * Get the formfield to use when editing the title inline |
|
947 | 2 | * |
|
948 | * @param string $column |
||
949 | * @return FormField |
||
950 | */ |
||
951 | 2 | public function getInlineTitleField($column) |
|
957 | |||
958 | /** |
||
959 | * Get the JS expression for selecting the holder for this field |
||
960 | * |
||
961 | * @return string |
||
962 | */ |
||
963 | public function getSelectorHolder() |
||
967 | |||
968 | /** |
||
969 | * Returns only the JS identifier of a string, less the $(), which can be inserted elsewhere, for example when you |
||
970 | * want to perform selections on multiple selectors |
||
971 | * @return string |
||
972 | 10 | */ |
|
973 | public function getSelectorOnly() |
||
977 | 10 | ||
978 | /** |
||
979 | * Gets the JS expression for selecting the value for this field |
||
980 | 1 | * |
|
981 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
982 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
983 | * |
||
984 | * @return string |
||
985 | */ |
||
986 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
990 | |||
991 | /** |
||
992 | * @return string |
||
993 | */ |
||
994 | public function getSelectorFieldOnly() |
||
998 | |||
999 | |||
1000 | /** |
||
1001 | * Get the list of classes that can be selected and used as data-values |
||
1002 | * |
||
1003 | * @param $includeLiterals Set to false to exclude non-data fields |
||
1004 | * @return array |
||
1005 | */ |
||
1006 | public function getEditableFieldClasses($includeLiterals = true) |
||
1034 | |||
1035 | /** |
||
1036 | * @return EditableFormFieldValidator |
||
1037 | */ |
||
1038 | public function getCMSValidator() |
||
1043 | |||
1044 | /** |
||
1045 | * Determine effective display rules for this field. |
||
1046 | * |
||
1047 | * @return SS_List |
||
1048 | */ |
||
1049 | public function EffectiveDisplayRules() |
||
1056 | |||
1057 | /** |
||
1058 | * Extracts info from DisplayRules into array so UserDefinedForm->buildWatchJS can run through it. |
||
1059 | * @return array|null |
||
1060 | */ |
||
1061 | public function formatDisplayRules() |
||
1102 | |||
1103 | /** |
||
1104 | * Replaces the set DisplayRulesConjunction with their JS logical operators |
||
1105 | * @return string |
||
1106 | */ |
||
1107 | public function DisplayRulesConjunctionNice() |
||
1111 | |||
1112 | /** |
||
1113 | * Replaces boolean ShowOnLoad with its JS string equivalent |
||
1114 | * @return string |
||
1115 | */ |
||
1116 | public function ShowOnLoadNice() |
||
1120 | |||
1121 | /** |
||
1122 | * Returns whether this is of type EditableCheckBoxField |
||
1123 | * @return bool |
||
1124 | */ |
||
1125 | public function isCheckBoxField() |
||
1129 | |||
1130 | /** |
||
1131 | * Returns whether this is of type EditableRadioField |
||
1132 | * @return bool |
||
1133 | */ |
||
1134 | public function isRadioField() |
||
1138 | |||
1139 | /** |
||
1140 | * Determined is this is of type EditableCheckboxGroupField |
||
1141 | * @return bool |
||
1142 | */ |
||
1143 | public function isCheckBoxGroupField() |
||
1147 | } |
||
1148 |
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.