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 |
||
| 27 | class EditableMultipleOptionField extends EditableFormField |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Define this field as abstract (not inherited) |
||
| 32 | * |
||
| 33 | * @config |
||
| 34 | 3 | * @var bool |
|
| 35 | */ |
||
| 36 | private static $abstract = true; |
||
| 37 | |||
| 38 | private static $has_many = array( |
||
| 39 | "Options" => "EditableOption" |
||
| 40 | ); |
||
| 41 | |||
| 42 | private static $owns = [ |
||
| 43 | "Options", |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return FieldList |
||
| 48 | */ |
||
| 49 | public function getCMSFields() |
||
| 100 | |||
| 101 | 1 | /** |
|
| 102 | 1 | * Publishing Versioning support. |
|
| 103 | * |
||
| 104 | 1 | * When publishing it needs to handle copying across / publishing |
|
| 105 | 1 | * each of the individual field options |
|
| 106 | 1 | * |
|
| 107 | 1 | * @return void |
|
| 108 | 1 | */ |
|
| 109 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
||
| 127 | 1 | ||
| 128 | /** |
||
| 129 | 1 | * Unpublishing Versioning support |
|
| 130 | 1 | * |
|
| 131 | * When unpublishing the field it has to remove all options attached |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function doDeleteFromStage($stage) |
||
| 146 | 1 | ||
| 147 | /** |
||
| 148 | 1 | * Deletes all the options attached to this field before deleting the |
|
| 149 | 1 | * field. Keeps stray options from floating around |
|
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function delete() |
||
| 165 | 1 | ||
| 166 | 1 | /** |
|
| 167 | * Duplicate a pages content. We need to make sure all the fields attached |
||
| 168 | 1 | * to that page go with it |
|
| 169 | * |
||
| 170 | * @return DataObject |
||
| 171 | */ |
||
| 172 | public function duplicate($doWrite = true) |
||
| 185 | |||
| 186 | /** |
||
| 187 | 3 | * Return whether or not this field has addable options such as a |
|
| 188 | * {@link EditableDropdownField} or {@link EditableRadioField} |
||
| 189 | 3 | * |
|
| 190 | 3 | * @return bool |
|
| 191 | 3 | */ |
|
| 192 | 3 | public function getHasAddableOptions() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Gets map of field options suitable for use in a form |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | 3 | protected function getOptionsMap() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Returns all default options |
||
| 214 | * |
||
| 215 | * @return SS_List |
||
| 216 | */ |
||
| 217 | protected function getDefaultOptions() |
||
| 221 | } |
||
| 222 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: