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 | * Set to true to hide from class selector |
||
| 27 | * |
||
| 28 | * @config |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | private static $hidden = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Define this field as abstract (not inherited) |
||
| 35 | * |
||
| 36 | * @config |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | private static $abstract = true; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Flag this field type as non-data (e.g. literal, header, html) |
||
| 43 | * |
||
| 44 | * @config |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private static $literal = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Default sort order |
||
| 51 | * |
||
| 52 | * @config |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private static $default_sort = '"Sort"'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * A list of CSS classes that can be added |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | public static $allowed_css = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Set this to true to enable placeholder field for any given class |
||
| 66 | * @config |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | private static $has_placeholder = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @config |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private static $summary_fields = array( |
||
| 76 | 'Title' |
||
| 77 | ); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @config |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private static $db = array( |
||
| 84 | "Name" => "Varchar", |
||
| 85 | "Title" => "Varchar(255)", |
||
| 86 | "Default" => "Varchar(255)", |
||
| 87 | "Sort" => "Int", |
||
| 88 | "Required" => "Boolean", |
||
| 89 | "CustomErrorMessage" => "Varchar(255)", |
||
| 90 | |||
| 91 | "CustomRules" => "Text", // @deprecated from 2.0 |
||
| 92 | "CustomSettings" => "Text", // @deprecated from 2.0 |
||
| 93 | "Migrated" => "Boolean", // set to true when migrated |
||
| 94 | |||
| 95 | "ExtraClass" => "Text", // from CustomSettings |
||
| 96 | "RightTitle" => "Varchar(255)", // from CustomSettings |
||
| 97 | "ShowOnLoad" => "Boolean(1)", // from CustomSettings |
||
| 98 | "ShowInSummary" => "Boolean", |
||
| 99 | "Placeholder" => "Varchar(255)", |
||
| 100 | 'DisplayRulesConjunction' => 'Enum("And,Or","Or")', |
||
| 101 | ); |
||
| 102 | |||
| 103 | private static $defaults = array( |
||
| 104 | 'ShowOnLoad' => true, |
||
| 105 | ); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @config |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private static $has_one = array( |
||
| 112 | "Parent" => "UserDefinedForm", |
||
| 113 | ); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Built in extensions required |
||
| 117 | * |
||
| 118 | * @config |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private static $extensions = array( |
||
| 122 | "Versioned('Stage', 'Live')" |
||
| 123 | ); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @config |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private static $has_many = array( |
||
| 130 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
| 131 | ); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var bool |
||
| 135 | */ |
||
| 136 | protected $readonly; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Property holds the JS event which gets fired for this type of element |
||
| 140 | * |
||
| 141 | * @var string |
||
| 142 | */ |
||
| 143 | protected $jsEventHandler = 'change'; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the jsEventHandler property for the current object. Bearing in mind it could've been overridden. |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 2 | public function getJsEventHandler() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Set the visibility of an individual form field |
||
| 156 | * |
||
| 157 | * @param bool |
||
| 158 | */ |
||
| 159 | 1 | public function setReadonly($readonly = true) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Returns whether this field is readonly |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | 1 | private function isReadonly() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @return FieldList |
||
| 176 | */ |
||
| 177 | 1 | public function getCMSFields() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Return fields to display on the 'Display Rules' tab |
||
| 292 | * |
||
| 293 | * @return FieldList |
||
| 294 | */ |
||
| 295 | protected function getDisplayRuleFields() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @throws ValidationException |
||
| 367 | */ |
||
| 368 | 43 | public function onBeforeWrite() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Generate a new non-conflicting Name value |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | 26 | protected function generateName() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | public function getSetsOwnError() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Return whether a user can delete this form field |
||
| 419 | * based on whether they can edit the page |
||
| 420 | * |
||
| 421 | * @param Member $member |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | 1 | public function canDelete($member = null) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Return whether a user can edit this form field |
||
| 431 | * based on whether they can edit the page |
||
| 432 | * |
||
| 433 | * @param Member $member |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | 1 | public function canEdit($member = null) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Return whether a user can view this form field |
||
| 463 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
| 464 | * |
||
| 465 | * @param Member $member |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | 1 | public function canView($member = null) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Return whether a user can create an object of this type |
||
| 480 | * |
||
| 481 | * @param Member $member |
||
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | 3 | View Code Duplication | public function canCreate($member = null) |
| 495 | |||
| 496 | /** |
||
| 497 | * Helper method to check the parent for this object |
||
| 498 | * |
||
| 499 | * @param array $args List of arguments passed to canCreate |
||
| 500 | * @return SiteTree Parent page instance |
||
| 501 | */ |
||
| 502 | 3 | View Code Duplication | protected function getCanCreateContext($args) |
| 516 | |||
| 517 | /** |
||
| 518 | * Check if can publish |
||
| 519 | * |
||
| 520 | * @param Member $member |
||
| 521 | * @return bool |
||
| 522 | */ |
||
| 523 | public function canPublish($member = null) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Check if can unpublish |
||
| 530 | * |
||
| 531 | * @param Member $member |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | public function canUnpublish($member = null) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Publish this Form Field to the live site |
||
| 541 | * |
||
| 542 | * Wrapper for the {@link Versioned} publish function |
||
| 543 | * |
||
| 544 | * @param string $fromStage |
||
| 545 | * @param string $toStage |
||
| 546 | * @param bool $createNewVersion |
||
| 547 | */ |
||
| 548 | 10 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Publish all field rules |
||
| 556 | * |
||
| 557 | * @param string $fromStage |
||
| 558 | * @param string $toStage |
||
| 559 | * @param bool $createNewVersion |
||
| 560 | */ |
||
| 561 | 10 | View Code Duplication | protected function publishRules($fromStage, $toStage, $createNewVersion) |
| 584 | |||
| 585 | /** |
||
| 586 | * Delete this field from a given stage |
||
| 587 | * |
||
| 588 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
| 589 | * |
||
| 590 | * @param string $stage |
||
| 591 | */ |
||
| 592 | 1 | public function doDeleteFromStage($stage) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * checks whether record is new, copied from SiteTree |
||
| 607 | */ |
||
| 608 | public function isNew() |
||
| 620 | |||
| 621 | /** |
||
| 622 | * checks if records is changed on stage |
||
| 623 | * @return boolean |
||
| 624 | */ |
||
| 625 | public function getIsModifiedOnStage() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @deprecated since version 4.0 |
||
| 640 | */ |
||
| 641 | public function getSettings() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @deprecated since version 4.0 |
||
| 649 | * |
||
| 650 | * @param array $settings |
||
| 651 | */ |
||
| 652 | public function setSettings($settings = array()) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @deprecated since version 4.0 |
||
| 660 | * @param string $key |
||
| 661 | * @param mixed $value |
||
| 662 | */ |
||
| 663 | public function setSetting($key, $value) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Set the allowed css classes for the extraClass custom setting |
||
| 674 | * |
||
| 675 | * @param array $allowed The permissible CSS classes to add |
||
| 676 | */ |
||
| 677 | public function setAllowedCss(array $allowed) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @deprecated since version 4.0 |
||
| 688 | * @param string $setting |
||
| 689 | * @return mixed|string |
||
| 690 | */ |
||
| 691 | public function getSetting($setting) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Get the path to the icon for this field type, relative to the site root. |
||
| 706 | * |
||
| 707 | * @return string |
||
| 708 | */ |
||
| 709 | public function getIcon() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Return whether or not this field has addable options |
||
| 716 | * such as a dropdown field or radio set |
||
| 717 | * |
||
| 718 | * @return bool |
||
| 719 | */ |
||
| 720 | public function getHasAddableOptions() |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Return whether or not this field needs to show the extra |
||
| 727 | * options dropdown list |
||
| 728 | * |
||
| 729 | * @return bool |
||
| 730 | */ |
||
| 731 | public function showExtraOptions() |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
| 738 | * |
||
| 739 | * @return string |
||
| 740 | */ |
||
| 741 | 18 | public function getEscapedTitle() |
|
| 745 | |||
| 746 | /** |
||
| 747 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
| 748 | * |
||
| 749 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
| 750 | * or groups |
||
| 751 | * |
||
| 752 | * @return string |
||
| 753 | */ |
||
| 754 | public function getFieldNumber() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * @return string |
||
| 790 | */ |
||
| 791 | public function getCMSTitle() |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @deprecated since version 4.0 |
||
| 798 | * @param bool $field |
||
| 799 | * |
||
| 800 | * @return string |
||
| 801 | */ |
||
| 802 | public function getFieldName($field = false) |
||
| 807 | |||
| 808 | /** |
||
| 809 | * @deprecated since version 4.0 |
||
| 810 | * @param $field |
||
| 811 | * |
||
| 812 | * @return string |
||
| 813 | */ |
||
| 814 | public function getSettingName($field) |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Append custom validation fields to the default 'Validation' |
||
| 824 | * section in the editable options view |
||
| 825 | * |
||
| 826 | * @return FieldList |
||
| 827 | */ |
||
| 828 | public function getFieldValidationOptions() |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Return a FormField to appear on the front end. Implement on |
||
| 843 | * your subclass. |
||
| 844 | * |
||
| 845 | * @return FormField |
||
| 846 | */ |
||
| 847 | public function getFormField() |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Updates a formfield with extensions |
||
| 854 | * |
||
| 855 | * @param FormField $field |
||
| 856 | */ |
||
| 857 | 17 | public function doUpdateFormField($field) |
|
| 863 | |||
| 864 | /** |
||
| 865 | * Updates a formfield with the additional metadata specified by this field |
||
| 866 | * |
||
| 867 | * @param FormField $field |
||
| 868 | */ |
||
| 869 | 17 | protected function updateFormField($field) |
|
| 904 | |||
| 905 | /** |
||
| 906 | * Return the instance of the submission field class |
||
| 907 | * |
||
| 908 | * @return SubmittedFormField |
||
| 909 | */ |
||
| 910 | 2 | public function getSubmittedFormField() |
|
| 914 | |||
| 915 | |||
| 916 | /** |
||
| 917 | * Show this form field (and its related value) in the reports and in emails. |
||
| 918 | * |
||
| 919 | * @return bool |
||
| 920 | */ |
||
| 921 | 2 | public function showInReports() |
|
| 925 | |||
| 926 | /** |
||
| 927 | * Return the error message for this field. Either uses the custom |
||
| 928 | * one (if provided) or the default SilverStripe message |
||
| 929 | * |
||
| 930 | * @return Varchar |
||
| 931 | */ |
||
| 932 | 17 | public function getErrorMessage() |
|
| 945 | |||
| 946 | /** |
||
| 947 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
| 948 | * to the field proper |
||
| 949 | * |
||
| 950 | * @param array $data Unserialised data |
||
| 951 | */ |
||
| 952 | 2 | public function migrateSettings($data) |
|
| 967 | |||
| 968 | /** |
||
| 969 | * Get the formfield to use when editing this inline in gridfield |
||
| 970 | * |
||
| 971 | * @param string $column name of column |
||
| 972 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
| 973 | * @return FormField |
||
| 974 | */ |
||
| 975 | public function getInlineClassnameField($column, $fieldClasses) |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Get the formfield to use when editing the title inline |
||
| 982 | * |
||
| 983 | * @param string $column |
||
| 984 | * @return FormField |
||
| 985 | */ |
||
| 986 | public function getInlineTitleField($column) |
||
| 992 | |||
| 993 | /** |
||
| 994 | * Get the JS expression for selecting the holder for this field |
||
| 995 | * |
||
| 996 | * @return string |
||
| 997 | */ |
||
| 998 | public function getSelectorHolder() |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Returns only the JS identifier of a string, less the $(), which can be inserted elsewhere, for example when you |
||
| 1005 | * want to perform selections on multiple selectors |
||
| 1006 | * @return string |
||
| 1007 | */ |
||
| 1008 | 9 | public function getSelectorOnly() |
|
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Gets the JS expression for selecting the value for this field |
||
| 1015 | * |
||
| 1016 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
| 1017 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
| 1018 | * |
||
| 1019 | * @return string |
||
| 1020 | */ |
||
| 1021 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * @return string |
||
| 1028 | */ |
||
| 1029 | 2 | public function getSelectorFieldOnly() |
|
| 1033 | |||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Get the list of classes that can be selected and used as data-values |
||
| 1037 | * |
||
| 1038 | * @param bool $includeLiterals Set to false to exclude non-data fields |
||
| 1039 | * @return array |
||
| 1040 | */ |
||
| 1041 | 3 | public function getEditableFieldClasses($includeLiterals = true) |
|
| 1069 | |||
| 1070 | /** |
||
| 1071 | * @return EditableFormFieldValidator |
||
| 1072 | */ |
||
| 1073 | public function getCMSValidator() |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * Determine effective display rules for this field. |
||
| 1081 | * |
||
| 1082 | * @return SS_List |
||
| 1083 | */ |
||
| 1084 | 11 | public function EffectiveDisplayRules() |
|
| 1091 | |||
| 1092 | /** |
||
| 1093 | * Extracts info from DisplayRules into array so UserDefinedForm->buildWatchJS can run through it. |
||
| 1094 | * @return array|null |
||
| 1095 | */ |
||
| 1096 | 9 | public function formatDisplayRules() |
|
| 1137 | |||
| 1138 | /** |
||
| 1139 | * Replaces the set DisplayRulesConjunction with their JS logical operators |
||
| 1140 | * @return string |
||
| 1141 | */ |
||
| 1142 | 9 | public function DisplayRulesConjunctionNice() |
|
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Replaces boolean ShowOnLoad with its JS string equivalent |
||
| 1149 | * @return string |
||
| 1150 | */ |
||
| 1151 | 9 | public function ShowOnLoadNice() |
|
| 1155 | |||
| 1156 | /** |
||
| 1157 | * Returns whether this is of type EditableCheckBoxField |
||
| 1158 | * @return bool |
||
| 1159 | */ |
||
| 1160 | 2 | public function isCheckBoxField() |
|
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Returns whether this is of type EditableRadioField |
||
| 1167 | * @return bool |
||
| 1168 | */ |
||
| 1169 | 2 | public function isRadioField() |
|
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Determined is this is of type EditableCheckboxGroupField |
||
| 1176 | * @return bool |
||
| 1177 | */ |
||
| 1178 | public function isCheckBoxGroupField() |
||
| 1182 | } |
||
| 1183 |
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.