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 |
||
| 20 | class EditableFormField extends DataObject |
||
|
|
|||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Set to true to hide from class selector |
||
| 25 | * |
||
| 26 | * @config |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | private static $hidden = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Define this field as abstract (not inherited) |
||
| 33 | * |
||
| 34 | * @config |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | private static $abstract = true; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Flag this field type as non-data (e.g. literal, header, html) |
||
| 41 | * |
||
| 42 | * @config |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | private static $literal = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Default sort order |
||
| 49 | * |
||
| 50 | * @config |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private static $default_sort = '"Sort"'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * A list of CSS classes that can be added |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | public static $allowed_css = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Set this to true to enable placeholder field for any given class |
||
| 64 | * @config |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | private static $has_placeholder = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @config |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private static $summary_fields = array( |
||
| 74 | 'Title' |
||
| 75 | ); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @config |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private static $db = array( |
||
| 82 | "Name" => "Varchar", |
||
| 83 | "Title" => "Varchar(255)", |
||
| 84 | "Default" => "Varchar(255)", |
||
| 85 | "Sort" => "Int", |
||
| 86 | "Required" => "Boolean", |
||
| 87 | "CustomErrorMessage" => "Varchar(255)", |
||
| 88 | |||
| 89 | "CustomRules" => "Text", // @deprecated from 2.0 |
||
| 90 | "CustomSettings" => "Text", // @deprecated from 2.0 |
||
| 91 | "Migrated" => "Boolean", // set to true when migrated |
||
| 92 | |||
| 93 | "ExtraClass" => "Text", // from CustomSettings |
||
| 94 | "RightTitle" => "Varchar(255)", // from CustomSettings |
||
| 95 | "ShowOnLoad" => "Boolean(1)", // from CustomSettings |
||
| 96 | "ShowInSummary" => "Boolean", |
||
| 97 | "Placeholder" => "Varchar(255)" |
||
| 98 | ); |
||
| 99 | |||
| 100 | private static $defaults = array( |
||
| 101 | 'ShowOnLoad' => true, |
||
| 102 | ); |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * @config |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | private static $has_one = array( |
||
| 110 | "Parent" => "UserDefinedForm", |
||
| 111 | ); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Built in extensions required |
||
| 115 | * |
||
| 116 | * @config |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | private static $extensions = array( |
||
| 120 | "Versioned('Stage', 'Live')" |
||
| 121 | ); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @config |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | private static $has_many = array( |
||
| 128 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
| 129 | ); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var bool |
||
| 133 | */ |
||
| 134 | protected $readonly; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set the visibility of an individual form field |
||
| 138 | * |
||
| 139 | * @param bool |
||
| 140 | */ |
||
| 141 | 10 | public function setReadonly($readonly = true) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Returns whether this field is readonly |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | 11 | private function isReadonly() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @return FieldList |
||
| 158 | */ |
||
| 159 | public function getCMSFields() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Return fields to display on the 'Display Rules' tab |
||
| 260 | * |
||
| 261 | * @return FieldList |
||
| 262 | */ |
||
| 263 | protected function getDisplayRuleFields() |
||
| 335 | |||
| 336 | 39 | public function onBeforeWrite() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Generate a new non-conflicting Name value |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | 25 | protected function generateName() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function getSetsOwnError() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Return whether a user can delete this form field |
||
| 387 | * based on whether they can edit the page |
||
| 388 | * |
||
| 389 | * @param Member $member |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | 1 | public function canDelete($member = null) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Return whether a user can edit this form field |
||
| 399 | * based on whether they can edit the page |
||
| 400 | * |
||
| 401 | * @param Member $member |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | 1 | public function canEdit($member = null) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Return whether a user can view this form field |
||
| 431 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
| 432 | * |
||
| 433 | * @param Member $member |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | 1 | public function canView($member = null) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Return whether a user can create an object of this type |
||
| 448 | * |
||
| 449 | * @param Member $member |
||
| 450 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | 3 | View Code Duplication | public function canCreate($member = null) |
| 464 | |||
| 465 | /** |
||
| 466 | * Helper method to check the parent for this object |
||
| 467 | * |
||
| 468 | * @param array $args List of arguments passed to canCreate |
||
| 469 | * @return SiteTree Parent page instance |
||
| 470 | */ |
||
| 471 | 3 | View Code Duplication | protected function getCanCreateContext($args) |
| 485 | |||
| 486 | /** |
||
| 487 | * Check if can publish |
||
| 488 | * |
||
| 489 | * @param Member $member |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | public function canPublish($member = null) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Check if can unpublish |
||
| 499 | * |
||
| 500 | * @param Member $member |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | public function canUnpublish($member = null) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Publish this Form Field to the live site |
||
| 510 | * |
||
| 511 | * Wrapper for the {@link Versioned} publish function |
||
| 512 | */ |
||
| 513 | 9 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Delete this field from a given stage |
||
| 541 | * |
||
| 542 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
| 543 | */ |
||
| 544 | 1 | public function doDeleteFromStage($stage) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * checks wether record is new, copied from Sitetree |
||
| 559 | */ |
||
| 560 | public function isNew() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * checks if records is changed on stage |
||
| 575 | * @return boolean |
||
| 576 | */ |
||
| 577 | public function getIsModifiedOnStage() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @deprecated since version 4.0 |
||
| 592 | */ |
||
| 593 | public function getSettings() |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @deprecated since version 4.0 |
||
| 601 | */ |
||
| 602 | public function setSettings($settings = array()) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @deprecated since version 4.0 |
||
| 610 | */ |
||
| 611 | public function setSetting($key, $value) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Set the allowed css classes for the extraClass custom setting |
||
| 622 | * |
||
| 623 | * @param array The permissible CSS classes to add |
||
| 624 | */ |
||
| 625 | public function setAllowedCss(array $allowed) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @deprecated since version 4.0 |
||
| 636 | */ |
||
| 637 | public function getSetting($setting) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Get the path to the icon for this field type, relative to the site root. |
||
| 652 | * |
||
| 653 | * @return string |
||
| 654 | */ |
||
| 655 | public function getIcon() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Return whether or not this field has addable options |
||
| 662 | * such as a dropdown field or radio set |
||
| 663 | * |
||
| 664 | * @return bool |
||
| 665 | */ |
||
| 666 | public function getHasAddableOptions() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Return whether or not this field needs to show the extra |
||
| 673 | * options dropdown list |
||
| 674 | * |
||
| 675 | * @return bool |
||
| 676 | */ |
||
| 677 | public function showExtraOptions() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
| 684 | * |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | 18 | public function getEscapedTitle() |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
| 694 | * |
||
| 695 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
| 696 | * or groups |
||
| 697 | * |
||
| 698 | * @return string |
||
| 699 | */ |
||
| 700 | public function getFieldNumber() |
||
| 731 | |||
| 732 | public function getCMSTitle() |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @deprecated since version 4.0 |
||
| 739 | */ |
||
| 740 | public function getFieldName($field = false) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * @deprecated since version 4.0 |
||
| 748 | */ |
||
| 749 | public function getSettingName($field) |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Append custom validation fields to the default 'Validation' |
||
| 759 | * section in the editable options view |
||
| 760 | * |
||
| 761 | * @return FieldList |
||
| 762 | */ |
||
| 763 | public function getFieldValidationOptions() |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Return a FormField to appear on the front end. Implement on |
||
| 778 | * your subclass. |
||
| 779 | * |
||
| 780 | * @return FormField |
||
| 781 | */ |
||
| 782 | public function getFormField() |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Updates a formfield with extensions |
||
| 789 | * |
||
| 790 | * @param FormField $field |
||
| 791 | */ |
||
| 792 | 17 | public function doUpdateFormField($field) |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Updates a formfield with the additional metadata specified by this field |
||
| 801 | * |
||
| 802 | * @param FormField $field |
||
| 803 | */ |
||
| 804 | 17 | protected function updateFormField($field) |
|
| 839 | |||
| 840 | /** |
||
| 841 | * Return the instance of the submission field class |
||
| 842 | * |
||
| 843 | * @return SubmittedFormField |
||
| 844 | */ |
||
| 845 | 2 | public function getSubmittedFormField() |
|
| 849 | |||
| 850 | |||
| 851 | /** |
||
| 852 | * Show this form field (and its related value) in the reports and in emails. |
||
| 853 | * |
||
| 854 | * @return bool |
||
| 855 | */ |
||
| 856 | 2 | public function showInReports() |
|
| 860 | |||
| 861 | /** |
||
| 862 | * Return the error message for this field. Either uses the custom |
||
| 863 | * one (if provided) or the default SilverStripe message |
||
| 864 | * |
||
| 865 | * @return Varchar |
||
| 866 | */ |
||
| 867 | 17 | public function getErrorMessage() |
|
| 877 | |||
| 878 | /** |
||
| 879 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
| 880 | * to the field proper |
||
| 881 | * |
||
| 882 | * @param array $data Unserialised data |
||
| 883 | */ |
||
| 884 | 2 | public function migrateSettings($data) |
|
| 899 | |||
| 900 | /** |
||
| 901 | * Get the formfield to use when editing this inline in gridfield |
||
| 902 | * |
||
| 903 | * @param string $column name of column |
||
| 904 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
| 905 | * @return FormField |
||
| 906 | */ |
||
| 907 | public function getInlineClassnameField($column, $fieldClasses) |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Get the formfield to use when editing the title inline |
||
| 914 | * |
||
| 915 | * @param string $column |
||
| 916 | * @return FormField |
||
| 917 | */ |
||
| 918 | public function getInlineTitleField($column) |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Get the JS expression for selecting the holder for this field |
||
| 927 | * |
||
| 928 | * @return string |
||
| 929 | */ |
||
| 930 | 8 | public function getSelectorHolder() |
|
| 934 | |||
| 935 | /** |
||
| 936 | * Gets the JS expression for selecting the value for this field |
||
| 937 | * |
||
| 938 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
| 939 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
| 940 | */ |
||
| 941 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
| 945 | |||
| 946 | |||
| 947 | /** |
||
| 948 | * Get the list of classes that can be selected and used as data-values |
||
| 949 | * |
||
| 950 | * @param $includeLiterals Set to false to exclude non-data fields |
||
| 951 | * @return array |
||
| 952 | */ |
||
| 953 | 2 | public function getEditableFieldClasses($includeLiterals = true) |
|
| 981 | |||
| 982 | /** |
||
| 983 | * @return EditableFormFieldValidator |
||
| 984 | */ |
||
| 985 | public function getCMSValidator() |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Determine effective display rules for this field. |
||
| 993 | * |
||
| 994 | * @return SS_List |
||
| 995 | */ |
||
| 996 | 10 | public function EffectiveDisplayRules() |
|
| 1003 | } |
||
| 1004 |
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.