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 |
||
| 4 | class UpdateNote extends DataObject |
||
|
|
|||
| 5 | { |
||
| 6 | private static $fields_to_remove_in_the_cms_fields = []; |
||
| 7 | |||
| 8 | private static $singular_name = 'Update Note'; |
||
| 9 | |||
| 10 | public function i18n_singular_name() |
||
| 14 | |||
| 15 | private static $plural_name = 'Update Notes'; |
||
| 16 | |||
| 17 | public function i18n_plural_name() |
||
| 21 | |||
| 22 | private static $db = array( |
||
| 23 | 'Note' => 'Varchar', |
||
| 24 | 'FutureReminderDate' => 'Date', |
||
| 25 | 'FutureReminderNote' => 'Varchar', |
||
| 26 | 'FutureReminderCompleted' => 'Boolean', |
||
| 27 | 'UpdateNoteRecordID' => 'Int', |
||
| 28 | 'UpdateNoteRecordClass' => 'Varchar(100)' |
||
| 29 | ); |
||
| 30 | |||
| 31 | private static $has_one = array( |
||
| 32 | 'UpdatedBy' => 'Member', |
||
| 33 | 'UpdateNoteRecord' => 'DataObject' |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $casting = array( |
||
| 37 | 'Title' => 'Varchar' |
||
| 38 | ); |
||
| 39 | |||
| 40 | private static $indexes = array( |
||
| 41 | 'UpdateNoteRecordID' => true, |
||
| 42 | 'UpdateNoteRecordClass' => true, |
||
| 43 | 'FutureReminderNote' => true, |
||
| 44 | 'FutureReminderDate' => true |
||
| 45 | ); |
||
| 46 | |||
| 47 | private static $default_sort = array( |
||
| 48 | 'IF("ClassName" = \'UpdateNoteToBeCompleted\' AND "FutureReminderCompleted" = 0, 0, 1)' => 'ASC', |
||
| 49 | 'FutureReminderDate' => 'ASC', |
||
| 50 | 'Created' => 'DESC' |
||
| 51 | ); |
||
| 52 | |||
| 53 | // private static $required_fields = array(); |
||
| 54 | |||
| 55 | private static $summary_fields = array( |
||
| 56 | 'Created.Nice' => 'When', |
||
| 57 | 'LastEdited.Nice' => 'Last Edited', |
||
| 58 | 'UpdateNoteRecord.Title' => 'What', |
||
| 59 | 'Note' => 'Note', |
||
| 60 | 'UpdatedBy.Email' => 'Editor', |
||
| 61 | 'FutureReminderDate.Nice' => 'Future Reminder', |
||
| 62 | 'FutureReminderNote' => 'Reminder Note' |
||
| 63 | ); |
||
| 64 | |||
| 65 | private static $field_labels = array( |
||
| 66 | 'Note' => 'Note', |
||
| 67 | 'FutureReminderDate' => 'Future Reminder Date', |
||
| 68 | 'FutureReminderNote' => 'Future Reminder Note', |
||
| 69 | 'UpdateNoteRecord' => 'What', |
||
| 70 | 'UpdateNoteRecordClass' => 'Record Type', |
||
| 71 | 'UpdateNoteRecordID' => 'Record ID', |
||
| 72 | 'UpdatedBy' => 'Editor' |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * |
||
| 77 | * PartialMatchFilter |
||
| 78 | */ |
||
| 79 | private static $searchable_fields = array( |
||
| 80 | 'UpdatedByID' => array( |
||
| 81 | 'field' => 'UpdateNoteSearchField', |
||
| 82 | 'filter' => 'ExactMatchFilter', |
||
| 83 | 'title' => 'Edited By' |
||
| 84 | ), |
||
| 85 | 'Note' => 'PartialMatchFilter', |
||
| 86 | 'FutureReminderDate' => 'PartialMatchFilter', |
||
| 87 | 'FutureReminderNote' => 'PartialMatchFilter', |
||
| 88 | ); |
||
| 89 | |||
| 90 | |||
| 91 | protected function getEditorsDropdown() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * e.g. |
||
| 103 | * $controller = singleton("MyModelAdmin"); |
||
| 104 | * return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit"; |
||
| 105 | */ |
||
| 106 | public function CMSEditLink() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * CMS Fields |
||
| 116 | * @return FieldList |
||
| 117 | */ |
||
| 118 | public function getCMSFields() |
||
| 204 | |||
| 205 | public function getParentField($fieldLabels = null, $linkMethod = 'CMSEditLink') |
||
| 231 | |||
| 232 | public function getParent() |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * Event handler called before writing to the database. |
||
| 250 | */ |
||
| 251 | public function onBeforeWrite() |
||
| 262 | /** |
||
| 263 | * Creating Permissions |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | public function canCreate($member = null) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Editing Permissions |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function canEdit($member = null) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Deleting Permissions |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function canDelete($member = null) |
||
| 288 | |||
| 289 | public function getTitle() |
||
| 300 | } |
||
| 301 |
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.