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 | * Define this field as abstract (not inherited) |
||
| 20 | * |
||
| 21 | * @config |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | private static $abstract = true; |
||
| 25 | |||
| 26 | private static $has_many = array( |
||
| 27 | "Options" => "EditableOption" |
||
| 28 | ); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return FieldList |
||
| 32 | */ |
||
| 33 | public function getCMSFields() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Publishing Versioning support. |
||
| 84 | * |
||
| 85 | * When publishing it needs to handle copying across / publishing |
||
| 86 | * each of the individual field options |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Unpublishing Versioning support |
||
| 110 | * |
||
| 111 | * When unpublishing the field it has to remove all options attached |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function doDeleteFromStage($stage) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Deletes all the options attached to this field before deleting the |
||
| 128 | * field. Keeps stray options from floating around |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | public function delete() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Duplicate a pages content. We need to make sure all the fields attached |
||
| 146 | * to that page go with it |
||
| 147 | * |
||
| 148 | * @return DataObject |
||
| 149 | */ |
||
| 150 | public function duplicate($doWrite = true) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Return whether or not this field has addable options such as a |
||
| 165 | * {@link EditableDropdownField} or {@link EditableRadioField} |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function getHasAddableOptions() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Gets map of field options suitable for use in a form |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | protected function getOptionsMap() { |
||
| 179 | $optionSet = $this->Options(); |
||
| 180 | $optionMap = $optionSet->map('Value', 'Title'); |
||
| 181 | if($optionMap instanceof SS_Map) { |
||
| 182 | return $optionMap->toArray(); |
||
| 183 | } |
||
| 184 | return $optionMap; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns all default options |
||
| 189 | * |
||
| 190 | * @return SS_List |
||
| 191 | */ |
||
| 192 | protected function getDefaultOptions() { |
||
| 195 | } |
||
| 196 |
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.