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 |
||
15 | class EditableFormField extends DataObject { |
||
|
|||
16 | |||
17 | /** |
||
18 | * Set to true to hide from class selector |
||
19 | * |
||
20 | * @config |
||
21 | * @var bool |
||
22 | */ |
||
23 | private static $hidden = false; |
||
24 | |||
25 | /** |
||
26 | * Define this field as abstract (not inherited) |
||
27 | * |
||
28 | * @config |
||
29 | * @var bool |
||
30 | */ |
||
31 | private static $abstract = true; |
||
32 | |||
33 | /** |
||
34 | * Flag this field type as non-data (e.g. literal, header, html) |
||
35 | * |
||
36 | * @config |
||
37 | * @var bool |
||
38 | */ |
||
39 | private static $literal = false; |
||
40 | |||
41 | /** |
||
42 | * Default sort order |
||
43 | * |
||
44 | * @config |
||
45 | * @var string |
||
46 | */ |
||
47 | private static $default_sort = '"Sort"'; |
||
48 | |||
49 | /** |
||
50 | * A list of CSS classes that can be added |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | public static $allowed_css = array(); |
||
55 | |||
56 | /** |
||
57 | * @config |
||
58 | * @var array |
||
59 | */ |
||
60 | private static $summary_fields = array( |
||
61 | 'Title' |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * @config |
||
66 | * @var array |
||
67 | */ |
||
68 | private static $db = array( |
||
69 | "Name" => "Varchar", |
||
70 | "Title" => "Varchar(255)", |
||
71 | "Default" => "Varchar(255)", |
||
72 | "Sort" => "Int", |
||
73 | "Required" => "Boolean", |
||
74 | "CustomErrorMessage" => "Varchar(255)", |
||
75 | |||
76 | "CustomRules" => "Text", // @deprecated from 2.0 |
||
77 | "CustomSettings" => "Text", // @deprecated from 2.0 |
||
78 | "Migrated" => "Boolean", // set to true when migrated |
||
79 | |||
80 | "ExtraClass" => "Text", // from CustomSettings |
||
81 | "RightTitle" => "Varchar(255)", // from CustomSettings |
||
82 | "ShowOnLoad" => "Boolean(1)", // from CustomSettings |
||
83 | ); |
||
84 | |||
85 | /** |
||
86 | * @config |
||
87 | * @var array |
||
88 | */ |
||
89 | private static $has_one = array( |
||
90 | "Parent" => "UserDefinedForm", |
||
91 | ); |
||
92 | |||
93 | /** |
||
94 | * Built in extensions required |
||
95 | * |
||
96 | * @config |
||
97 | * @var array |
||
98 | */ |
||
99 | private static $extensions = array( |
||
100 | "Versioned('Stage', 'Live')" |
||
101 | ); |
||
102 | |||
103 | /** |
||
104 | * @config |
||
105 | * @var array |
||
106 | */ |
||
107 | private static $has_many = array( |
||
108 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
109 | ); |
||
110 | |||
111 | /** |
||
112 | * @var bool |
||
113 | */ |
||
114 | protected $readonly; |
||
115 | |||
116 | /** |
||
117 | * Set the visibility of an individual form field |
||
118 | * |
||
119 | * @param bool |
||
120 | */ |
||
121 | public function setReadonly($readonly = true) { |
||
124 | |||
125 | /** |
||
126 | * Returns whether this field is readonly |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | private function isReadonly() { |
||
133 | |||
134 | /** |
||
135 | * @return FieldList |
||
136 | */ |
||
137 | public function getCMSFields() { |
||
138 | $fields = new FieldList(new TabSet('Root')); |
||
139 | |||
140 | // Main tab |
||
141 | $fields->addFieldsToTab( |
||
142 | 'Root.Main', |
||
143 | array( |
||
144 | ReadonlyField::create( |
||
145 | 'Type', |
||
146 | _t('EditableFormField.TYPE', 'Type'), |
||
147 | $this->i18n_singular_name() |
||
148 | ), |
||
149 | LiteralField::create( |
||
150 | 'MergeField', |
||
151 | _t( |
||
152 | 'EditableFormField.MERGEFIELDNAME', |
||
153 | '<div class="field readonly">' . |
||
154 | '<label class="left">Merge field</label>' . |
||
155 | '<div class="middleColumn">' . |
||
156 | '<span class="readonly">$' . $this->Name . '</span>' . |
||
157 | '</div>' . |
||
158 | '</div>' |
||
159 | ) |
||
160 | ), |
||
161 | TextField::create('Title'), |
||
162 | TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')), |
||
163 | TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')), |
||
164 | SegmentField::create('Name')->setModifiers(array( |
||
165 | UnderscoreSegmentFieldModifier::create()->setDefault('FieldName'), |
||
166 | DisambiguationSegmentFieldModifier::create(), |
||
167 | ))->setPreview($this->Name) |
||
168 | ) |
||
169 | ); |
||
170 | |||
171 | // Custom settings |
||
172 | if (!empty(self::$allowed_css)) { |
||
173 | $cssList = array(); |
||
174 | foreach(self::$allowed_css as $k => $v) { |
||
175 | if (!is_array($v)) { |
||
176 | $cssList[$k]=$v; |
||
177 | } elseif ($k === $this->ClassName) { |
||
178 | $cssList = array_merge($cssList, $v); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | $fields->addFieldToTab('Root.Main', |
||
183 | DropdownField::create( |
||
184 | 'ExtraClass', |
||
185 | _t('EditableFormField.EXTRACLASS_TITLE', 'Extra Styling/Layout'), |
||
186 | $cssList |
||
187 | )->setDescription(_t( |
||
188 | 'EditableFormField.EXTRACLASS_SELECT', |
||
189 | 'Select from the list of allowed styles' |
||
190 | )) |
||
191 | ); |
||
192 | } else { |
||
193 | $fields->addFieldToTab('Root.Main', |
||
194 | TextField::create( |
||
195 | 'ExtraClass', |
||
196 | _t('EditableFormField.EXTRACLASS_Title', 'Extra CSS classes') |
||
197 | )->setDescription(_t( |
||
198 | 'EditableFormField.EXTRACLASS_MULTIPLE', |
||
199 | 'Separate each CSS class with a single space' |
||
200 | )) |
||
201 | ); |
||
202 | } |
||
203 | |||
204 | // Validation |
||
205 | $validationFields = $this->getFieldValidationOptions(); |
||
206 | if($validationFields) { |
||
207 | $fields->addFieldsToTab( |
||
208 | 'Root.Validation', |
||
209 | $this->getFieldValidationOptions() |
||
210 | ); |
||
211 | } |
||
212 | $allowedClasses = array_keys($this->getEditableFieldClasses(false)); |
||
213 | $self = $this; |
||
214 | $editableColumns = new GridFieldEditableColumns(); |
||
215 | $editableColumns->setDisplayFields(array( |
||
216 | 'Display' => '', |
||
217 | 'ConditionFieldID' => function($record, $column, $grid) use ($allowedClasses, $self) { |
||
218 | return DropdownField::create( |
||
219 | $column, |
||
220 | '', |
||
221 | EditableFormField::get() |
||
222 | ->filter(array( |
||
223 | 'ParentID' => $self->ParentID, |
||
224 | 'ClassName' => $allowedClasses |
||
225 | )) |
||
226 | ->exclude(array( |
||
227 | 'ID' => $self->ID |
||
228 | )) |
||
229 | ->map('ID', 'Title') |
||
230 | ); |
||
231 | }, |
||
232 | 'ConditionOption' => function($record, $column, $grid) { |
||
233 | $options = Config::inst()->get('EditableCustomRule', 'condition_options'); |
||
234 | return DropdownField::create($column, '', $options); |
||
235 | }, |
||
236 | 'FieldValue' => function($record, $column, $grid) { |
||
237 | return TextField::create($column); |
||
238 | }, |
||
239 | 'ParentID' => function($record, $column, $grid) use ($self) { |
||
240 | return HiddenField::create($column, '', $self->ID); |
||
241 | } |
||
242 | )); |
||
243 | |||
244 | // Custom rules |
||
245 | $customRulesConfig = GridFieldConfig::create() |
||
246 | ->addComponents( |
||
247 | $editableColumns, |
||
248 | new GridFieldButtonRow(), |
||
249 | new GridFieldToolbarHeader(), |
||
250 | new GridFieldAddNewInlineButton(), |
||
251 | new GridFieldDeleteAction() |
||
252 | ); |
||
253 | |||
254 | $fields->addFieldsToTab('Root.DisplayRules', array( |
||
255 | CheckboxField::create('ShowOnLoad') |
||
256 | ->setDescription(_t( |
||
257 | 'EditableFormField.SHOWONLOAD', |
||
258 | 'Initial visibility before processing these rules' |
||
259 | )), |
||
260 | GridField::create( |
||
261 | 'DisplayRules', |
||
262 | _t('EditableFormField.CUSTOMRULES', 'Custom Rules'), |
||
263 | $this->DisplayRules(), |
||
264 | $customRulesConfig |
||
265 | ) |
||
266 | )); |
||
267 | |||
268 | $this->extend('updateCMSFields', $fields); |
||
269 | |||
270 | return $fields; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @return void |
||
275 | */ |
||
276 | public function onBeforeWrite() { |
||
277 | parent::onBeforeWrite(); |
||
278 | |||
279 | // Set a field name. |
||
280 | if(!$this->Name) { |
||
281 | // New random name |
||
282 | $this->Name = $this->generateName(); |
||
283 | |||
284 | } elseif($this->Name === 'Field') { |
||
285 | throw new ValidationException('Field name cannot be "Field"'); |
||
286 | } |
||
287 | |||
288 | if(!$this->Sort && $this->ParentID) { |
||
289 | $parentID = $this->ParentID; |
||
290 | $this->Sort = EditableFormField::get() |
||
291 | ->filter('ParentID', $parentID) |
||
292 | ->max('Sort') + 1; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Generate a new non-conflicting Name value |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | protected function generateName() { |
||
302 | do { |
||
303 | // Generate a new random name after this class |
||
304 | $class = get_class($this); |
||
305 | $entropy = substr(sha1(uniqid()), 0, 5); |
||
306 | $name = "{$class}_{$entropy}"; |
||
307 | |||
308 | // Check if it conflicts |
||
309 | $exists = EditableFormField::get()->filter('Name', $name)->count() > 0; |
||
310 | } while($exists); |
||
311 | return $name; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
316 | * |
||
317 | * @return bool |
||
318 | */ |
||
319 | public function getSetsOwnError() { |
||
322 | |||
323 | /** |
||
324 | * Return whether a user can delete this form field |
||
325 | * based on whether they can edit the page |
||
326 | * |
||
327 | * @param Member $member |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function canDelete($member = null) { |
||
333 | |||
334 | /** |
||
335 | * Return whether a user can edit this form field |
||
336 | * based on whether they can edit the page |
||
337 | * |
||
338 | * @param Member $member |
||
339 | * @return bool |
||
340 | */ |
||
341 | public function canEdit($member = null) { |
||
350 | |||
351 | /** |
||
352 | * Return whether a user can view this form field |
||
353 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
354 | * |
||
355 | * @param Member $member |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function canView($member = null) { |
||
366 | |||
367 | /** |
||
368 | * Return whether a user can create an object of this type |
||
369 | * |
||
370 | * @param Member $member |
||
371 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
372 | * @return bool |
||
373 | */ |
||
374 | View Code Duplication | public function canCreate($member = null) { |
|
384 | |||
385 | /** |
||
386 | * Helper method to check the parent for this object |
||
387 | * |
||
388 | * @param array $args List of arguments passed to canCreate |
||
389 | * @return SiteTree Parent page instance |
||
390 | */ |
||
391 | View Code Duplication | protected function getCanCreateContext($args) { |
|
404 | |||
405 | /** |
||
406 | * Check if can publish |
||
407 | * |
||
408 | * @param Member $member |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function canPublish($member = null) { |
||
414 | |||
415 | /** |
||
416 | * Check if can unpublish |
||
417 | * |
||
418 | * @param Member $member |
||
419 | * @return bool |
||
420 | */ |
||
421 | public function canUnpublish($member = null) { |
||
424 | |||
425 | /** |
||
426 | * Publish this Form Field to the live site |
||
427 | * |
||
428 | * Wrapper for the {@link Versioned} publish function |
||
429 | */ |
||
430 | public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
||
438 | |||
439 | /** |
||
440 | * Delete this field from a given stage |
||
441 | * |
||
442 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
443 | */ |
||
444 | public function doDeleteFromStage($stage) { |
||
455 | |||
456 | /** |
||
457 | * checks wether record is new, copied from Sitetree |
||
458 | */ |
||
459 | function isNew() { |
||
466 | |||
467 | /** |
||
468 | * checks if records is changed on stage |
||
469 | * @return boolean |
||
470 | */ |
||
471 | public function getIsModifiedOnStage() { |
||
480 | |||
481 | /** |
||
482 | * @deprecated since version 4.0 |
||
483 | */ |
||
484 | public function getSettings() { |
||
488 | |||
489 | /** |
||
490 | * @deprecated since version 4.0 |
||
491 | */ |
||
492 | public function setSettings($settings = array()) { |
||
496 | |||
497 | /** |
||
498 | * @deprecated since version 4.0 |
||
499 | */ |
||
500 | public function setSetting($key, $value) { |
||
507 | |||
508 | /** |
||
509 | * Set the allowed css classes for the extraClass custom setting |
||
510 | * |
||
511 | * @param array The permissible CSS classes to add |
||
512 | */ |
||
513 | public function setAllowedCss(array $allowed) { |
||
520 | |||
521 | /** |
||
522 | * @deprecated since version 4.0 |
||
523 | */ |
||
524 | public function getSetting($setting) { |
||
535 | |||
536 | /** |
||
537 | * Get the path to the icon for this field type, relative to the site root. |
||
538 | * |
||
539 | * @return string |
||
540 | */ |
||
541 | public function getIcon() { |
||
544 | |||
545 | /** |
||
546 | * Return whether or not this field has addable options |
||
547 | * such as a dropdown field or radio set |
||
548 | * |
||
549 | * @return bool |
||
550 | */ |
||
551 | public function getHasAddableOptions() { |
||
554 | |||
555 | /** |
||
556 | * Return whether or not this field needs to show the extra |
||
557 | * options dropdown list |
||
558 | * |
||
559 | * @return bool |
||
560 | */ |
||
561 | public function showExtraOptions() { |
||
564 | |||
565 | /** |
||
566 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
567 | * |
||
568 | * @return string |
||
569 | */ |
||
570 | public function getEscapedTitle() { |
||
573 | |||
574 | /** |
||
575 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
576 | * |
||
577 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
578 | * or groups |
||
579 | * |
||
580 | * @return string |
||
581 | */ |
||
582 | public function getFieldNumber() { |
||
612 | |||
613 | public function getCMSTitle() { |
||
616 | |||
617 | /** |
||
618 | * @deprecated since version 4.0 |
||
619 | */ |
||
620 | public function getFieldName($field = false) { |
||
624 | |||
625 | /** |
||
626 | * @deprecated since version 4.0 |
||
627 | */ |
||
628 | public function getSettingName($field) { |
||
634 | |||
635 | /** |
||
636 | * Append custom validation fields to the default 'Validation' |
||
637 | * section in the editable options view |
||
638 | * |
||
639 | * @return FieldList |
||
640 | */ |
||
641 | public function getFieldValidationOptions() { |
||
652 | |||
653 | /** |
||
654 | * Return a FormField to appear on the front end. Implement on |
||
655 | * your subclass. |
||
656 | * |
||
657 | * @return FormField |
||
658 | */ |
||
659 | public function getFormField() { |
||
662 | |||
663 | /** |
||
664 | * Updates a formfield with extensions |
||
665 | * |
||
666 | * @param FormField $field |
||
667 | */ |
||
668 | public function doUpdateFormField($field) { |
||
673 | |||
674 | /** |
||
675 | * Updates a formfield with the additional metadata specified by this field |
||
676 | * |
||
677 | * @param FormField $field |
||
678 | */ |
||
679 | protected function updateFormField($field) { |
||
708 | |||
709 | /** |
||
710 | * Return the instance of the submission field class |
||
711 | * |
||
712 | * @return SubmittedFormField |
||
713 | */ |
||
714 | public function getSubmittedFormField() { |
||
717 | |||
718 | |||
719 | /** |
||
720 | * Show this form field (and its related value) in the reports and in emails. |
||
721 | * |
||
722 | * @return bool |
||
723 | */ |
||
724 | public function showInReports() { |
||
727 | |||
728 | /** |
||
729 | * Return the error message for this field. Either uses the custom |
||
730 | * one (if provided) or the default SilverStripe message |
||
731 | * |
||
732 | * @return Varchar |
||
733 | */ |
||
734 | public function getErrorMessage() { |
||
743 | |||
744 | /** |
||
745 | * Validate the field taking into account its custom rules. |
||
746 | * |
||
747 | * @param Array $data |
||
748 | * @param UserForm $form |
||
749 | * |
||
750 | * @return boolean |
||
751 | */ |
||
752 | public function validateField($data, $form) { |
||
771 | |||
772 | /** |
||
773 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
774 | * to the field proper |
||
775 | * |
||
776 | * @param array $data Unserialised data |
||
777 | */ |
||
778 | public function migrateSettings($data) { |
||
792 | |||
793 | /** |
||
794 | * Get the formfield to use when editing this inline in gridfield |
||
795 | * |
||
796 | * @param string $column name of column |
||
797 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
798 | * @return FormField |
||
799 | */ |
||
800 | public function getInlineClassnameField($column, $fieldClasses) { |
||
803 | |||
804 | /** |
||
805 | * Get the formfield to use when editing the title inline |
||
806 | * |
||
807 | * @param string $column |
||
808 | * @return FormField |
||
809 | */ |
||
810 | public function getInlineTitleField($column) { |
||
815 | |||
816 | /** |
||
817 | * Get the JS expression for selecting the holder for this field |
||
818 | * |
||
819 | * @return string |
||
820 | */ |
||
821 | public function getSelectorHolder() { |
||
824 | |||
825 | /** |
||
826 | * Gets the JS expression for selecting the value for this field |
||
827 | * |
||
828 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
829 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
830 | */ |
||
831 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) { |
||
834 | |||
835 | |||
836 | /** |
||
837 | * Get the list of classes that can be selected and used as data-values |
||
838 | * |
||
839 | * @param $includeLiterals Set to false to exclude non-data fields |
||
840 | * @return array |
||
841 | */ |
||
842 | public function getEditableFieldClasses($includeLiterals = true) { |
||
869 | |||
870 | /** |
||
871 | * @return EditableFormFieldValidator |
||
872 | */ |
||
873 | public function getCMSValidator() { |
||
877 | } |
||
878 |
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.