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() |
|
236 | |||
237 | /** |
||
238 | * Return fields to display on the 'Display Rules' tab |
||
239 | * |
||
240 | * @return FieldList |
||
241 | */ |
||
242 | protected function getDisplayRuleFields() |
||
310 | |||
311 | 39 | public function onBeforeWrite() |
|
330 | |||
331 | /** |
||
332 | * Generate a new non-conflicting Name value |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | 25 | protected function generateName() |
|
349 | |||
350 | /** |
||
351 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
352 | * |
||
353 | * @return bool |
||
354 | */ |
||
355 | public function getSetsOwnError() |
||
359 | |||
360 | /** |
||
361 | * Return whether a user can delete this form field |
||
362 | * based on whether they can edit the page |
||
363 | * |
||
364 | * @param Member $member |
||
365 | * @return bool |
||
366 | */ |
||
367 | 1 | public function canDelete($member = null) |
|
371 | |||
372 | /** |
||
373 | * Return whether a user can edit this form field |
||
374 | * based on whether they can edit the page |
||
375 | * |
||
376 | * @param Member $member |
||
377 | * @return bool |
||
378 | */ |
||
379 | 1 | public function canEdit($member = null) |
|
403 | |||
404 | /** |
||
405 | * Return whether a user can view this form field |
||
406 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
407 | * |
||
408 | * @param Member $member |
||
409 | * @return bool |
||
410 | */ |
||
411 | 1 | public function canView($member = null) |
|
420 | |||
421 | /** |
||
422 | * Return whether a user can create an object of this type |
||
423 | * |
||
424 | * @param Member $member |
||
425 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
426 | * @return bool |
||
427 | */ |
||
428 | 3 | View Code Duplication | public function canCreate($member = null) |
439 | |||
440 | /** |
||
441 | * Helper method to check the parent for this object |
||
442 | * |
||
443 | * @param array $args List of arguments passed to canCreate |
||
444 | * @return SiteTree Parent page instance |
||
445 | */ |
||
446 | 3 | View Code Duplication | protected function getCanCreateContext($args) |
460 | |||
461 | /** |
||
462 | * Check if can publish |
||
463 | * |
||
464 | * @param Member $member |
||
465 | * @return bool |
||
466 | */ |
||
467 | public function canPublish($member = null) |
||
471 | |||
472 | /** |
||
473 | * Check if can unpublish |
||
474 | * |
||
475 | * @param Member $member |
||
476 | * @return bool |
||
477 | */ |
||
478 | public function canUnpublish($member = null) |
||
482 | |||
483 | /** |
||
484 | * Publish this Form Field to the live site |
||
485 | * |
||
486 | * Wrapper for the {@link Versioned} publish function |
||
487 | */ |
||
488 | 9 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
|
513 | |||
514 | /** |
||
515 | * Delete this field from a given stage |
||
516 | * |
||
517 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
518 | */ |
||
519 | 1 | public function doDeleteFromStage($stage) |
|
531 | |||
532 | /** |
||
533 | * checks wether record is new, copied from Sitetree |
||
534 | */ |
||
535 | public function isNew() |
||
547 | |||
548 | /** |
||
549 | * checks if records is changed on stage |
||
550 | * @return boolean |
||
551 | */ |
||
552 | public function getIsModifiedOnStage() |
||
564 | |||
565 | /** |
||
566 | * @deprecated since version 4.0 |
||
567 | */ |
||
568 | public function getSettings() |
||
573 | |||
574 | /** |
||
575 | * @deprecated since version 4.0 |
||
576 | */ |
||
577 | public function setSettings($settings = array()) |
||
582 | |||
583 | /** |
||
584 | * @deprecated since version 4.0 |
||
585 | */ |
||
586 | public function setSetting($key, $value) |
||
594 | |||
595 | /** |
||
596 | * Set the allowed css classes for the extraClass custom setting |
||
597 | * |
||
598 | * @param array The permissible CSS classes to add |
||
599 | */ |
||
600 | public function setAllowedCss(array $allowed) |
||
608 | |||
609 | /** |
||
610 | * @deprecated since version 4.0 |
||
611 | */ |
||
612 | public function getSetting($setting) |
||
624 | |||
625 | /** |
||
626 | * Get the path to the icon for this field type, relative to the site root. |
||
627 | * |
||
628 | * @return string |
||
629 | */ |
||
630 | public function getIcon() |
||
634 | |||
635 | /** |
||
636 | * Return whether or not this field has addable options |
||
637 | * such as a dropdown field or radio set |
||
638 | * |
||
639 | * @return bool |
||
640 | */ |
||
641 | public function getHasAddableOptions() |
||
645 | |||
646 | /** |
||
647 | * Return whether or not this field needs to show the extra |
||
648 | * options dropdown list |
||
649 | * |
||
650 | * @return bool |
||
651 | */ |
||
652 | public function showExtraOptions() |
||
656 | |||
657 | /** |
||
658 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
659 | * |
||
660 | * @return string |
||
661 | */ |
||
662 | 18 | public function getEscapedTitle() |
|
666 | |||
667 | /** |
||
668 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
669 | * |
||
670 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
671 | * or groups |
||
672 | * |
||
673 | * @return string |
||
674 | */ |
||
675 | public function getFieldNumber() |
||
706 | |||
707 | public function getCMSTitle() |
||
711 | |||
712 | /** |
||
713 | * @deprecated since version 4.0 |
||
714 | */ |
||
715 | public function getFieldName($field = false) |
||
720 | |||
721 | /** |
||
722 | * @deprecated since version 4.0 |
||
723 | */ |
||
724 | public function getSettingName($field) |
||
731 | |||
732 | /** |
||
733 | * Append custom validation fields to the default 'Validation' |
||
734 | * section in the editable options view |
||
735 | * |
||
736 | * @return FieldList |
||
737 | */ |
||
738 | public function getFieldValidationOptions() |
||
750 | |||
751 | /** |
||
752 | * Return a FormField to appear on the front end. Implement on |
||
753 | * your subclass. |
||
754 | * |
||
755 | * @return FormField |
||
756 | */ |
||
757 | public function getFormField() |
||
761 | |||
762 | /** |
||
763 | * Updates a formfield with extensions |
||
764 | * |
||
765 | * @param FormField $field |
||
766 | */ |
||
767 | 17 | public function doUpdateFormField($field) |
|
773 | |||
774 | /** |
||
775 | * Updates a formfield with the additional metadata specified by this field |
||
776 | * |
||
777 | * @param FormField $field |
||
778 | */ |
||
779 | 17 | protected function updateFormField($field) |
|
809 | |||
810 | /** |
||
811 | * Return the instance of the submission field class |
||
812 | * |
||
813 | * @return SubmittedFormField |
||
814 | */ |
||
815 | 2 | public function getSubmittedFormField() |
|
819 | |||
820 | |||
821 | /** |
||
822 | * Show this form field (and its related value) in the reports and in emails. |
||
823 | * |
||
824 | * @return bool |
||
825 | */ |
||
826 | 2 | public function showInReports() |
|
830 | |||
831 | /** |
||
832 | * Return the error message for this field. Either uses the custom |
||
833 | * one (if provided) or the default SilverStripe message |
||
834 | * |
||
835 | * @return Varchar |
||
836 | */ |
||
837 | 17 | public function getErrorMessage() |
|
847 | |||
848 | /** |
||
849 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
850 | * to the field proper |
||
851 | * |
||
852 | * @param array $data Unserialised data |
||
853 | */ |
||
854 | 2 | public function migrateSettings($data) |
|
869 | |||
870 | /** |
||
871 | * Get the formfield to use when editing this inline in gridfield |
||
872 | * |
||
873 | * @param string $column name of column |
||
874 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
875 | * @return FormField |
||
876 | */ |
||
877 | public function getInlineClassnameField($column, $fieldClasses) |
||
881 | |||
882 | /** |
||
883 | * Get the formfield to use when editing the title inline |
||
884 | * |
||
885 | * @param string $column |
||
886 | * @return FormField |
||
887 | */ |
||
888 | public function getInlineTitleField($column) |
||
894 | |||
895 | /** |
||
896 | * Get the JS expression for selecting the holder for this field |
||
897 | * |
||
898 | * @return string |
||
899 | */ |
||
900 | 8 | public function getSelectorHolder() |
|
904 | |||
905 | /** |
||
906 | * Gets the JS expression for selecting the value for this field |
||
907 | * |
||
908 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
909 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
910 | */ |
||
911 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
915 | |||
916 | |||
917 | /** |
||
918 | * Get the list of classes that can be selected and used as data-values |
||
919 | * |
||
920 | * @param $includeLiterals Set to false to exclude non-data fields |
||
921 | * @return array |
||
922 | */ |
||
923 | 2 | public function getEditableFieldClasses($includeLiterals = true) |
|
951 | |||
952 | /** |
||
953 | * @return EditableFormFieldValidator |
||
954 | */ |
||
955 | public function getCMSValidator() |
||
960 | |||
961 | /** |
||
962 | * Determine effective display rules for this field. |
||
963 | * |
||
964 | * @return SS_List |
||
965 | */ |
||
966 | 10 | public function EffectiveDisplayRules() |
|
973 | } |
||
974 |
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.