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 | |||
| 13 | private static $default_sort = "Sort"; |
||
| 14 | |||
| 15 | private static $db = array( |
||
| 16 | "Name" => "Varchar(255)", |
||
| 17 | "Title" => "Varchar(255)", |
||
| 18 | "Default" => "Boolean", |
||
| 19 | "Sort" => "Int", |
||
| 20 | "Value" => "Varchar(255)", |
||
| 21 | ); |
||
| 22 | |||
| 23 | private static $has_one = array( |
||
| 24 | "Parent" => "EditableMultipleOptionField", |
||
| 25 | ); |
||
| 26 | |||
| 27 | private static $extensions = array( |
||
| 28 | "Versioned('Stage', 'Live')" |
||
| 29 | ); |
||
| 30 | |||
| 31 | private static $summary_fields = array( |
||
| 32 | 'Title', |
||
| 33 | 'Default' |
||
| 34 | ); |
||
| 35 | |||
| 36 | protected static $allow_empty_values = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns whether to allow empty values or not. |
||
| 40 | * |
||
| 41 | * @return boolean |
||
| 42 | */ |
||
| 43 | public static function allow_empty_values() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Set whether to allow empty values. |
||
| 50 | * |
||
| 51 | * @param boolean $allow |
||
| 52 | */ |
||
| 53 | public static function set_allow_empty_values($allow) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param Member $member |
||
| 60 | * |
||
| 61 | * @return boolean |
||
| 62 | */ |
||
| 63 | public function canEdit($member = null) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param Member $member |
||
| 70 | * |
||
| 71 | * @return boolean |
||
| 72 | */ |
||
| 73 | public function canDelete($member = null) |
||
| 77 | |||
| 78 | public function getEscapedTitle() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param Member $member |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | public function canView($member = null) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return whether a user can create an object of this type |
||
| 94 | * |
||
| 95 | * @param Member $member |
||
| 96 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function canCreate($member = null) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Helper method to check the parent for this object |
||
| 113 | * |
||
| 114 | * @param array $args List of arguments passed to canCreate |
||
| 115 | * @return DataObject Some parent dataobject to inherit permissions from |
||
| 116 | */ |
||
| 117 | View Code Duplication | protected function getCanCreateContext($args) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param Member $member |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | public function canPublish($member = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param Member $member |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function canUnpublish($member = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Fetches a value for $this->Value. If empty values are not allowed, |
||
| 152 | * then this will return the title in the case of an empty value. |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function getValue() |
||
| 164 | |||
| 165 | protected function onBeforeWrite() { |
||
| 172 | } |
||
| 173 |
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.