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 | 2 | 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 | public function getEscapedValue() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param Member $member |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function canView($member = null) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return whether a user can create an object of this type |
||
| 99 | * |
||
| 100 | * @param Member $member |
||
| 101 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function canCreate($member = null) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Helper method to check the parent for this object |
||
| 118 | * |
||
| 119 | * @param array $args List of arguments passed to canCreate |
||
| 120 | * @return DataObject Some parent dataobject to inherit permissions from |
||
| 121 | */ |
||
| 122 | View Code Duplication | protected function getCanCreateContext($args) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param Member $member |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function canPublish($member = null) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param Member $member |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function canUnpublish($member = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Fetches a value for $this->Value. If empty values are not allowed, |
||
| 157 | * then this will return the title in the case of an empty value. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | 17 | public function getValue() |
|
| 169 | |||
| 170 | 39 | protected function onBeforeWrite() |
|
| 178 | } |
||
| 179 |
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.