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 | ); |
||
117 | |||
118 | private static $defaults = array( |
||
119 | 'ShowOnLoad' => true, |
||
120 | ); |
||
121 | |||
122 | |||
123 | /** |
||
124 | * @config |
||
125 | * @var array |
||
126 | */ |
||
127 | private static $has_one = array( |
||
128 | "Parent" => "UserDefinedForm", |
||
129 | ); |
||
130 | |||
131 | /** |
||
132 | * Built in extensions required |
||
133 | * |
||
134 | * @config |
||
135 | * @var array |
||
136 | */ |
||
137 | private static $extensions = array( |
||
138 | "SilverStripe\\ORM\\Versioning\\Versioned('Stage', 'Live')" |
||
139 | ); |
||
140 | |||
141 | /** |
||
142 | * @config |
||
143 | * @var array |
||
144 | */ |
||
145 | private static $has_many = array( |
||
146 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
147 | ); |
||
148 | |||
149 | /** |
||
150 | * @var bool |
||
151 | */ |
||
152 | protected $readonly; |
||
153 | |||
154 | /** |
||
155 | * Set the visibility of an individual form field |
||
156 | * |
||
157 | * @param bool |
||
158 | */ |
||
159 | public function setReadonly($readonly = true) |
||
163 | |||
164 | /** |
||
165 | * Returns whether this field is readonly |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | private function isReadonly() |
||
173 | |||
174 | /** |
||
175 | * @return FieldList |
||
176 | */ |
||
177 | public function getCMSFields() |
||
261 | |||
262 | /** |
||
263 | * Return fields to display on the 'Display Rules' tab |
||
264 | * |
||
265 | * @return FieldList |
||
266 | */ |
||
267 | protected function getDisplayRuleFields() |
||
335 | |||
336 | public function onBeforeWrite() |
||
355 | |||
356 | /** |
||
357 | * Generate a new non-conflicting Name value |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | 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 | 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 | 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 | 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 | View Code Duplication | public function canCreate($member = null, $context = array()) |
|
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 | View Code Duplication | protected function getCanCreateContext($context) |
|
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 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
||
522 | |||
523 | /** |
||
524 | * Delete this field from a given stage |
||
525 | * |
||
526 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
527 | */ |
||
528 | public function doDeleteFromStage($stage) |
||
540 | |||
541 | /** |
||
542 | * checks wether record is new, copied from Sitetree |
||
543 | */ |
||
544 | public function isNew() |
||
556 | |||
557 | /** |
||
558 | * checks if records is changed on stage |
||
559 | * @return boolean |
||
560 | */ |
||
561 | public function getIsModifiedOnStage() |
||
573 | |||
574 | /** |
||
575 | * @deprecated since version 4.0 |
||
576 | */ |
||
577 | public function getSettings() |
||
582 | |||
583 | /** |
||
584 | * @deprecated since version 4.0 |
||
585 | */ |
||
586 | public function setSettings($settings = array()) |
||
591 | |||
592 | /** |
||
593 | * @deprecated since version 4.0 |
||
594 | */ |
||
595 | public function setSetting($key, $value) |
||
603 | |||
604 | /** |
||
605 | * Set the allowed css classes for the extraClass custom setting |
||
606 | * |
||
607 | * @param array The permissible CSS classes to add |
||
608 | */ |
||
609 | public function setAllowedCss(array $allowed) |
||
617 | |||
618 | /** |
||
619 | * @deprecated since version 4.0 |
||
620 | */ |
||
621 | public function getSetting($setting) |
||
633 | |||
634 | /** |
||
635 | * Get the path to the icon for this field type, relative to the site root. |
||
636 | * |
||
637 | * @return string |
||
638 | */ |
||
639 | public function getIcon() |
||
643 | |||
644 | /** |
||
645 | * Return whether or not this field has addable options |
||
646 | * such as a dropdown field or radio set |
||
647 | * |
||
648 | * @return bool |
||
649 | */ |
||
650 | public function getHasAddableOptions() |
||
654 | |||
655 | /** |
||
656 | * Return whether or not this field needs to show the extra |
||
657 | * options dropdown list |
||
658 | * |
||
659 | * @return bool |
||
660 | */ |
||
661 | public function showExtraOptions() |
||
665 | |||
666 | /** |
||
667 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
668 | * |
||
669 | * @return string |
||
670 | */ |
||
671 | public function getEscapedTitle() |
||
675 | |||
676 | /** |
||
677 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
678 | * |
||
679 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
680 | * or groups |
||
681 | * |
||
682 | * @return string |
||
683 | */ |
||
684 | public function getFieldNumber() |
||
715 | |||
716 | public function getCMSTitle() |
||
720 | |||
721 | /** |
||
722 | * @deprecated since version 4.0 |
||
723 | */ |
||
724 | public function getFieldName($field = false) |
||
729 | |||
730 | /** |
||
731 | * @deprecated since version 4.0 |
||
732 | */ |
||
733 | public function getSettingName($field) |
||
740 | |||
741 | /** |
||
742 | * Append custom validation fields to the default 'Validation' |
||
743 | * section in the editable options view |
||
744 | * |
||
745 | * @return FieldList |
||
746 | */ |
||
747 | public function getFieldValidationOptions() |
||
759 | |||
760 | /** |
||
761 | * Return a FormField to appear on the front end. Implement on |
||
762 | * your subclass. |
||
763 | * |
||
764 | * @return FormField |
||
765 | */ |
||
766 | public function getFormField() |
||
770 | |||
771 | /** |
||
772 | * Updates a formfield with extensions |
||
773 | * |
||
774 | * @param FormField $field |
||
775 | */ |
||
776 | public function doUpdateFormField($field) |
||
782 | |||
783 | /** |
||
784 | * Updates a formfield with the additional metadata specified by this field |
||
785 | * |
||
786 | * @param FormField $field |
||
787 | */ |
||
788 | protected function updateFormField($field) |
||
818 | |||
819 | /** |
||
820 | * Return the instance of the submission field class |
||
821 | * |
||
822 | * @return SubmittedFormField |
||
823 | */ |
||
824 | public function getSubmittedFormField() |
||
828 | |||
829 | |||
830 | /** |
||
831 | * Show this form field (and its related value) in the reports and in emails. |
||
832 | * |
||
833 | * @return bool |
||
834 | */ |
||
835 | public function showInReports() |
||
839 | |||
840 | /** |
||
841 | * Return the error message for this field. Either uses the custom |
||
842 | * one (if provided) or the default SilverStripe message |
||
843 | * |
||
844 | * @return Varchar |
||
845 | */ |
||
846 | public function getErrorMessage() |
||
856 | |||
857 | /** |
||
858 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
859 | * to the field proper |
||
860 | * |
||
861 | * @param array $data Unserialised data |
||
862 | */ |
||
863 | public function migrateSettings($data) |
||
878 | |||
879 | /** |
||
880 | * Get the formfield to use when editing this inline in gridfield |
||
881 | * |
||
882 | * @param string $column name of column |
||
883 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
884 | * @return FormField |
||
885 | */ |
||
886 | public function getInlineClassnameField($column, $fieldClasses) |
||
890 | |||
891 | /** |
||
892 | * Get the formfield to use when editing the title inline |
||
893 | * |
||
894 | * @param string $column |
||
895 | * @return FormField |
||
896 | */ |
||
897 | public function getInlineTitleField($column) |
||
903 | |||
904 | /** |
||
905 | * Get the JS expression for selecting the holder for this field |
||
906 | * |
||
907 | * @return string |
||
908 | */ |
||
909 | public function getSelectorHolder() |
||
913 | |||
914 | /** |
||
915 | * Gets the JS expression for selecting the value for this field |
||
916 | * |
||
917 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
918 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
919 | */ |
||
920 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
924 | |||
925 | |||
926 | /** |
||
927 | * Get the list of classes that can be selected and used as data-values |
||
928 | * |
||
929 | * @param $includeLiterals Set to false to exclude non-data fields |
||
930 | * @return array |
||
931 | */ |
||
932 | public function getEditableFieldClasses($includeLiterals = true) |
||
960 | |||
961 | /** |
||
962 | * @return EditableFormFieldValidator |
||
963 | */ |
||
964 | public function getCMSValidator() |
||
969 | |||
970 | /** |
||
971 | * Determine effective display rules for this field. |
||
972 | * |
||
973 | * @return SS_List |
||
974 | */ |
||
975 | public function EffectiveDisplayRules() |
||
982 | } |
||
983 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: