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 |
||
| 22 | class EditableTextField extends EditableFormField |
||
| 23 | { |
||
| 24 | private static $singular_name = 'Text Field'; |
||
| 25 | |||
| 26 | private static $plural_name = 'Text Fields'; |
||
| 27 | |||
| 28 | private static $has_placeholder = true; |
||
| 29 | |||
| 30 | /** @skipUpgrade */ |
||
| 31 | private static $autocomplete_options = [ |
||
| 32 | 'off' => 'Off', |
||
| 33 | 'on' => 'On', |
||
| 34 | 'name' => 'Full name', |
||
| 35 | 'honorific-prefix' => 'Prefix or title', |
||
| 36 | 'given-name' => 'First name', |
||
| 37 | 'additional-name' => 'Additional name', |
||
| 38 | 'family-name' => 'Family name', |
||
| 39 | 'honorific-suffix' => 'Suffix (e.g Jr.)', |
||
| 40 | 'nickname' => 'Nickname', |
||
| 41 | 'email' => 'Email', |
||
| 42 | 'organization-title' => 'Job title', |
||
| 43 | 'organization' => 'Organization', |
||
| 44 | 'street-address' => 'Street address', |
||
| 45 | 'address-line1' => 'Address line 1', |
||
| 46 | 'address-line2' => 'Address line 2', |
||
| 47 | 'address-line3' => 'Address line 3', |
||
| 48 | 'address-level1' => 'Address level 1', |
||
| 49 | 'address-level2' => 'Address level 2', |
||
| 50 | 'address-level3' => 'Address level 3', |
||
| 51 | 'address-level4' => 'Address level 4', |
||
| 52 | 'country' => 'Country', |
||
| 53 | 'country-name' => 'Country name', |
||
| 54 | 'postal-code' => 'Postal code', |
||
| 55 | 'bday' => 'Birthday', |
||
| 56 | 'sex' => 'Gender identity', |
||
| 57 | 'tel' => 'Telephone number', |
||
| 58 | 'url' => 'Home page' |
||
| 59 | ]; |
||
| 60 | |||
| 61 | protected $jsEventHandler = 'keyup'; |
||
| 62 | |||
| 63 | private static $db = [ |
||
| 64 | 'MinLength' => 'Int', |
||
| 65 | 'MaxLength' => 'Int', |
||
| 66 | 'Rows' => 'Int(1)', |
||
| 67 | 'Autocomplete' => 'Varchar(255)' |
||
| 68 | ]; |
||
| 69 | |||
| 70 | private static $defaults = [ |
||
| 71 | 'Rows' => 1 |
||
| 72 | ]; |
||
| 73 | |||
| 74 | private static $table_name = 'EditableTextField'; |
||
| 75 | |||
| 76 | public function getCMSFields() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return FieldList |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function getFieldValidationOptions() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @return TextareaField|TextField |
||
| 127 | */ |
||
| 128 | public function getFormField() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Updates a formfield with the additional metadata specified by this field |
||
| 148 | * |
||
| 149 | * @param FormField $field |
||
| 150 | */ |
||
| 151 | protected function updateFormField($field) |
||
| 170 | } |
||
| 171 |