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 |
||
| 4 | class EcommerceSecurityBaseClass extends DataObject |
||
|
|
|||
| 5 | { |
||
| 6 | |||
| 7 | |||
| 8 | /** |
||
| 9 | * standard SS variable |
||
| 10 | * @Var String |
||
| 11 | */ |
||
| 12 | private static $singular_name = "Blacklisted Item"; |
||
| 13 | public function i18n_singular_name() |
||
| 17 | /** |
||
| 18 | * standard SS variable |
||
| 19 | * @Var String |
||
| 20 | */ |
||
| 21 | private static $plural_name = "Blacklisted Items"; |
||
| 22 | public function i18n_plural_name() |
||
| 26 | |||
| 27 | private static $db = array( |
||
| 28 | 'Title' => 'Varchar(200)', |
||
| 29 | 'Status' => 'Enum("Unknown, Good, Bad", "Unknown")' |
||
| 30 | ); |
||
| 31 | |||
| 32 | private static $belongs_many_many = array( |
||
| 33 | 'SecurityChecks' => 'OrderStatusLog_SecurityCheck' |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $casting = array( |
||
| 37 | 'Type' => 'Varchar', |
||
| 38 | 'SimplerName' => 'Varchar' |
||
| 39 | ); |
||
| 40 | |||
| 41 | private static $summary_fields = array( |
||
| 42 | 'Created' => 'Created', |
||
| 43 | 'LastEdited.Ago' => 'Last Edit', |
||
| 44 | 'SimplerName' => 'Type', |
||
| 45 | 'Title' => 'Value', |
||
| 46 | 'Status' => 'Status' |
||
| 47 | ); |
||
| 48 | |||
| 49 | private static $indexes = array( |
||
| 50 | 'ClassName_Title' => array('type' => 'unique', 'value' => '"ClassName","Title"'), |
||
| 51 | 'Title' => true, |
||
| 52 | 'ClassName' => true, |
||
| 53 | ); |
||
| 54 | |||
| 55 | private static $field_labels = array( |
||
| 56 | 'Title' => 'Value' |
||
| 57 | ); |
||
| 58 | |||
| 59 | private static $searchable_fields = array( |
||
| 60 | 'Title' => 'PartialMatchFilter', |
||
| 61 | 'ClassName' => array( |
||
| 62 | 'filter' => 'PartialMatchFilter', |
||
| 63 | 'title' => 'Type' |
||
| 64 | ), |
||
| 65 | 'Status' => 'PartialMatchFilter' |
||
| 66 | ); |
||
| 67 | |||
| 68 | private static $default_sort = 'Status DESC'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * filter value examples are: |
||
| 72 | * ```php |
||
| 73 | * array('Title' => 'Foo') |
||
| 74 | * ``` |
||
| 75 | * you can not provide multi-dimensional arrays |
||
| 76 | * |
||
| 77 | * @param array $filterArray associative array of filter values |
||
| 78 | * @param bool $filterArray if a new one is created, should it be written |
||
| 79 | * @return DataObject |
||
| 80 | */ |
||
| 81 | public static function find_or_create($filterArray, $write = true) |
||
| 100 | |||
| 101 | public function canCreate($member = null) |
||
| 105 | |||
| 106 | View Code Duplication | public function canView($member = null) |
|
| 121 | |||
| 122 | View Code Duplication | public function canEdit($member = null) |
|
| 137 | |||
| 138 | public function canDelete($member = null) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * CMS Fields |
||
| 145 | * @return FieldList |
||
| 146 | */ |
||
| 147 | public function getCMSFields() |
||
| 177 | |||
| 178 | public function getType() |
||
| 182 | |||
| 183 | public function getSimplerName() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function hasRisks() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function isSafe() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function hasOpinion() |
||
| 217 | |||
| 218 | public function requireDefaultRecords() |
||
| 222 | } |
||
| 223 |
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.