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 | * @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() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Publishing Versioning support. |
||
| 101 | * |
||
| 102 | * When publishing it needs to handle copying across / publishing |
||
| 103 | * each of the individual field options |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Unpublishing Versioning support |
||
| 128 | * |
||
| 129 | * When unpublishing the field it has to remove all options attached |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | public function doDeleteFromStage($stage) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Deletes all the options attached to this field before deleting the |
||
| 147 | * field. Keeps stray options from floating around |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function delete() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Duplicate a pages content. We need to make sure all the fields attached |
||
| 166 | * to that page go with it |
||
| 167 | * |
||
| 168 | * @return DataObject |
||
| 169 | */ |
||
| 170 | public function duplicate($doWrite = true) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Return whether or not this field has addable options such as a |
||
| 186 | * {@link EditableDropdownField} or {@link EditableRadioField} |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function getHasAddableOptions() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Gets map of field options suitable for use in a form |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | protected function getOptionsMap() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns all default options |
||
| 212 | * |
||
| 213 | * @return SS_List |
||
| 214 | */ |
||
| 215 | protected function getDefaultOptions() |
||
| 219 | } |
||
| 220 |
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: