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