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 |
||
| 21 | class SubmittedForm extends DataObject |
||
| 22 | { |
||
| 23 | private static $has_one = [ |
||
| 24 | 'SubmittedBy' => Member::class, |
||
| 25 | 'Parent' => UserDefinedForm::class, |
||
| 26 | ]; |
||
| 27 | |||
| 28 | private static $has_many = [ |
||
| 29 | 'Values' => SubmittedFormField::class |
||
| 30 | ]; |
||
| 31 | |||
| 32 | private static $cascade_deletes = [ |
||
| 33 | 'Values', |
||
| 34 | ]; |
||
| 35 | |||
| 36 | private static $summary_fields = [ |
||
| 37 | 'ID', |
||
| 38 | 'Created' |
||
| 39 | ]; |
||
| 40 | |||
| 41 | private static $table_name = 'SubmittedForm'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns the value of a relation or, in the case of this form, the value |
||
| 45 | * of a given child {@link SubmittedFormField} |
||
| 46 | * |
||
| 47 | * @param string |
||
| 48 | * |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | public function relField($fieldName) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return FieldList |
||
| 71 | */ |
||
| 72 | public function getCMSFields() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param Member |
||
| 121 | * |
||
| 122 | * @return boolean |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function canCreate($member = null, $context = []) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param Member |
||
| 135 | * |
||
| 136 | * @return boolean |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function canView($member = null) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param Member |
||
| 149 | * |
||
| 150 | * @return boolean |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function canEdit($member = null) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param Member |
||
| 163 | * |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | View Code Duplication | public function canDelete($member = null) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Before we delete this form make sure we delete all the |
||
| 177 | * field values so that we don't leave old data round |
||
| 178 | * |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | protected function onBeforeDelete() |
||
| 191 | } |
||
| 192 |