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:
| 1 | <?php |
||
| 16 | class EditableRadioField extends EditableMultipleOptionField |
||
| 17 | { |
||
| 18 | private static $singular_name = 'Radio Group'; |
||
| 19 | |||
| 20 | private static $plural_name = 'Radio Groups'; |
||
| 21 | |||
| 22 | private static $table_name = 'EditableRadioField'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @return FieldList |
||
| 26 | */ |
||
| 27 | public function getCMSFields() |
||
| 35 | |||
| 36 | View Code Duplication | public function getFormField() |
|
| 37 | { |
||
| 38 | $field = OptionsetField::create($this->Name, $this->EscapedTitle, $this->getOptionsMap()) |
||
| 39 | ->setFieldHolderTemplate(EditableMultipleOptionField::class . '_holder') |
||
| 40 | ->setTemplate('SilverStripe\\UserForms\\FormField\\UserFormsOptionSetField'); |
||
| 41 | |||
| 42 | // Set default item |
||
| 43 | $defaultOption = $this->getDefaultOptions()->first(); |
||
| 44 | if ($defaultOption) { |
||
| 45 | $field->setValue($defaultOption->Value); |
||
| 46 | } |
||
| 47 | $this->doUpdateFormField($field); |
||
| 48 | return $field; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
| 57 | |||
| 58 | public function isRadioField() |
||
| 62 | } |
||
| 63 |