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 |
||
| 47 | class EditableFormField extends DataObject |
||
| 48 | { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set to true to hide from class selector |
||
| 52 | * |
||
| 53 | * @config |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | private static $hidden = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Define this field as abstract (not inherited) |
||
| 60 | * |
||
| 61 | * @config |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | private static $abstract = true; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Flag this field type as non-data (e.g. literal, header, html) |
||
| 68 | * |
||
| 69 | * @config |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | private static $literal = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Default sort order |
||
| 76 | * |
||
| 77 | * @config |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private static $default_sort = '"Sort"'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * A list of CSS classes that can be added |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | public static $allowed_css = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @config |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private static $summary_fields = array( |
||
| 94 | 'Title' |
||
| 95 | ); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @config |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | private static $db = array( |
||
| 102 | "Name" => "Varchar", |
||
| 103 | "Title" => "Varchar(255)", |
||
| 104 | "Default" => "Varchar(255)", |
||
| 105 | "Sort" => "Int", |
||
| 106 | "Required" => "Boolean", |
||
| 107 | "CustomErrorMessage" => "Varchar(255)", |
||
| 108 | |||
| 109 | "CustomRules" => "Text", // @deprecated from 2.0 |
||
| 110 | "CustomSettings" => "Text", // @deprecated from 2.0 |
||
| 111 | "Migrated" => "Boolean", // set to true when migrated |
||
| 112 | |||
| 113 | "ExtraClass" => "Text", // from CustomSettings |
||
| 114 | "RightTitle" => "Varchar(255)", // from CustomSettings |
||
| 115 | "ShowOnLoad" => "Boolean(1)", // from CustomSettings |
||
| 116 | "ShowInSummary" => "Boolean", |
||
| 117 | ); |
||
| 118 | |||
| 119 | private static $defaults = array( |
||
| 120 | 'ShowOnLoad' => true, |
||
| 121 | ); |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @config |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | private static $has_one = array( |
||
| 129 | "Parent" => "UserDefinedForm", |
||
| 130 | ); |
||
| 131 | |||
| 132 | /** |
||
| 133 | 1 | * Built in extensions required |
|
| 134 | * |
||
| 135 | 1 | * @config |
|
| 136 | 1 | * @var array |
|
| 137 | */ |
||
| 138 | private static $extensions = array( |
||
| 139 | "SilverStripe\\ORM\\Versioning\\Versioned('Stage', 'Live')" |
||
| 140 | ); |
||
| 141 | |||
| 142 | /** |
||
| 143 | 1 | * @config |
|
| 144 | * @var array |
||
| 145 | 1 | */ |
|
| 146 | private static $has_many = array( |
||
| 147 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
| 148 | ); |
||
| 149 | |||
| 150 | /** |
||
| 151 | 10 | * @var bool |
|
| 152 | */ |
||
| 153 | 10 | protected $readonly; |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Set the visibility of an individual form field |
||
| 157 | 1 | * |
|
| 158 | * @param bool |
||
| 159 | */ |
||
| 160 | 10 | public function setReadonly($readonly = true) |
|
| 164 | 10 | ||
| 165 | /** |
||
| 166 | * Returns whether this field is readonly |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | private function isReadonly() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return FieldList |
||
| 177 | */ |
||
| 178 | public function getCMSFields() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Return fields to display on the 'Display Rules' tab |
||
| 266 | * |
||
| 267 | * @return FieldList |
||
| 268 | */ |
||
| 269 | protected function getDisplayRuleFields() |
||
| 337 | |||
| 338 | public function onBeforeWrite() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Generate a new non-conflicting Name value |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | protected function generateName() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
| 379 | 1 | * |
|
| 380 | * @return bool |
||
| 381 | 1 | */ |
|
| 382 | 1 | public function getSetsOwnError() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Return whether a user can delete this form field |
||
| 389 | * based on whether they can edit the page |
||
| 390 | * |
||
| 391 | * @param Member $member |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | public function canDelete($member = null) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Return whether a user can edit this form field |
||
| 401 | * based on whether they can edit the page |
||
| 402 | * |
||
| 403 | * @param Member $member |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | public function canEdit($member = null) |
||
| 430 | |||
| 431 | 3 | /** |
|
| 432 | 3 | * Return whether a user can view this form field |
|
| 433 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
| 434 | * |
||
| 435 | * @param Member $member |
||
| 436 | * @return bool |
||
| 437 | 3 | */ |
|
| 438 | public function canView($member = null) |
||
| 447 | |||
| 448 | /** |
||
| 449 | 3 | * Return whether a user can create an object of this type |
|
| 450 | * |
||
| 451 | * @param Member $member |
||
| 452 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 453 | 3 | * @return bool |
|
| 454 | */ |
||
| 455 | View Code Duplication | public function canCreate($member = null, $context = array()) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Helper method to check the parent for this object |
||
| 469 | * |
||
| 470 | * @param array $args List of arguments passed to canCreate |
||
| 471 | * @return SiteTree Parent page instance |
||
| 472 | */ |
||
| 473 | View Code Duplication | protected function getCanCreateContext($context) |
|
| 487 | |||
| 488 | 9 | /** |
|
| 489 | * Check if can publish |
||
| 490 | 9 | * |
|
| 491 | * @param Member $member |
||
| 492 | * @return bool |
||
| 493 | 9 | */ |
|
| 494 | 1 | public function canPublish($member = null) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Check if can unpublish |
||
| 501 | * |
||
| 502 | * @param Member $member |
||
| 503 | 2 | * @return bool |
|
| 504 | */ |
||
| 505 | public function canUnpublish($member = null) |
||
| 509 | 1 | ||
| 510 | 2 | /** |
|
| 511 | * Publish this Form Field to the live site |
||
| 512 | * |
||
| 513 | 2 | * Wrapper for the {@link Versioned} publish function |
|
| 514 | 2 | */ |
|
| 515 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Delete this field from a given stage |
||
| 527 | * |
||
| 528 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
| 529 | */ |
||
| 530 | public function doDeleteFromStage($stage) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * checks wether record is new, copied from Sitetree |
||
| 545 | */ |
||
| 546 | public function isNew() |
||
| 558 | |||
| 559 | /** |
||
| 560 | * checks if records is changed on stage |
||
| 561 | * @return boolean |
||
| 562 | */ |
||
| 563 | public function getIsModifiedOnStage() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @deprecated since version 4.0 |
||
| 578 | */ |
||
| 579 | public function getSettings() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @deprecated since version 4.0 |
||
| 587 | */ |
||
| 588 | public function setSettings($settings = array()) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @deprecated since version 4.0 |
||
| 596 | */ |
||
| 597 | public function setSetting($key, $value) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Set the allowed css classes for the extraClass custom setting |
||
| 608 | * |
||
| 609 | * @param array The permissible CSS classes to add |
||
| 610 | */ |
||
| 611 | public function setAllowedCss(array $allowed) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @deprecated since version 4.0 |
||
| 622 | */ |
||
| 623 | public function getSetting($setting) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the path to the icon for this field type, relative to the site root. |
||
| 638 | * |
||
| 639 | * @return string |
||
| 640 | */ |
||
| 641 | public function getIcon() |
||
| 645 | |||
| 646 | 18 | /** |
|
| 647 | * Return whether or not this field has addable options |
||
| 648 | 18 | * such as a dropdown field or radio set |
|
| 649 | * |
||
| 650 | * @return bool |
||
| 651 | */ |
||
| 652 | public function getHasAddableOptions() |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Return whether or not this field needs to show the extra |
||
| 659 | * options dropdown list |
||
| 660 | * |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | public function showExtraOptions() |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
| 670 | * |
||
| 671 | * @return string |
||
| 672 | */ |
||
| 673 | public function getEscapedTitle() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
| 680 | * |
||
| 681 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
| 682 | * or groups |
||
| 683 | * |
||
| 684 | * @return string |
||
| 685 | */ |
||
| 686 | public function getFieldNumber() |
||
| 717 | |||
| 718 | public function getCMSTitle() |
||
| 722 | |||
| 723 | /** |
||
| 724 | * @deprecated since version 4.0 |
||
| 725 | */ |
||
| 726 | public function getFieldName($field = false) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @deprecated since version 4.0 |
||
| 734 | */ |
||
| 735 | public function getSettingName($field) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Append custom validation fields to the default 'Validation' |
||
| 745 | * section in the editable options view |
||
| 746 | * |
||
| 747 | * @return FieldList |
||
| 748 | */ |
||
| 749 | public function getFieldValidationOptions() |
||
| 761 | |||
| 762 | /** |
||
| 763 | 17 | * Return a FormField to appear on the front end. Implement on |
|
| 764 | * your subclass. |
||
| 765 | * |
||
| 766 | 17 | * @return FormField |
|
| 767 | */ |
||
| 768 | public function getFormField() |
||
| 772 | 1 | ||
| 773 | /** |
||
| 774 | * Updates a formfield with extensions |
||
| 775 | 17 | * |
|
| 776 | * @param FormField $field |
||
| 777 | 3 | */ |
|
| 778 | 3 | public function doUpdateFormField($field) |
|
| 784 | 1 | ||
| 785 | 1 | /** |
|
| 786 | 3 | * Updates a formfield with the additional metadata specified by this field |
|
| 787 | * |
||
| 788 | * @param FormField $field |
||
| 789 | 17 | */ |
|
| 790 | protected function updateFormField($field) |
||
| 820 | |||
| 821 | 17 | /** |
|
| 822 | * Return the instance of the submission field class |
||
| 823 | 17 | * |
|
| 824 | 17 | * @return SubmittedFormField |
|
| 825 | */ |
||
| 826 | public function getSubmittedFormField() |
||
| 830 | |||
| 831 | |||
| 832 | /** |
||
| 833 | * Show this form field (and its related value) in the reports and in emails. |
||
| 834 | * |
||
| 835 | * @return bool |
||
| 836 | */ |
||
| 837 | public function showInReports() |
||
| 841 | 2 | ||
| 842 | 2 | /** |
|
| 843 | 2 | * Return the error message for this field. Either uses the custom |
|
| 844 | 2 | * one (if provided) or the default SilverStripe message |
|
| 845 | * |
||
| 846 | * @return Varchar |
||
| 847 | 2 | */ |
|
| 848 | 2 | public function getErrorMessage() |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
| 861 | * to the field proper |
||
| 862 | * |
||
| 863 | * @param array $data Unserialised data |
||
| 864 | */ |
||
| 865 | public function migrateSettings($data) |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Get the formfield to use when editing this inline in gridfield |
||
| 883 | * |
||
| 884 | 8 | * @param string $column name of column |
|
| 885 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
| 886 | 8 | * @return FormField |
|
| 887 | */ |
||
| 888 | public function getInlineClassnameField($column, $fieldClasses) |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Get the formfield to use when editing the title inline |
||
| 895 | * |
||
| 896 | * @param string $column |
||
| 897 | * @return FormField |
||
| 898 | */ |
||
| 899 | public function getInlineTitleField($column) |
||
| 905 | |||
| 906 | /** |
||
| 907 | 2 | * Get the JS expression for selecting the holder for this field |
|
| 908 | * |
||
| 909 | 2 | * @return string |
|
| 910 | */ |
||
| 911 | public function getSelectorHolder() |
||
| 915 | 2 | ||
| 916 | 2 | /** |
|
| 917 | 2 | * Gets the JS expression for selecting the value for this field |
|
| 918 | * |
||
| 919 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
| 920 | 2 | * @param bool $forOnLoad Set to true if this will be invoked on load |
|
| 921 | */ |
||
| 922 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
| 926 | |||
| 927 | |||
| 928 | /** |
||
| 929 | 2 | * Get the list of classes that can be selected and used as data-values |
|
| 930 | 2 | * |
|
| 931 | * @param $includeLiterals Set to false to exclude non-data fields |
||
| 932 | 2 | * @return array |
|
| 933 | 2 | */ |
|
| 934 | public function getEditableFieldClasses($includeLiterals = true) |
||
| 962 | |||
| 963 | /** |
||
| 964 | * @return EditableFormFieldValidator |
||
| 965 | */ |
||
| 966 | public function getCMSValidator() |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Determine effective display rules for this field. |
||
| 974 | * |
||
| 975 | * @return SS_List |
||
| 976 | */ |
||
| 977 | public function EffectiveDisplayRules() |
||
| 984 | } |
||
| 985 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: