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 | * @config |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private static $summary_fields = array( |
||
| 67 | 'Title' |
||
| 68 | ); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @config |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private static $db = array( |
||
| 75 | "Name" => "Varchar", |
||
| 76 | "Title" => "Varchar(255)", |
||
| 77 | "Default" => "Varchar(255)", |
||
| 78 | "Sort" => "Int", |
||
| 79 | "Required" => "Boolean", |
||
| 80 | "CustomErrorMessage" => "Varchar(255)", |
||
| 81 | |||
| 82 | "CustomRules" => "Text", // @deprecated from 2.0 |
||
| 83 | "CustomSettings" => "Text", // @deprecated from 2.0 |
||
| 84 | "Migrated" => "Boolean", // set to true when migrated |
||
| 85 | |||
| 86 | "ExtraClass" => "Text", // from CustomSettings |
||
| 87 | "RightTitle" => "Varchar(255)", // from CustomSettings |
||
| 88 | "ShowOnLoad" => "Boolean(1)", // from CustomSettings |
||
| 89 | ); |
||
| 90 | |||
| 91 | private static $defaults = array( |
||
| 92 | 'ShowOnLoad' => true, |
||
| 93 | ); |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * @config |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private static $has_one = array( |
||
| 101 | "Parent" => "UserDefinedForm", |
||
| 102 | ); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Built in extensions required |
||
| 106 | * |
||
| 107 | * @config |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private static $extensions = array( |
||
| 111 | "Versioned('Stage', 'Live')" |
||
| 112 | ); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @config |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | private static $has_many = array( |
||
| 119 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
| 120 | ); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | protected $readonly; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set the visibility of an individual form field |
||
| 129 | * |
||
| 130 | * @param bool |
||
| 131 | */ |
||
| 132 | public function setReadonly($readonly = true) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns whether this field is readonly |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | private function isReadonly() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return FieldList |
||
| 149 | */ |
||
| 150 | public function getCMSFields() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Return fields to display on the 'Display Rules' tab |
||
| 237 | * |
||
| 238 | * @return FieldList |
||
| 239 | */ |
||
| 240 | protected function getDisplayRuleFields() |
||
| 308 | |||
| 309 | public function onBeforeWrite() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Generate a new non-conflicting Name value |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | protected function generateName() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public function getSetsOwnError() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Return whether a user can delete this form field |
||
| 360 | * based on whether they can edit the page |
||
| 361 | * |
||
| 362 | * @param Member $member |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | public function canDelete($member = null) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Return whether a user can edit this form field |
||
| 372 | * based on whether they can edit the page |
||
| 373 | * |
||
| 374 | * @param Member $member |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | public function canEdit($member = null) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Return whether a user can view this form field |
||
| 404 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
| 405 | * |
||
| 406 | * @param Member $member |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | public function canView($member = null) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Return whether a user can create an object of this type |
||
| 421 | * |
||
| 422 | * @param Member $member |
||
| 423 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 424 | * @return bool |
||
| 425 | */ |
||
| 426 | View Code Duplication | public function canCreate($member = null) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Helper method to check the parent for this object |
||
| 440 | * |
||
| 441 | * @param array $args List of arguments passed to canCreate |
||
| 442 | * @return SiteTree Parent page instance |
||
| 443 | */ |
||
| 444 | View Code Duplication | protected function getCanCreateContext($args) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Check if can publish |
||
| 461 | * |
||
| 462 | * @param Member $member |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | public function canPublish($member = null) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Check if can unpublish |
||
| 472 | * |
||
| 473 | * @param Member $member |
||
| 474 | * @return bool |
||
| 475 | */ |
||
| 476 | public function canUnpublish($member = null) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Publish this Form Field to the live site |
||
| 483 | * |
||
| 484 | * Wrapper for the {@link Versioned} publish function |
||
| 485 | */ |
||
| 486 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Delete this field from a given stage |
||
| 498 | * |
||
| 499 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
| 500 | */ |
||
| 501 | public function doDeleteFromStage($stage) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * checks wether record is new, copied from Sitetree |
||
| 516 | */ |
||
| 517 | public function isNew() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * checks if records is changed on stage |
||
| 532 | * @return boolean |
||
| 533 | */ |
||
| 534 | public function getIsModifiedOnStage() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @deprecated since version 4.0 |
||
| 549 | */ |
||
| 550 | public function getSettings() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @deprecated since version 4.0 |
||
| 558 | */ |
||
| 559 | public function setSettings($settings = array()) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @deprecated since version 4.0 |
||
| 567 | */ |
||
| 568 | public function setSetting($key, $value) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Set the allowed css classes for the extraClass custom setting |
||
| 579 | * |
||
| 580 | * @param array The permissible CSS classes to add |
||
| 581 | */ |
||
| 582 | public function setAllowedCss(array $allowed) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @deprecated since version 4.0 |
||
| 593 | */ |
||
| 594 | public function getSetting($setting) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Get the path to the icon for this field type, relative to the site root. |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | public function getIcon() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Return whether or not this field has addable options |
||
| 619 | * such as a dropdown field or radio set |
||
| 620 | * |
||
| 621 | * @return bool |
||
| 622 | */ |
||
| 623 | public function getHasAddableOptions() |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Return whether or not this field needs to show the extra |
||
| 630 | * options dropdown list |
||
| 631 | * |
||
| 632 | * @return bool |
||
| 633 | */ |
||
| 634 | public function showExtraOptions() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
| 641 | * |
||
| 642 | * @return string |
||
| 643 | */ |
||
| 644 | public function getEscapedTitle() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
| 651 | * |
||
| 652 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
| 653 | * or groups |
||
| 654 | * |
||
| 655 | * @return string |
||
| 656 | */ |
||
| 657 | public function getFieldNumber() |
||
| 688 | |||
| 689 | public function getCMSTitle() |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @deprecated since version 4.0 |
||
| 696 | */ |
||
| 697 | public function getFieldName($field = false) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @deprecated since version 4.0 |
||
| 705 | */ |
||
| 706 | public function getSettingName($field) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Append custom validation fields to the default 'Validation' |
||
| 716 | * section in the editable options view |
||
| 717 | * |
||
| 718 | * @return FieldList |
||
| 719 | */ |
||
| 720 | public function getFieldValidationOptions() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Return a FormField to appear on the front end. Implement on |
||
| 735 | * your subclass. |
||
| 736 | * |
||
| 737 | * @return FormField |
||
| 738 | */ |
||
| 739 | public function getFormField() |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Updates a formfield with extensions |
||
| 746 | * |
||
| 747 | * @param FormField $field |
||
| 748 | */ |
||
| 749 | public function doUpdateFormField($field) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Updates a formfield with the additional metadata specified by this field |
||
| 758 | * |
||
| 759 | * @param FormField $field |
||
| 760 | */ |
||
| 761 | protected function updateFormField($field) |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Return the instance of the submission field class |
||
| 794 | * |
||
| 795 | * @return SubmittedFormField |
||
| 796 | */ |
||
| 797 | public function getSubmittedFormField() |
||
| 801 | |||
| 802 | |||
| 803 | /** |
||
| 804 | * Show this form field (and its related value) in the reports and in emails. |
||
| 805 | * |
||
| 806 | * @return bool |
||
| 807 | */ |
||
| 808 | public function showInReports() |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Return the error message for this field. Either uses the custom |
||
| 815 | * one (if provided) or the default SilverStripe message |
||
| 816 | * |
||
| 817 | * @return Varchar |
||
| 818 | */ |
||
| 819 | public function getErrorMessage() |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
| 832 | * to the field proper |
||
| 833 | * |
||
| 834 | * @param array $data Unserialised data |
||
| 835 | */ |
||
| 836 | public function migrateSettings($data) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Get the formfield to use when editing this inline in gridfield |
||
| 854 | * |
||
| 855 | * @param string $column name of column |
||
| 856 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
| 857 | * @return FormField |
||
| 858 | */ |
||
| 859 | public function getInlineClassnameField($column, $fieldClasses) |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Get the formfield to use when editing the title inline |
||
| 866 | * |
||
| 867 | * @param string $column |
||
| 868 | * @return FormField |
||
| 869 | */ |
||
| 870 | public function getInlineTitleField($column) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Get the JS expression for selecting the holder for this field |
||
| 879 | * |
||
| 880 | * @return string |
||
| 881 | */ |
||
| 882 | public function getSelectorHolder() |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Gets the JS expression for selecting the value for this field |
||
| 889 | * |
||
| 890 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
| 891 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
| 892 | */ |
||
| 893 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
| 897 | |||
| 898 | |||
| 899 | /** |
||
| 900 | * Get the list of classes that can be selected and used as data-values |
||
| 901 | * |
||
| 902 | * @param $includeLiterals Set to false to exclude non-data fields |
||
| 903 | * @return array |
||
| 904 | */ |
||
| 905 | public function getEditableFieldClasses($includeLiterals = true) |
||
| 933 | |||
| 934 | /** |
||
| 935 | * @return EditableFormFieldValidator |
||
| 936 | */ |
||
| 937 | public function getCMSValidator() |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Determine effective display rules for this field. |
||
| 945 | * |
||
| 946 | * @return SS_List |
||
| 947 | */ |
||
| 948 | public function EffectiveDisplayRules() |
||
| 955 | } |
||
| 956 |
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.