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 |
||
| 8 | class SubmittedForm extends DataObject |
||
|
|
|||
| 9 | { |
||
| 10 | |||
| 11 | private static $has_one = array( |
||
| 12 | "SubmittedBy" => "Member", |
||
| 13 | "Parent" => "UserDefinedForm", |
||
| 14 | ); |
||
| 15 | |||
| 16 | private static $has_many = array( |
||
| 17 | "Values" => "SubmittedFormField" |
||
| 18 | ); |
||
| 19 | |||
| 20 | private static $summary_fields = array( |
||
| 21 | 'ID', |
||
| 22 | 'Created' |
||
| 23 | ); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Returns the value of a relation or, in the case of this form, the value |
||
| 27 | * of a given child {@link SubmittedFormField} |
||
| 28 | * |
||
| 29 | * @param string |
||
| 30 | * |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | public function relField($fieldName) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return FieldList |
||
| 53 | */ |
||
| 54 | public function getCMSFields() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param Member |
||
| 105 | * |
||
| 106 | * @return boolean |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function canCreate($member = null) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param Member |
||
| 119 | * |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function canView($member = null) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @param Member |
||
| 133 | * |
||
| 134 | * @return boolean |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function canEdit($member = null) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param Member |
||
| 147 | * |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function canDelete($member = null) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Before we delete this form make sure we delete all the |
||
| 161 | * field values so that we don't leave old data round |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | protected function onBeforeDelete() |
||
| 175 | } |
||
| 176 |
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.