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:
Complex classes like DiscountCouponOption often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DiscountCouponOption, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class DiscountCouponOption extends DataObject |
||
| 8 | { |
||
| 9 | private static $db = array( |
||
|
|
|||
| 10 | 'ApplyPercentageToApplicableProducts' => 'Boolean', |
||
| 11 | 'ApplyEvenWithoutCode' => 'Boolean', |
||
| 12 | 'Title' => 'Varchar(255)', |
||
| 13 | 'Code' => 'Varchar(32)', |
||
| 14 | 'NumberOfTimesCouponCanBeUsed' => 'Int', |
||
| 15 | 'StartDate' => 'Date', |
||
| 16 | 'EndDate' => 'Date', |
||
| 17 | 'MaximumDiscount' => 'Currency', |
||
| 18 | 'DiscountAbsolute' => 'Currency', |
||
| 19 | 'DiscountPercentage' => 'Decimal(4,2)', |
||
| 20 | 'MinimumOrderSubTotalValue' => 'Currency' |
||
| 21 | ); |
||
| 22 | |||
| 23 | private static $many_many = array( |
||
| 24 | 'Products' => 'Product', |
||
| 25 | 'ProductGroups' => 'ProductGroup', |
||
| 26 | 'ProductGroupsMustAlsoBePresentIn' => 'ProductGroup' |
||
| 27 | ); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * standard SS variable |
||
| 31 | * |
||
| 32 | */ |
||
| 33 | private static $indexes = array( |
||
| 34 | "Title" => true, |
||
| 35 | "Code" => true, |
||
| 36 | "StartDate" => true, |
||
| 37 | "EndDate" => true |
||
| 38 | ); |
||
| 39 | /** |
||
| 40 | * standard SS variable |
||
| 41 | * |
||
| 42 | */ |
||
| 43 | private static $casting = array( |
||
| 44 | "UseCount" => "Int", |
||
| 45 | "IsValid" => "Boolean", |
||
| 46 | "IsValidNice" => "Varchar" |
||
| 47 | ); |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * standard SS variable |
||
| 52 | * |
||
| 53 | */ |
||
| 54 | private static $searchable_fields = array( |
||
| 55 | 'StartDate' => array( |
||
| 56 | 'filter' => 'DiscountCouponFilterForDate', |
||
| 57 | ), |
||
| 58 | 'Title' => 'PartialMatchFilter', |
||
| 59 | "Code" => "PartialMatchFilter", |
||
| 60 | 'ApplyPercentageToApplicableProducts' => 'ExactMatchFilter', |
||
| 61 | 'ApplyEvenWithoutCode' => 'ExactMatchFilter', |
||
| 62 | 'DiscountAbsolute' => 'ExactMatchFilter', |
||
| 63 | 'DiscountPercentage' => 'ExactMatchFilter' |
||
| 64 | ); |
||
| 65 | |||
| 66 | public function scaffoldSearchFields($_params = null) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * standard SS variable |
||
| 87 | * |
||
| 88 | */ |
||
| 89 | private static $field_labels = array( |
||
| 90 | 'StartDate' => 'Start Date', |
||
| 91 | 'EndDate' => 'Last Day', |
||
| 92 | "Title" => "Name", |
||
| 93 | "MaximumDiscount" => "Maximum deduction", |
||
| 94 | "DiscountAbsolute" => "Absolute Discount", |
||
| 95 | "DiscountPercentage" => "Percentage Discount", |
||
| 96 | "ApplyPercentageToApplicableProducts" => "Applicable products only", |
||
| 97 | "NumberOfTimesCouponCanBeUsed" => "Availability count", |
||
| 98 | "UseCount" => "Count of usage thus far", |
||
| 99 | "IsValidNice" => "Current validity", |
||
| 100 | "ApplyEvenWithoutCode" => "Automatically applied", |
||
| 101 | "Products" => "Applicable products", |
||
| 102 | "ProductGroups" => "Applicable Categories", |
||
| 103 | "ProductGroupsMustAlsoBePresentIn" => "Products must also be listed in ... ", |
||
| 104 | ); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * standard SS variable |
||
| 108 | * |
||
| 109 | */ |
||
| 110 | private static $field_labels_right = array( |
||
| 111 | "ApplyEvenWithoutCode" => "Discount is automatically applied: the user does not have to enter the coupon at all. ", |
||
| 112 | "ApplyPercentageToApplicableProducts" => "Rather than applying it to the order, the discount is directly applied to selected products (you must select products).", |
||
| 113 | "Title" => "The name of the coupon is for internal use only. This name is not exposed to the customer but can be used to find a particular coupon.", |
||
| 114 | 'Code' => 'The code that the customer enters to get their discount.', |
||
| 115 | 'StartDate' => 'First date the coupon can be used.', |
||
| 116 | 'EndDate' => 'Last day the coupon can be used.', |
||
| 117 | "MaximumDiscount" => "This is the total amount of discount that can ever be applied - no matter waht. Set to zero to ignore.", |
||
| 118 | "DiscountAbsolute" => "Absolute reduction. For example, 10 = -$10.00 off. Set this value to zero to ignore.", |
||
| 119 | "DiscountPercentage" => "Percentage Discount. For example, 10 = -10% discount Set this value to zero to ignore.", |
||
| 120 | "MinimumOrderSubTotalValue" => "Minimum sub-total of total order to make coupon applicable. For example, order must be at least $100 before the customer gets a discount.", |
||
| 121 | "NumberOfTimesCouponCanBeUsed" => "Set to zero to disallow usage, set to 999,999 to allow unlimited usage.", |
||
| 122 | "UseCount" => "number of times this coupon has been used", |
||
| 123 | "IsValidNice" => "coupon is currently valid", |
||
| 124 | "Products" => "This is the final list of products to which the coupon applies. To edit this list directly, please remove all product groups selections in the 'Add Products Using Categories' tab.", |
||
| 125 | "ProductGroups" => "Adding product categories helps you to select a large number of products at once. Please select categories above. The products in each category selected will be added to the list.", |
||
| 126 | "ProductGroupsMustAlsoBePresentIn" => "Select cross-reference listing products (listed in both categories) - e.g. products that are in the Large Items category and Expensive Items category will have a discount.", |
||
| 127 | ); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * standard SS variable |
||
| 131 | * |
||
| 132 | */ |
||
| 133 | private static $summary_fields = array( |
||
| 134 | "Title" => "Name", |
||
| 135 | "Code" => 'Code', |
||
| 136 | "StartDate.Full" => 'From', |
||
| 137 | "EndDate.Full" => 'Until', |
||
| 138 | 'IsValidNice' => 'Current' |
||
| 139 | ); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * standard SS variable |
||
| 143 | * |
||
| 144 | */ |
||
| 145 | private static $defaults = array( |
||
| 146 | "NumberOfTimesCouponCanBeUsed" => "999999" |
||
| 147 | ); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * standard SS variable |
||
| 151 | * |
||
| 152 | */ |
||
| 153 | private static $singular_name = "Discount Coupon"; |
||
| 154 | public function i18n_singular_name() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * standard SS variable |
||
| 161 | * |
||
| 162 | */ |
||
| 163 | private static $plural_name = "Discount Coupons"; |
||
| 164 | public function i18n_plural_name() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * standard SS variable |
||
| 171 | * |
||
| 172 | */ |
||
| 173 | private static $default_sort = [ |
||
| 174 | 'EndDate' => 'DESC', |
||
| 175 | 'StartDate' => 'DESC', |
||
| 176 | 'ID' => 'ASC' |
||
| 177 | ]; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * |
||
| 181 | * @var Boolean |
||
| 182 | */ |
||
| 183 | protected $isNew = false; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * standard SS method |
||
| 187 | * |
||
| 188 | */ |
||
| 189 | public function populateDefaults() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * casted variable |
||
| 198 | * returns the number of times this coupon has been used. |
||
| 199 | * Some of the used coupons are not submitted yet, but it should still |
||
| 200 | * work on first come first served basis. |
||
| 201 | * |
||
| 202 | * @return Int |
||
| 203 | */ |
||
| 204 | public function UseCount() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * casted variable telling us if the discount coupon is valid. |
||
| 218 | * |
||
| 219 | * @return Bool |
||
| 220 | */ |
||
| 221 | public function IsValid() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * casted variable telling us if the discount coupon is valid - formatted nicely... |
||
| 259 | * |
||
| 260 | * @return String |
||
| 261 | */ |
||
| 262 | public function IsValidNice() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * standard SS method |
||
| 273 | * @param Member | NULL |
||
| 274 | * @return Boolean |
||
| 275 | */ |
||
| 276 | View Code Duplication | public function canCreate($member = null) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * standard SS method |
||
| 286 | * @param Member | NULL |
||
| 287 | * @return Boolean |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function canView($member = null) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * standard SS method |
||
| 299 | * @param Member | NULL |
||
| 300 | * @return Boolean |
||
| 301 | */ |
||
| 302 | View Code Duplication | public function canEdit($member = null) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * standard SS method |
||
| 312 | * |
||
| 313 | * @param Member | NULL |
||
| 314 | * |
||
| 315 | * @return Boolean |
||
| 316 | */ |
||
| 317 | public function canDelete($member = null) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * standard SS method |
||
| 330 | * |
||
| 331 | */ |
||
| 332 | public function getCMSFields() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * standard SS method |
||
| 382 | * THIS ONLY WORKS FOR CREATED OBJECTS |
||
| 383 | */ |
||
| 384 | |||
| 385 | public function validate() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Checks if there are coupons with the same code as the current one |
||
| 419 | * @return Boolean |
||
| 420 | */ |
||
| 421 | protected function thereAreCouponsWithTheSameCode() |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * standard SS method |
||
| 429 | * |
||
| 430 | */ |
||
| 431 | public function onBeforeWrite() |
||
| 458 | |||
| 459 | protected $_productsCalculated = false; |
||
| 460 | /** |
||
| 461 | * standard SS method |
||
| 462 | * |
||
| 463 | */ |
||
| 464 | public function onAfterWrite() |
||
| 494 | |||
| 495 | public function onBeforeDelete() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * returns a random string. |
||
| 504 | * @param Int $length - number of characters |
||
| 505 | * @param Int $chars - input characters |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | protected function createRandomCode($length = 5, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890') |
||
| 520 | } |
||
| 521 |