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 |
||
| 3 | class ProductAttributeValue extends DataObject implements EditableEcommerceObject |
||
|
|
|||
| 4 | { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Standard SS variable. |
||
| 8 | */ |
||
| 9 | private static $api_access = array( |
||
| 10 | 'view' => array( |
||
| 11 | "Value", |
||
| 12 | "Type" |
||
| 13 | ) |
||
| 14 | ); |
||
| 15 | |||
| 16 | private static $db = array( |
||
| 17 | 'Code' => 'Varchar(255)', |
||
| 18 | 'Value' => 'Varchar(255)', |
||
| 19 | 'Sort' => 'Int', |
||
| 20 | 'MergeIntoNote' => 'Varchar(255)' |
||
| 21 | ); |
||
| 22 | |||
| 23 | private static $has_one = array( |
||
| 24 | 'Type' => 'ProductAttributeType', |
||
| 25 | 'MergeInto' => 'ProductAttributeValue' |
||
| 26 | ); |
||
| 27 | |||
| 28 | private static $belongs_many_many = array( |
||
| 29 | 'ProductVariation' => 'ProductVariation' |
||
| 30 | ); |
||
| 31 | |||
| 32 | private static $summary_fields = array( |
||
| 33 | 'Type.FullName' => 'Type', |
||
| 34 | 'Value' => 'Value' |
||
| 35 | ); |
||
| 36 | |||
| 37 | private static $searchable_fields = array( |
||
| 38 | 'Value' => 'PartialMatchFilter' |
||
| 39 | ); |
||
| 40 | |||
| 41 | private static $casting = array( |
||
| 42 | 'Title' => 'HTMLText', |
||
| 43 | 'FullTitle' => 'Varchar', |
||
| 44 | 'ValueForDropdown' => "HTMLText", |
||
| 45 | 'ValueForTable' => "HTMLText" |
||
| 46 | ); |
||
| 47 | |||
| 48 | private static $indexes = array( |
||
| 49 | 'Sort' => true, |
||
| 50 | 'Code' => true |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * finds or makes a ProductAttributeType, based on the lower case Name. |
||
| 55 | * |
||
| 56 | * @param productAttributeType | int $type |
||
| 57 | * @param string $value |
||
| 58 | * @param boolean $create |
||
| 59 | * @param boolean $findByID |
||
| 60 | * |
||
| 61 | * @return ProductAttributeType |
||
| 62 | */ |
||
| 63 | public static function find_or_make($type, $value, $create = true, $findByID = false) |
||
| 95 | |||
| 96 | private static $default_sort = "\"TypeID\" ASC, \"Sort\" ASC"; |
||
| 97 | |||
| 98 | private static $singular_name = "Variation Attribute Value"; |
||
| 99 | public function i18n_singular_name() |
||
| 103 | |||
| 104 | private static $plural_name = "Variation Attribute Values"; |
||
| 105 | public function i18n_plural_name() |
||
| 109 | |||
| 110 | public function canDelete($member = null) |
||
| 127 | |||
| 128 | public function getCMSFields() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * link to edit the record |
||
| 153 | * @param String | Null $action - e.g. edit |
||
| 154 | * @return String |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function CMSEditLink($action = null) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * casted variable |
||
| 167 | * returns the value for the option in the select dropdown box. |
||
| 168 | * @return String (HTML) |
||
| 169 | **/ |
||
| 170 | public function ValueForDropdown() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * casted variable |
||
| 186 | * returns the value for the variations table |
||
| 187 | * @return String (HTML) |
||
| 188 | **/ |
||
| 189 | public function ValueForTable() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * casted variable |
||
| 205 | * returns the value for the variations table |
||
| 206 | * @return String |
||
| 207 | **/ |
||
| 208 | public function Title() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * casted variable |
||
| 219 | * returns the value for the variations table |
||
| 220 | * @return String |
||
| 221 | **/ |
||
| 222 | public function FullTitle() |
||
| 235 | |||
| 236 | public function onBeforeDelete() |
||
| 242 | |||
| 243 | public function onBeforeWrite() |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Event handler called after writing to the database. |
||
| 263 | */ |
||
| 264 | public function onAfterWrite() |
||
| 293 | |||
| 294 | } |
||
| 295 |
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.