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 | * Set to true to hide from class selector |
||
24 | * |
||
25 | * @config |
||
26 | * @var bool |
||
27 | */ |
||
28 | private static $hidden = false; |
||
29 | |||
30 | /** |
||
31 | * Define this field as abstract (not inherited) |
||
32 | * |
||
33 | * @config |
||
34 | * @var bool |
||
35 | */ |
||
36 | private static $abstract = true; |
||
37 | |||
38 | /** |
||
39 | * Flag this field type as non-data (e.g. literal, header, html) |
||
40 | * |
||
41 | * @config |
||
42 | * @var bool |
||
43 | */ |
||
44 | private static $literal = false; |
||
45 | |||
46 | /** |
||
47 | * Default sort order |
||
48 | * |
||
49 | * @config |
||
50 | * @var string |
||
51 | */ |
||
52 | private static $default_sort = '"Sort"'; |
||
53 | |||
54 | /** |
||
55 | * A list of CSS classes that can be added |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | public static $allowed_css = array(); |
||
60 | |||
61 | /** |
||
62 | * @config |
||
63 | * @var array |
||
64 | */ |
||
65 | private static $summary_fields = array( |
||
66 | 'Title' |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * @config |
||
71 | * @var array |
||
72 | */ |
||
73 | private static $db = array( |
||
74 | "Name" => "Varchar", |
||
75 | "Title" => "Varchar(255)", |
||
76 | "Default" => "Varchar(255)", |
||
77 | "Sort" => "Int", |
||
78 | "Required" => "Boolean", |
||
79 | "CustomErrorMessage" => "Varchar(255)", |
||
80 | |||
81 | "CustomRules" => "Text", // @deprecated from 2.0 |
||
82 | "CustomSettings" => "Text", // @deprecated from 2.0 |
||
83 | "Migrated" => "Boolean", // set to true when migrated |
||
84 | |||
85 | "ExtraClass" => "Text", // from CustomSettings |
||
86 | "RightTitle" => "Varchar(255)", // from CustomSettings |
||
87 | "ShowOnLoad" => "Boolean(1)", // from CustomSettings |
||
88 | ); |
||
89 | |||
90 | private static $defaults = array( |
||
91 | 'ShowOnLoad' => true, |
||
92 | ); |
||
93 | |||
94 | |||
95 | /** |
||
96 | * @config |
||
97 | * @var array |
||
98 | */ |
||
99 | private static $has_one = array( |
||
100 | "Parent" => "UserDefinedForm", |
||
101 | ); |
||
102 | |||
103 | /** |
||
104 | * Built in extensions required |
||
105 | * |
||
106 | * @config |
||
107 | * @var array |
||
108 | */ |
||
109 | private static $extensions = array( |
||
110 | "Versioned('Stage', 'Live')" |
||
111 | ); |
||
112 | |||
113 | /** |
||
114 | * @config |
||
115 | * @var array |
||
116 | */ |
||
117 | private static $has_many = array( |
||
118 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
119 | ); |
||
120 | |||
121 | /** |
||
122 | * @var bool |
||
123 | */ |
||
124 | protected $readonly; |
||
125 | |||
126 | /** |
||
127 | * Set the visibility of an individual form field |
||
128 | * |
||
129 | * @param bool |
||
130 | */ |
||
131 | public function setReadonly($readonly = true) { |
||
134 | |||
135 | /** |
||
136 | * Returns whether this field is readonly |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | private function isReadonly() { |
||
143 | |||
144 | /** |
||
145 | * @return FieldList |
||
146 | */ |
||
147 | public function getCMSFields() { |
||
148 | $fields = new FieldList(new TabSet('Root')); |
||
149 | |||
150 | // Main tab |
||
151 | $fields->addFieldsToTab( |
||
152 | 'Root.Main', |
||
153 | array( |
||
154 | ReadonlyField::create( |
||
155 | 'Type', |
||
156 | _t('EditableFormField.TYPE', 'Type'), |
||
157 | $this->i18n_singular_name() |
||
158 | ), |
||
159 | LiteralField::create( |
||
160 | 'MergeField', |
||
161 | _t( |
||
162 | 'EditableFormField.MERGEFIELDNAME', |
||
163 | '<div class="field readonly">' . |
||
164 | '<label class="left">Merge field</label>' . |
||
165 | '<div class="middleColumn">' . |
||
166 | '<span class="readonly">$' . $this->Name . '</span>' . |
||
167 | '</div>' . |
||
168 | '</div>' |
||
169 | ) |
||
170 | ), |
||
171 | TextField::create('Title'), |
||
172 | TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')), |
||
173 | TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')), |
||
174 | SegmentField::create('Name')->setModifiers(array( |
||
175 | UnderscoreSegmentFieldModifier::create()->setDefault('FieldName'), |
||
176 | DisambiguationSegmentFieldModifier::create(), |
||
177 | ))->setPreview($this->Name) |
||
178 | ) |
||
179 | ); |
||
180 | |||
181 | // Custom settings |
||
182 | if (!empty(self::$allowed_css)) { |
||
183 | $cssList = array(); |
||
184 | foreach(self::$allowed_css as $k => $v) { |
||
185 | if (!is_array($v)) { |
||
186 | $cssList[$k]=$v; |
||
187 | } elseif ($k === $this->ClassName) { |
||
188 | $cssList = array_merge($cssList, $v); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | $fields->addFieldToTab('Root.Main', |
||
193 | DropdownField::create( |
||
194 | 'ExtraClass', |
||
195 | _t('EditableFormField.EXTRACLASS_TITLE', 'Extra Styling/Layout'), |
||
196 | $cssList |
||
197 | )->setDescription(_t( |
||
198 | 'EditableFormField.EXTRACLASS_SELECT', |
||
199 | 'Select from the list of allowed styles' |
||
200 | )) |
||
201 | ); |
||
202 | } else { |
||
203 | $fields->addFieldToTab('Root.Main', |
||
204 | TextField::create( |
||
205 | 'ExtraClass', |
||
206 | _t('EditableFormField.EXTRACLASS_Title', 'Extra CSS classes') |
||
207 | )->setDescription(_t( |
||
208 | 'EditableFormField.EXTRACLASS_MULTIPLE', |
||
209 | 'Separate each CSS class with a single space' |
||
210 | )) |
||
211 | ); |
||
212 | } |
||
213 | |||
214 | // Validation |
||
215 | $validationFields = $this->getFieldValidationOptions(); |
||
216 | if($validationFields && $validationFields->count()) { |
||
217 | $fields->addFieldsToTab('Root.Validation', $validationFields); |
||
218 | } |
||
219 | |||
220 | // Add display rule fields |
||
221 | $displayFields = $this->getDisplayRuleFields(); |
||
222 | if($displayFields && $displayFields->count()) { |
||
223 | $fields->addFieldsToTab('Root.DisplayRules', $displayFields); |
||
224 | } |
||
225 | |||
226 | $this->extend('updateCMSFields', $fields); |
||
227 | |||
228 | return $fields; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Return fields to display on the 'Display Rules' tab |
||
233 | * |
||
234 | * @return FieldList |
||
235 | */ |
||
236 | protected function getDisplayRuleFields() { |
||
303 | |||
304 | public function onBeforeWrite() { |
||
305 | parent::onBeforeWrite(); |
||
306 | |||
307 | // Set a field name. |
||
308 | if(!$this->Name) { |
||
309 | // New random name |
||
310 | $this->Name = $this->generateName(); |
||
311 | |||
312 | } elseif($this->Name === 'Field') { |
||
313 | throw new ValidationException('Field name cannot be "Field"'); |
||
314 | } |
||
315 | |||
316 | if(!$this->Sort && $this->ParentID) { |
||
317 | $parentID = $this->ParentID; |
||
318 | $this->Sort = EditableFormField::get() |
||
319 | ->filter('ParentID', $parentID) |
||
320 | ->max('Sort') + 1; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Generate a new non-conflicting Name value |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | protected function generateName() { |
||
330 | do { |
||
331 | // Generate a new random name after this class |
||
332 | $class = get_class($this); |
||
333 | $entropy = substr(sha1(uniqid()), 0, 5); |
||
334 | $name = "{$class}_{$entropy}"; |
||
335 | |||
336 | // Check if it conflicts |
||
337 | $exists = EditableFormField::get()->filter('Name', $name)->count() > 0; |
||
338 | } while($exists); |
||
339 | return $name; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function getSetsOwnError() { |
||
350 | |||
351 | /** |
||
352 | * Return whether a user can delete this form field |
||
353 | * based on whether they can edit the page |
||
354 | * |
||
355 | * @param Member $member |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function canDelete($member = null) { |
||
361 | |||
362 | /** |
||
363 | * Return whether a user can edit this form field |
||
364 | * based on whether they can edit the page |
||
365 | * |
||
366 | * @param Member $member |
||
367 | * @return bool |
||
368 | */ |
||
369 | public function canEdit($member = null) { |
||
385 | |||
386 | /** |
||
387 | * Return whether a user can view this form field |
||
388 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
389 | * |
||
390 | * @param Member $member |
||
391 | * @return bool |
||
392 | */ |
||
393 | public function canView($member = null) { |
||
401 | |||
402 | /** |
||
403 | * Return whether a user can create an object of this type |
||
404 | * |
||
405 | * @param Member $member |
||
406 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
407 | * @return bool |
||
408 | */ |
||
409 | View Code Duplication | public function canCreate($member = null) { |
|
419 | |||
420 | /** |
||
421 | * Helper method to check the parent for this object |
||
422 | * |
||
423 | * @param array $args List of arguments passed to canCreate |
||
424 | * @return SiteTree Parent page instance |
||
425 | */ |
||
426 | View Code Duplication | protected function getCanCreateContext($args) { |
|
439 | |||
440 | /** |
||
441 | * Check if can publish |
||
442 | * |
||
443 | * @param Member $member |
||
444 | * @return bool |
||
445 | */ |
||
446 | public function canPublish($member = null) { |
||
449 | |||
450 | /** |
||
451 | * Check if can unpublish |
||
452 | * |
||
453 | * @param Member $member |
||
454 | * @return bool |
||
455 | */ |
||
456 | public function canUnpublish($member = null) { |
||
459 | |||
460 | /** |
||
461 | * Publish this Form Field to the live site |
||
462 | * |
||
463 | * Wrapper for the {@link Versioned} publish function |
||
464 | */ |
||
465 | public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
||
473 | |||
474 | /** |
||
475 | * Delete this field from a given stage |
||
476 | * |
||
477 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
478 | */ |
||
479 | public function doDeleteFromStage($stage) { |
||
490 | |||
491 | /** |
||
492 | * checks wether record is new, copied from Sitetree |
||
493 | */ |
||
494 | function isNew() { |
||
501 | |||
502 | /** |
||
503 | * checks if records is changed on stage |
||
504 | * @return boolean |
||
505 | */ |
||
506 | public function getIsModifiedOnStage() { |
||
515 | |||
516 | /** |
||
517 | * @deprecated since version 4.0 |
||
518 | */ |
||
519 | public function getSettings() { |
||
523 | |||
524 | /** |
||
525 | * @deprecated since version 4.0 |
||
526 | */ |
||
527 | public function setSettings($settings = array()) { |
||
531 | |||
532 | /** |
||
533 | * @deprecated since version 4.0 |
||
534 | */ |
||
535 | public function setSetting($key, $value) { |
||
542 | |||
543 | /** |
||
544 | * Set the allowed css classes for the extraClass custom setting |
||
545 | * |
||
546 | * @param array The permissible CSS classes to add |
||
547 | */ |
||
548 | public function setAllowedCss(array $allowed) { |
||
555 | |||
556 | /** |
||
557 | * @deprecated since version 4.0 |
||
558 | */ |
||
559 | public function getSetting($setting) { |
||
570 | |||
571 | /** |
||
572 | * Get the path to the icon for this field type, relative to the site root. |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | public function getIcon() { |
||
579 | |||
580 | /** |
||
581 | * Return whether or not this field has addable options |
||
582 | * such as a dropdown field or radio set |
||
583 | * |
||
584 | * @return bool |
||
585 | */ |
||
586 | public function getHasAddableOptions() { |
||
589 | |||
590 | /** |
||
591 | * Return whether or not this field needs to show the extra |
||
592 | * options dropdown list |
||
593 | * |
||
594 | * @return bool |
||
595 | */ |
||
596 | public function showExtraOptions() { |
||
599 | |||
600 | /** |
||
601 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
602 | * |
||
603 | * @return string |
||
604 | */ |
||
605 | public function getEscapedTitle() { |
||
608 | |||
609 | /** |
||
610 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
611 | * |
||
612 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
613 | * or groups |
||
614 | * |
||
615 | * @return string |
||
616 | */ |
||
617 | public function getFieldNumber() { |
||
647 | |||
648 | public function getCMSTitle() { |
||
651 | |||
652 | /** |
||
653 | * @deprecated since version 4.0 |
||
654 | */ |
||
655 | public function getFieldName($field = false) { |
||
659 | |||
660 | /** |
||
661 | * @deprecated since version 4.0 |
||
662 | */ |
||
663 | public function getSettingName($field) { |
||
669 | |||
670 | /** |
||
671 | * Append custom validation fields to the default 'Validation' |
||
672 | * section in the editable options view |
||
673 | * |
||
674 | * @return FieldList |
||
675 | */ |
||
676 | public function getFieldValidationOptions() { |
||
687 | |||
688 | /** |
||
689 | * Return a FormField to appear on the front end. Implement on |
||
690 | * your subclass. |
||
691 | * |
||
692 | * @return FormField |
||
693 | */ |
||
694 | public function getFormField() { |
||
697 | |||
698 | /** |
||
699 | * Updates a formfield with extensions |
||
700 | * |
||
701 | * @param FormField $field |
||
702 | */ |
||
703 | public function doUpdateFormField($field) { |
||
708 | |||
709 | /** |
||
710 | * Updates a formfield with the additional metadata specified by this field |
||
711 | * |
||
712 | * @param FormField $field |
||
713 | */ |
||
714 | protected function updateFormField($field) { |
||
743 | |||
744 | /** |
||
745 | * Return the instance of the submission field class |
||
746 | * |
||
747 | * @return SubmittedFormField |
||
748 | */ |
||
749 | public function getSubmittedFormField() { |
||
752 | |||
753 | |||
754 | /** |
||
755 | * Show this form field (and its related value) in the reports and in emails. |
||
756 | * |
||
757 | * @return bool |
||
758 | */ |
||
759 | public function showInReports() { |
||
762 | |||
763 | /** |
||
764 | * Return the error message for this field. Either uses the custom |
||
765 | * one (if provided) or the default SilverStripe message |
||
766 | * |
||
767 | * @return Varchar |
||
768 | */ |
||
769 | public function getErrorMessage() { |
||
778 | |||
779 | /** |
||
780 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
781 | * to the field proper |
||
782 | * |
||
783 | * @param array $data Unserialised data |
||
784 | */ |
||
785 | public function migrateSettings($data) { |
||
799 | |||
800 | /** |
||
801 | * Get the formfield to use when editing this inline in gridfield |
||
802 | * |
||
803 | * @param string $column name of column |
||
804 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
805 | * @return FormField |
||
806 | */ |
||
807 | public function getInlineClassnameField($column, $fieldClasses) { |
||
810 | |||
811 | /** |
||
812 | * Get the formfield to use when editing the title inline |
||
813 | * |
||
814 | * @param string $column |
||
815 | * @return FormField |
||
816 | */ |
||
817 | public function getInlineTitleField($column) { |
||
822 | |||
823 | /** |
||
824 | * Get the JS expression for selecting the holder for this field |
||
825 | * |
||
826 | * @return string |
||
827 | */ |
||
828 | public function getSelectorHolder() { |
||
831 | |||
832 | /** |
||
833 | * Gets the JS expression for selecting the value for this field |
||
834 | * |
||
835 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
836 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
837 | */ |
||
838 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) { |
||
841 | |||
842 | |||
843 | /** |
||
844 | * Get the list of classes that can be selected and used as data-values |
||
845 | * |
||
846 | * @param $includeLiterals Set to false to exclude non-data fields |
||
847 | * @return array |
||
848 | */ |
||
849 | public function getEditableFieldClasses($includeLiterals = true) { |
||
876 | |||
877 | /** |
||
878 | * @return EditableFormFieldValidator |
||
879 | */ |
||
880 | public function getCMSValidator() { |
||
884 | |||
885 | /** |
||
886 | * Determine effective display rules for this field. |
||
887 | * |
||
888 | * @return SS_List |
||
889 | */ |
||
890 | public function EffectiveDisplayRules() { |
||
896 | |||
897 | } |
||
898 |
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.