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 |
||
| 10 | class EditableOption extends DataObject { |
||
|
|
|||
| 11 | |||
| 12 | private static $default_sort = "Sort"; |
||
| 13 | |||
| 14 | private static $db = array( |
||
| 15 | "Name" => "Varchar(255)", |
||
| 16 | "Title" => "Varchar(255)", |
||
| 17 | "Default" => "Boolean", |
||
| 18 | "Sort" => "Int" |
||
| 19 | ); |
||
| 20 | |||
| 21 | private static $has_one = array( |
||
| 22 | "Parent" => "EditableMultipleOptionField", |
||
| 23 | ); |
||
| 24 | |||
| 25 | private static $extensions = array( |
||
| 26 | "Versioned('Stage', 'Live')" |
||
| 27 | ); |
||
| 28 | |||
| 29 | private static $summary_fields = array( |
||
| 30 | 'Title', |
||
| 31 | 'Default' |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param Member $member |
||
| 36 | * |
||
| 37 | * @return boolean |
||
| 38 | */ |
||
| 39 | public function canEdit($member = null) { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param Member $member |
||
| 45 | * |
||
| 46 | * @return boolean |
||
| 47 | */ |
||
| 48 | public function canDelete($member = null) { |
||
| 51 | |||
| 52 | public function getEscapedTitle() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param Member $member |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function canView($member = null) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Return whether a user can create an object of this type |
||
| 66 | * |
||
| 67 | * @param Member $member |
||
| 68 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function canCreate($member = null) { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Helper method to check the parent for this object |
||
| 84 | * |
||
| 85 | * @param array $args List of arguments passed to canCreate |
||
| 86 | * @return DataObject Some parent dataobject to inherit permissions from |
||
| 87 | */ |
||
| 88 | View Code Duplication | protected function getCanCreateContext($args) { |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param Member $member |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | public function canPublish($member = null) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param Member $member |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function canUnpublish($member = null) { |
||
| 117 | } |
||
| 118 |
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.