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 EditableMultipleOptionField extends EditableFormField |
||
|
|
|||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Define this field as abstract (not inherited) |
||
| 21 | * |
||
| 22 | * @config |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | private static $abstract = true; |
||
| 26 | |||
| 27 | private static $has_many = array( |
||
| 28 | "Options" => "EditableOption" |
||
| 29 | ); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return FieldList |
||
| 33 | */ |
||
| 34 | 3 | public function getCMSFields() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Publishing Versioning support. |
||
| 88 | * |
||
| 89 | * When publishing it needs to handle copying across / publishing |
||
| 90 | * each of the individual field options |
||
| 91 | * |
||
| 92 | * @param string $fromStage |
||
| 93 | * @param string $toStage |
||
| 94 | 1 | * @param bool $createNewVersion |
|
| 95 | */ |
||
| 96 | 1 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
|
| 101 | 1 | ||
| 102 | 1 | ||
| 103 | /** |
||
| 104 | 1 | * Publish list options |
|
| 105 | 1 | * |
|
| 106 | 1 | * @param string $fromStage |
|
| 107 | 1 | * @param string $toStage |
|
| 108 | 1 | * @param bool $createNewVersion |
|
| 109 | */ |
||
| 110 | 1 | View Code Duplication | protected function publishOptions($fromStage, $toStage, $createNewVersion) |
| 132 | |||
| 133 | /** |
||
| 134 | * Unpublishing Versioning support |
||
| 135 | * |
||
| 136 | * When unpublishing the field it has to remove all options attached |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function doDeleteFromStage($stage) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Deletes all the options attached to this field before deleting the |
||
| 154 | * field. Keeps stray options from floating around |
||
| 155 | * |
||
| 156 | * @return void |
||
| 157 | 1 | */ |
|
| 158 | public function delete() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Duplicate a pages content. We need to make sure all the fields attached |
||
| 173 | * to that page go with it |
||
| 174 | * |
||
| 175 | * @return DataObject |
||
| 176 | */ |
||
| 177 | public function duplicate($doWrite = true) |
||
| 190 | 3 | ||
| 191 | 3 | /** |
|
| 192 | 3 | * Return whether or not this field has addable options such as a |
|
| 193 | * {@link EditableDropdownField} or {@link EditableRadioField} |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | public function getHasAddableOptions() |
||
| 201 | |||
| 202 | 3 | /** |
|
| 203 | * Gets map of field options suitable for use in a form |
||
| 204 | 3 | * |
|
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | 1 | protected function getOptionsMap() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Returns all default options |
||
| 219 | * |
||
| 220 | * @return SS_List |
||
| 221 | */ |
||
| 222 | protected function getDefaultOptions() |
||
| 226 | } |
||
| 227 |
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.