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 | /** |
||
91 | * @config |
||
92 | * @var array |
||
93 | */ |
||
94 | private static $has_one = array( |
||
95 | "Parent" => "UserDefinedForm", |
||
96 | ); |
||
97 | |||
98 | /** |
||
99 | * Built in extensions required |
||
100 | * |
||
101 | * @config |
||
102 | * @var array |
||
103 | */ |
||
104 | private static $extensions = array( |
||
105 | "Versioned('Stage', 'Live')" |
||
106 | ); |
||
107 | |||
108 | /** |
||
109 | * @config |
||
110 | * @var array |
||
111 | */ |
||
112 | private static $has_many = array( |
||
113 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
114 | ); |
||
115 | |||
116 | /** |
||
117 | * @var bool |
||
118 | */ |
||
119 | protected $readonly; |
||
120 | |||
121 | /** |
||
122 | * Set the visibility of an individual form field |
||
123 | * |
||
124 | * @param bool |
||
125 | */ |
||
126 | public function setReadonly($readonly = true) { |
||
129 | |||
130 | /** |
||
131 | * Returns whether this field is readonly |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | private function isReadonly() { |
||
138 | |||
139 | /** |
||
140 | * @return FieldList |
||
141 | */ |
||
142 | public function getCMSFields() { |
||
143 | $fields = new FieldList(new TabSet('Root')); |
||
144 | |||
145 | // Main tab |
||
146 | $fields->addFieldsToTab( |
||
147 | 'Root.Main', |
||
148 | array( |
||
149 | ReadonlyField::create( |
||
150 | 'Type', |
||
151 | _t('EditableFormField.TYPE', 'Type'), |
||
152 | $this->i18n_singular_name() |
||
153 | ), |
||
154 | LiteralField::create( |
||
155 | 'MergeField', |
||
156 | _t( |
||
157 | 'EditableFormField.MERGEFIELDNAME', |
||
158 | '<div class="field readonly">' . |
||
159 | '<label class="left">Merge field</label>' . |
||
160 | '<div class="middleColumn">' . |
||
161 | '<span class="readonly">$' . $this->Name . '</span>' . |
||
162 | '</div>' . |
||
163 | '</div>' |
||
164 | ) |
||
165 | ), |
||
166 | TextField::create('Title'), |
||
167 | TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')), |
||
168 | TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')), |
||
169 | SegmentField::create('Name')->setModifiers(array( |
||
170 | UnderscoreSegmentFieldModifier::create()->setDefault('FieldName'), |
||
171 | DisambiguationSegmentFieldModifier::create(), |
||
172 | ))->setPreview($this->Name) |
||
173 | ) |
||
174 | ); |
||
175 | |||
176 | // Custom settings |
||
177 | if (!empty(self::$allowed_css)) { |
||
178 | $cssList = array(); |
||
179 | foreach(self::$allowed_css as $k => $v) { |
||
180 | if (!is_array($v)) { |
||
181 | $cssList[$k]=$v; |
||
182 | } elseif ($k === $this->ClassName) { |
||
183 | $cssList = array_merge($cssList, $v); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | $fields->addFieldToTab('Root.Main', |
||
188 | DropdownField::create( |
||
189 | 'ExtraClass', |
||
190 | _t('EditableFormField.EXTRACLASS_TITLE', 'Extra Styling/Layout'), |
||
191 | $cssList |
||
192 | )->setDescription(_t( |
||
193 | 'EditableFormField.EXTRACLASS_SELECT', |
||
194 | 'Select from the list of allowed styles' |
||
195 | )) |
||
196 | ); |
||
197 | } else { |
||
198 | $fields->addFieldToTab('Root.Main', |
||
199 | TextField::create( |
||
200 | 'ExtraClass', |
||
201 | _t('EditableFormField.EXTRACLASS_Title', 'Extra CSS classes') |
||
202 | )->setDescription(_t( |
||
203 | 'EditableFormField.EXTRACLASS_MULTIPLE', |
||
204 | 'Separate each CSS class with a single space' |
||
205 | )) |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | // Validation |
||
210 | $validationFields = $this->getFieldValidationOptions(); |
||
211 | if($validationFields && $validationFields->count()) { |
||
212 | $fields->addFieldsToTab('Root.Validation', $validationFields); |
||
213 | } |
||
214 | |||
215 | // Add display rule fields |
||
216 | $displayFields = $this->getDisplayRuleFields(); |
||
217 | if($displayFields && $displayFields->count()) { |
||
218 | $fields->addFieldsToTab('Root.DisplayRules', $displayFields); |
||
219 | } |
||
220 | |||
221 | $this->extend('updateCMSFields', $fields); |
||
222 | |||
223 | return $fields; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Return fields to display on the 'Display Rules' tab |
||
228 | * |
||
229 | * @return FieldList |
||
230 | */ |
||
231 | protected function getDisplayRuleFields() { |
||
232 | // Check display rules |
||
233 | if($this->Required) { |
||
234 | return new FieldList( |
||
235 | LabelField::create(_t( |
||
236 | 'EditableFormField.DISPLAY_RULES_DISABLED', |
||
237 | 'Display rules are not enabled for required fields. ' . |
||
238 | 'Please uncheck "Is this field Required?" under "Validation" to re-enable.' |
||
239 | ))->addExtraClass('message warning') |
||
240 | ); |
||
241 | } |
||
242 | $self = $this; |
||
243 | $allowedClasses = array_keys($this->getEditableFieldClasses(false)); |
||
244 | $editableColumns = new GridFieldEditableColumns(); |
||
245 | $editableColumns->setDisplayFields(array( |
||
246 | 'Display' => '', |
||
247 | 'ConditionFieldID' => function($record, $column, $grid) use ($allowedClasses, $self) { |
||
248 | return DropdownField::create( |
||
249 | $column, |
||
250 | '', |
||
251 | EditableFormField::get() |
||
252 | ->filter(array( |
||
253 | 'ParentID' => $self->ParentID, |
||
254 | 'ClassName' => $allowedClasses |
||
255 | )) |
||
256 | ->exclude(array( |
||
257 | 'ID' => $self->ID |
||
258 | )) |
||
259 | ->map('ID', 'Title') |
||
260 | ); |
||
261 | }, |
||
262 | 'ConditionOption' => function($record, $column, $grid) { |
||
263 | $options = Config::inst()->get('EditableCustomRule', 'condition_options'); |
||
264 | return DropdownField::create($column, '', $options); |
||
265 | }, |
||
266 | 'FieldValue' => function($record, $column, $grid) { |
||
267 | return TextField::create($column); |
||
268 | }, |
||
269 | 'ParentID' => function($record, $column, $grid) use ($self) { |
||
270 | return HiddenField::create($column, '', $self->ID); |
||
271 | } |
||
272 | )); |
||
273 | |||
274 | // Custom rules |
||
275 | $customRulesConfig = GridFieldConfig::create() |
||
276 | ->addComponents( |
||
277 | $editableColumns, |
||
278 | new GridFieldButtonRow(), |
||
279 | new GridFieldToolbarHeader(), |
||
280 | new GridFieldAddNewInlineButton(), |
||
281 | new GridFieldDeleteAction() |
||
282 | ); |
||
283 | |||
284 | return new FieldList( |
||
285 | CheckboxField::create('ShowOnLoad') |
||
286 | ->setDescription(_t( |
||
287 | 'EditableFormField.SHOWONLOAD', |
||
288 | 'Initial visibility before processing these rules' |
||
289 | )), |
||
290 | GridField::create( |
||
291 | 'DisplayRules', |
||
292 | _t('EditableFormField.CUSTOMRULES', 'Custom Rules'), |
||
293 | $this->DisplayRules(), |
||
294 | $customRulesConfig |
||
295 | ) |
||
296 | ); |
||
297 | } |
||
298 | |||
299 | public function onBeforeWrite() { |
||
300 | parent::onBeforeWrite(); |
||
301 | |||
302 | // Set a field name. |
||
303 | if(!$this->Name) { |
||
304 | // New random name |
||
305 | $this->Name = $this->generateName(); |
||
306 | |||
307 | } elseif($this->Name === 'Field') { |
||
308 | throw new ValidationException('Field name cannot be "Field"'); |
||
309 | } |
||
310 | |||
311 | if(!$this->Sort && $this->ParentID) { |
||
312 | $parentID = $this->ParentID; |
||
313 | $this->Sort = EditableFormField::get() |
||
314 | ->filter('ParentID', $parentID) |
||
315 | ->max('Sort') + 1; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Generate a new non-conflicting Name value |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | protected function generateName() { |
||
325 | do { |
||
326 | // Generate a new random name after this class |
||
327 | $class = get_class($this); |
||
328 | $entropy = substr(sha1(uniqid()), 0, 5); |
||
329 | $name = "{$class}_{$entropy}"; |
||
330 | |||
331 | // Check if it conflicts |
||
332 | $exists = EditableFormField::get()->filter('Name', $name)->count() > 0; |
||
333 | } while($exists); |
||
334 | return $name; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | public function getSetsOwnError() { |
||
345 | |||
346 | /** |
||
347 | * Return whether a user can delete this form field |
||
348 | * based on whether they can edit the page |
||
349 | * |
||
350 | * @param Member $member |
||
351 | * @return bool |
||
352 | */ |
||
353 | public function canDelete($member = null) { |
||
356 | |||
357 | /** |
||
358 | * Return whether a user can edit this form field |
||
359 | * based on whether they can edit the page |
||
360 | * |
||
361 | * @param Member $member |
||
362 | * @return bool |
||
363 | */ |
||
364 | public function canEdit($member = null) { |
||
373 | |||
374 | /** |
||
375 | * Return whether a user can view this form field |
||
376 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
377 | * |
||
378 | * @param Member $member |
||
379 | * @return bool |
||
380 | */ |
||
381 | public function canView($member = null) { |
||
389 | |||
390 | /** |
||
391 | * Return whether a user can create an object of this type |
||
392 | * |
||
393 | * @param Member $member |
||
394 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
395 | * @return bool |
||
396 | */ |
||
397 | View Code Duplication | public function canCreate($member = null) { |
|
407 | |||
408 | /** |
||
409 | * Helper method to check the parent for this object |
||
410 | * |
||
411 | * @param array $args List of arguments passed to canCreate |
||
412 | * @return SiteTree Parent page instance |
||
413 | */ |
||
414 | View Code Duplication | protected function getCanCreateContext($args) { |
|
427 | |||
428 | /** |
||
429 | * Check if can publish |
||
430 | * |
||
431 | * @param Member $member |
||
432 | * @return bool |
||
433 | */ |
||
434 | public function canPublish($member = null) { |
||
437 | |||
438 | /** |
||
439 | * Check if can unpublish |
||
440 | * |
||
441 | * @param Member $member |
||
442 | * @return bool |
||
443 | */ |
||
444 | public function canUnpublish($member = null) { |
||
447 | |||
448 | /** |
||
449 | * Publish this Form Field to the live site |
||
450 | * |
||
451 | * Wrapper for the {@link Versioned} publish function |
||
452 | */ |
||
453 | public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
||
461 | |||
462 | /** |
||
463 | * Delete this field from a given stage |
||
464 | * |
||
465 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
466 | */ |
||
467 | public function doDeleteFromStage($stage) { |
||
478 | |||
479 | /** |
||
480 | * checks wether record is new, copied from Sitetree |
||
481 | */ |
||
482 | function isNew() { |
||
489 | |||
490 | /** |
||
491 | * checks if records is changed on stage |
||
492 | * @return boolean |
||
493 | */ |
||
494 | public function getIsModifiedOnStage() { |
||
503 | |||
504 | /** |
||
505 | * @deprecated since version 4.0 |
||
506 | */ |
||
507 | public function getSettings() { |
||
511 | |||
512 | /** |
||
513 | * @deprecated since version 4.0 |
||
514 | */ |
||
515 | public function setSettings($settings = array()) { |
||
519 | |||
520 | /** |
||
521 | * @deprecated since version 4.0 |
||
522 | */ |
||
523 | public function setSetting($key, $value) { |
||
530 | |||
531 | /** |
||
532 | * Set the allowed css classes for the extraClass custom setting |
||
533 | * |
||
534 | * @param array The permissible CSS classes to add |
||
535 | */ |
||
536 | public function setAllowedCss(array $allowed) { |
||
543 | |||
544 | /** |
||
545 | * @deprecated since version 4.0 |
||
546 | */ |
||
547 | public function getSetting($setting) { |
||
558 | |||
559 | /** |
||
560 | * Get the path to the icon for this field type, relative to the site root. |
||
561 | * |
||
562 | * @return string |
||
563 | */ |
||
564 | public function getIcon() { |
||
567 | |||
568 | /** |
||
569 | * Return whether or not this field has addable options |
||
570 | * such as a dropdown field or radio set |
||
571 | * |
||
572 | * @return bool |
||
573 | */ |
||
574 | public function getHasAddableOptions() { |
||
577 | |||
578 | /** |
||
579 | * Return whether or not this field needs to show the extra |
||
580 | * options dropdown list |
||
581 | * |
||
582 | * @return bool |
||
583 | */ |
||
584 | public function showExtraOptions() { |
||
587 | |||
588 | /** |
||
589 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
590 | * |
||
591 | * @return string |
||
592 | */ |
||
593 | public function getEscapedTitle() { |
||
596 | |||
597 | /** |
||
598 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
599 | * |
||
600 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
601 | * or groups |
||
602 | * |
||
603 | * @return string |
||
604 | */ |
||
605 | public function getFieldNumber() { |
||
635 | |||
636 | public function getCMSTitle() { |
||
639 | |||
640 | /** |
||
641 | * @deprecated since version 4.0 |
||
642 | */ |
||
643 | public function getFieldName($field = false) { |
||
647 | |||
648 | /** |
||
649 | * @deprecated since version 4.0 |
||
650 | */ |
||
651 | public function getSettingName($field) { |
||
657 | |||
658 | /** |
||
659 | * Append custom validation fields to the default 'Validation' |
||
660 | * section in the editable options view |
||
661 | * |
||
662 | * @return FieldList |
||
663 | */ |
||
664 | public function getFieldValidationOptions() { |
||
675 | |||
676 | /** |
||
677 | * Return a FormField to appear on the front end. Implement on |
||
678 | * your subclass. |
||
679 | * |
||
680 | * @return FormField |
||
681 | */ |
||
682 | public function getFormField() { |
||
685 | |||
686 | /** |
||
687 | * Updates a formfield with extensions |
||
688 | * |
||
689 | * @param FormField $field |
||
690 | */ |
||
691 | public function doUpdateFormField($field) { |
||
696 | |||
697 | /** |
||
698 | * Updates a formfield with the additional metadata specified by this field |
||
699 | * |
||
700 | * @param FormField $field |
||
701 | */ |
||
702 | protected function updateFormField($field) { |
||
731 | |||
732 | /** |
||
733 | * Return the instance of the submission field class |
||
734 | * |
||
735 | * @return SubmittedFormField |
||
736 | */ |
||
737 | public function getSubmittedFormField() { |
||
740 | |||
741 | |||
742 | /** |
||
743 | * Show this form field (and its related value) in the reports and in emails. |
||
744 | * |
||
745 | * @return bool |
||
746 | */ |
||
747 | public function showInReports() { |
||
750 | |||
751 | /** |
||
752 | * Return the error message for this field. Either uses the custom |
||
753 | * one (if provided) or the default SilverStripe message |
||
754 | * |
||
755 | * @return Varchar |
||
756 | */ |
||
757 | public function getErrorMessage() { |
||
766 | |||
767 | /** |
||
768 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
769 | * to the field proper |
||
770 | * |
||
771 | * @param array $data Unserialised data |
||
772 | */ |
||
773 | public function migrateSettings($data) { |
||
787 | |||
788 | /** |
||
789 | * Get the formfield to use when editing this inline in gridfield |
||
790 | * |
||
791 | * @param string $column name of column |
||
792 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
793 | * @return FormField |
||
794 | */ |
||
795 | public function getInlineClassnameField($column, $fieldClasses) { |
||
798 | |||
799 | /** |
||
800 | * Get the formfield to use when editing the title inline |
||
801 | * |
||
802 | * @param string $column |
||
803 | * @return FormField |
||
804 | */ |
||
805 | public function getInlineTitleField($column) { |
||
810 | |||
811 | /** |
||
812 | * Get the JS expression for selecting the holder for this field |
||
813 | * |
||
814 | * @return string |
||
815 | */ |
||
816 | public function getSelectorHolder() { |
||
819 | |||
820 | /** |
||
821 | * Gets the JS expression for selecting the value for this field |
||
822 | * |
||
823 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
824 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
825 | */ |
||
826 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) { |
||
829 | |||
830 | |||
831 | /** |
||
832 | * Get the list of classes that can be selected and used as data-values |
||
833 | * |
||
834 | * @param $includeLiterals Set to false to exclude non-data fields |
||
835 | * @return array |
||
836 | */ |
||
837 | public function getEditableFieldClasses($includeLiterals = true) { |
||
864 | |||
865 | /** |
||
866 | * @return EditableFormFieldValidator |
||
867 | */ |
||
868 | public function getCMSValidator() { |
||
872 | |||
873 | /** |
||
874 | * Determine effective display rules for this field. |
||
875 | * |
||
876 | * @return SS_List |
||
877 | */ |
||
878 | public function EffectiveDisplayRules() { |
||
884 | |||
885 | } |
||
886 |
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.