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 | "ShowInSummary" => "Boolean", |
||
| 90 | ); |
||
| 91 | |||
| 92 | private static $defaults = array( |
||
| 93 | 'ShowOnLoad' => true, |
||
| 94 | ); |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * @config |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | private static $has_one = array( |
||
| 102 | "Parent" => "UserDefinedForm", |
||
| 103 | ); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Built in extensions required |
||
| 107 | * |
||
| 108 | * @config |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private static $extensions = array( |
||
| 112 | "Versioned('Stage', 'Live')" |
||
| 113 | ); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @config |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | private static $has_many = array( |
||
| 120 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
| 121 | ); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | protected $readonly; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set the visibility of an individual form field |
||
| 130 | * |
||
| 131 | * @param bool |
||
| 132 | */ |
||
| 133 | 1 | public function setReadonly($readonly = true) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Returns whether this field is readonly |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 1 | private function isReadonly() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return FieldList |
||
| 150 | */ |
||
| 151 | 10 | public function getCMSFields() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Return fields to display on the 'Display Rules' tab |
||
| 241 | * |
||
| 242 | * @return FieldList |
||
| 243 | */ |
||
| 244 | protected function getDisplayRuleFields() |
||
| 316 | |||
| 317 | 39 | public function onBeforeWrite() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Generate a new non-conflicting Name value |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | 25 | protected function generateName() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
| 358 | * |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | public function getSetsOwnError() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Return whether a user can delete this form field |
||
| 368 | * based on whether they can edit the page |
||
| 369 | * |
||
| 370 | * @param Member $member |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | 1 | public function canDelete($member = null) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Return whether a user can edit this form field |
||
| 380 | * based on whether they can edit the page |
||
| 381 | * |
||
| 382 | * @param Member $member |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 1 | public function canEdit($member = null) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Return whether a user can view this form field |
||
| 412 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
| 413 | * |
||
| 414 | * @param Member $member |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | 1 | public function canView($member = null) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Return whether a user can create an object of this type |
||
| 429 | * |
||
| 430 | * @param Member $member |
||
| 431 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | 3 | View Code Duplication | public function canCreate($member = null) |
| 445 | |||
| 446 | /** |
||
| 447 | * Helper method to check the parent for this object |
||
| 448 | * |
||
| 449 | * @param array $args List of arguments passed to canCreate |
||
| 450 | * @return SiteTree Parent page instance |
||
| 451 | */ |
||
| 452 | 3 | View Code Duplication | protected function getCanCreateContext($args) |
| 466 | |||
| 467 | /** |
||
| 468 | * Check if can publish |
||
| 469 | * |
||
| 470 | * @param Member $member |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | public function canPublish($member = null) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Check if can unpublish |
||
| 480 | * |
||
| 481 | * @param Member $member |
||
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | public function canUnpublish($member = null) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Publish this Form Field to the live site |
||
| 491 | * |
||
| 492 | * Wrapper for the {@link Versioned} publish function |
||
| 493 | */ |
||
| 494 | 9 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Delete this field from a given stage |
||
| 522 | * |
||
| 523 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
| 524 | */ |
||
| 525 | 1 | public function doDeleteFromStage($stage) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * checks wether record is new, copied from Sitetree |
||
| 540 | */ |
||
| 541 | public function isNew() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * checks if records is changed on stage |
||
| 556 | * @return boolean |
||
| 557 | */ |
||
| 558 | public function getIsModifiedOnStage() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @deprecated since version 4.0 |
||
| 573 | */ |
||
| 574 | public function getSettings() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @deprecated since version 4.0 |
||
| 582 | */ |
||
| 583 | public function setSettings($settings = array()) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @deprecated since version 4.0 |
||
| 591 | */ |
||
| 592 | public function setSetting($key, $value) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Set the allowed css classes for the extraClass custom setting |
||
| 603 | * |
||
| 604 | * @param array The permissible CSS classes to add |
||
| 605 | */ |
||
| 606 | public function setAllowedCss(array $allowed) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @deprecated since version 4.0 |
||
| 617 | */ |
||
| 618 | public function getSetting($setting) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get the path to the icon for this field type, relative to the site root. |
||
| 633 | * |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | public function getIcon() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Return whether or not this field has addable options |
||
| 643 | * such as a dropdown field or radio set |
||
| 644 | * |
||
| 645 | * @return bool |
||
| 646 | */ |
||
| 647 | public function getHasAddableOptions() |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Return whether or not this field needs to show the extra |
||
| 654 | * options dropdown list |
||
| 655 | * |
||
| 656 | * @return bool |
||
| 657 | */ |
||
| 658 | public function showExtraOptions() |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
| 665 | * |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | 18 | public function getEscapedTitle() |
|
| 672 | |||
| 673 | /** |
||
| 674 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
| 675 | * |
||
| 676 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
| 677 | * or groups |
||
| 678 | * |
||
| 679 | * @return string |
||
| 680 | */ |
||
| 681 | public function getFieldNumber() |
||
| 712 | |||
| 713 | public function getCMSTitle() |
||
| 717 | |||
| 718 | /** |
||
| 719 | * @deprecated since version 4.0 |
||
| 720 | */ |
||
| 721 | public function getFieldName($field = false) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @deprecated since version 4.0 |
||
| 729 | */ |
||
| 730 | public function getSettingName($field) |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Append custom validation fields to the default 'Validation' |
||
| 740 | * section in the editable options view |
||
| 741 | * |
||
| 742 | * @return FieldList |
||
| 743 | */ |
||
| 744 | public function getFieldValidationOptions() |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Return a FormField to appear on the front end. Implement on |
||
| 759 | * your subclass. |
||
| 760 | * |
||
| 761 | * @return FormField |
||
| 762 | */ |
||
| 763 | public function getFormField() |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Updates a formfield with extensions |
||
| 770 | * |
||
| 771 | * @param FormField $field |
||
| 772 | */ |
||
| 773 | 17 | public function doUpdateFormField($field) |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Updates a formfield with the additional metadata specified by this field |
||
| 782 | * |
||
| 783 | * @param FormField $field |
||
| 784 | */ |
||
| 785 | 17 | protected function updateFormField($field) |
|
| 815 | |||
| 816 | /** |
||
| 817 | * Return the instance of the submission field class |
||
| 818 | * |
||
| 819 | * @return SubmittedFormField |
||
| 820 | */ |
||
| 821 | 2 | public function getSubmittedFormField() |
|
| 825 | |||
| 826 | |||
| 827 | /** |
||
| 828 | * Show this form field (and its related value) in the reports and in emails. |
||
| 829 | * |
||
| 830 | * @return bool |
||
| 831 | */ |
||
| 832 | 2 | public function showInReports() |
|
| 836 | |||
| 837 | /** |
||
| 838 | * Return the error message for this field. Either uses the custom |
||
| 839 | * one (if provided) or the default SilverStripe message |
||
| 840 | * |
||
| 841 | * @return Varchar |
||
| 842 | */ |
||
| 843 | 17 | public function getErrorMessage() |
|
| 853 | |||
| 854 | /** |
||
| 855 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
| 856 | * to the field proper |
||
| 857 | * |
||
| 858 | * @param array $data Unserialised data |
||
| 859 | */ |
||
| 860 | 2 | public function migrateSettings($data) |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Get the formfield to use when editing this inline in gridfield |
||
| 878 | * |
||
| 879 | * @param string $column name of column |
||
| 880 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
| 881 | * @return FormField |
||
| 882 | */ |
||
| 883 | public function getInlineClassnameField($column, $fieldClasses) |
||
| 887 | |||
| 888 | /** |
||
| 889 | * Get the formfield to use when editing the title inline |
||
| 890 | * |
||
| 891 | * @param string $column |
||
| 892 | * @return FormField |
||
| 893 | */ |
||
| 894 | public function getInlineTitleField($column) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Get the JS expression for selecting the holder for this field |
||
| 903 | * |
||
| 904 | * @return string |
||
| 905 | */ |
||
| 906 | 8 | public function getSelectorHolder() |
|
| 910 | |||
| 911 | /** |
||
| 912 | * Gets the JS expression for selecting the value for this field |
||
| 913 | * |
||
| 914 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
| 915 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
| 916 | */ |
||
| 917 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
| 921 | |||
| 922 | |||
| 923 | /** |
||
| 924 | * Get the list of classes that can be selected and used as data-values |
||
| 925 | * |
||
| 926 | * @param $includeLiterals Set to false to exclude non-data fields |
||
| 927 | * @return array |
||
| 928 | */ |
||
| 929 | 2 | public function getEditableFieldClasses($includeLiterals = true) |
|
| 957 | |||
| 958 | /** |
||
| 959 | * @return EditableFormFieldValidator |
||
| 960 | */ |
||
| 961 | public function getCMSValidator() |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Determine effective display rules for this field. |
||
| 969 | * |
||
| 970 | * @return SS_List |
||
| 971 | */ |
||
| 972 | 10 | public function EffectiveDisplayRules() |
|
| 979 | } |
||
| 980 |
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.