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 DiscountManager 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 DiscountManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class DiscountManager extends MY_Controller |
||
| 23 | { |
||
| 24 | |||
| 25 | public $error = []; |
||
| 26 | |||
| 27 | public function __construct() { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * create brand discount |
||
| 37 | * @access public |
||
| 38 | * @author DevImageCms |
||
| 39 | * @param array $data input params: |
||
| 40 | * - (string) key: discount key optional |
||
| 41 | * - (string) name: discount name |
||
| 42 | * - (int) max_apply: max apply discount default null - infinity |
||
| 43 | * - (int) type_value: 1 - % 2 - float |
||
| 44 | * - (int) value: discount value |
||
| 45 | * - (string) type_discount: (all_order, comulativ, user, group_user, category, product, brand) |
||
| 46 | * - (string) date_begin: data begin discount |
||
| 47 | * - (string) date_end: data end discount default null - infinity |
||
| 48 | * - (int) brand_id: id brand |
||
| 49 | * @return array $data params: |
||
| 50 | * - (boolean) success: result of create brand discount |
||
| 51 | * - (string) errors: message of error |
||
| 52 | * @copyright (c) 2013, ImageCMS |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function createBrandDiscount($data) { |
|
| 64 | |||
| 65 | /** |
||
| 66 | * create category discount |
||
| 67 | * @access public |
||
| 68 | * @author DevImageCms |
||
| 69 | * @param array $data input params: |
||
| 70 | * - (string) key: discount key optional |
||
| 71 | * - (string) name: discount name |
||
| 72 | * - (int) max_apply: max apply discount default null - infinity |
||
| 73 | * - (int) type_value: 1 - % 2 - float |
||
| 74 | * - (int) value: discount value |
||
| 75 | * - (string) type_discount: (all_order, comulativ, user, group_user, category, product, brand) |
||
| 76 | * - (string) date_begin: data begin discount |
||
| 77 | * - (string) date_end: data end discount default null - infinity |
||
| 78 | * - (int) category_id: id category |
||
| 79 | * - (int) child: change childs category |
||
| 80 | * @return array $data params: |
||
| 81 | * - (boolean) success: result of create category discount |
||
| 82 | * - (string) errors: message of error |
||
| 83 | * @copyright (c) 2013, ImageCMS |
||
| 84 | */ |
||
| 85 | public function createCategoryDiscount($data) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * create product discount |
||
| 99 | * @access public |
||
| 100 | * @author DevImageCms |
||
| 101 | * @param array $data input params: |
||
| 102 | * - (string) key: discount key optional |
||
| 103 | * - (string) name: discount name |
||
| 104 | * - (int) max_apply: max apply discount default null - infinity |
||
| 105 | * - (int) type_value: 1 - % 2 - float |
||
| 106 | * - (int) value: discount value |
||
| 107 | * - (string) type_discount: (all_order, comulativ, user, group_user, category, product, brand) |
||
| 108 | * - (string) date_begin: data begin discount |
||
| 109 | * - (string) date_end: data end discount default null - infinity |
||
| 110 | * - (int) product_id: id product |
||
| 111 | * @return array $data params: |
||
| 112 | * - (boolean) success: result of create product discount |
||
| 113 | * - (string) errors: message of error |
||
| 114 | * @copyright (c) 2013, ImageCMS |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function createProductDiscount($data) { |
|
| 126 | |||
| 127 | /** |
||
| 128 | * create user discount |
||
| 129 | * @access public |
||
| 130 | * @author DevImageCms |
||
| 131 | * @param array $data input params: |
||
| 132 | * - (string) key: discount key optional |
||
| 133 | * - (string) name: discount name |
||
| 134 | * - (int) max_apply: max apply discount default null - infinity |
||
| 135 | * - (int) type_value: 1 - % 2 - float |
||
| 136 | * - (int) value: discount value |
||
| 137 | * - (string) type_discount: (all_order, comulativ, user, group_user, category, product, brand) |
||
| 138 | * - (string) date_begin: data begin discount |
||
| 139 | * - (string) date_end: data end discount default null - infinity |
||
| 140 | * - (int) user_id: id user |
||
| 141 | * @return array $data params: |
||
| 142 | * - (boolean) success: result of create user discount |
||
| 143 | * - (string) errors: message of error |
||
| 144 | * @copyright (c) 2013, ImageCMS |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function createUserDiscount($data) { |
|
| 156 | |||
| 157 | /** |
||
| 158 | * create user group discount discount |
||
| 159 | * @access public |
||
| 160 | * @author DevImageCms |
||
| 161 | * @param array $data input params: |
||
| 162 | * - (string) key: discount key optional |
||
| 163 | * - (string) name: discount name |
||
| 164 | * - (int) max_apply: max apply discount default null - infinity |
||
| 165 | * - (int) type_value: 1 - % 2 - float |
||
| 166 | * - (int) value: discount value |
||
| 167 | * - (string) type_discount: (all_order, comulativ, user, group_user, category, product, brand) |
||
| 168 | * - (string) date_begin: data begin discount |
||
| 169 | * - (string) date_end: data end discount default null - infinity |
||
| 170 | * - (int) group_id: id user group |
||
| 171 | * @return array $data params: |
||
| 172 | * - (boolean) success: result of create user group discount |
||
| 173 | * - (string) errors: message of error |
||
| 174 | * @copyright (c) 2013, ImageCMS |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function createUserGroupDiscount($data) { |
|
| 186 | |||
| 187 | /** |
||
| 188 | * create comulativ discount |
||
| 189 | * @access public |
||
| 190 | * @author DevImageCms |
||
| 191 | * @param array $data input params: |
||
| 192 | * - (string) key: discount key optional |
||
| 193 | * - (string) name: discount name |
||
| 194 | * - (int) max_apply: max apply discount default null - infinity |
||
| 195 | * - (int) type_value: 1 - % 2 - float |
||
| 196 | * - (int) value: discount value |
||
| 197 | * - (string) type_discount: (all_order, comulativ, user, group_user, category, product, brand) |
||
| 198 | * - (string) date_begin: data begin discount |
||
| 199 | * - (string) date_end: data end discount default null - infinity |
||
| 200 | * - (float) begin_value: value begin |
||
| 201 | * - (float) end_value: value end default null - infinity |
||
| 202 | * @return array $data params: |
||
| 203 | * - (boolean) success: result of create comulativ |
||
| 204 | * - (string) errors: message of error |
||
| 205 | * @copyright (c) 2013, ImageCMS |
||
| 206 | */ |
||
| 207 | public function createComulativDiscount($data) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * create gift discount |
||
| 219 | * @access public |
||
| 220 | * @author DevImageCms |
||
| 221 | * @param array $data input params: |
||
| 222 | * - (string) key: discount key optional |
||
| 223 | * - (string) name: discount name |
||
| 224 | * - (int) max_apply: max apply discount default null - infinity |
||
| 225 | * - (int) type_value: 1 - % 2 - float |
||
| 226 | * - (int) value: discount value |
||
| 227 | * - (string) type_discount: (all_order, comulativ, user, group_user, category, product, brand) |
||
| 228 | * - (string) date_begin: data begin discount |
||
| 229 | * - (string) date_end: data end discount default null - infinity |
||
| 230 | * @return array $data params: |
||
| 231 | * - (boolean) success: result of create gift |
||
| 232 | * - (string) errors: message of error |
||
| 233 | * @copyright (c) 2013, ImageCMS |
||
| 234 | */ |
||
| 235 | public function createGift($data) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * delete discount |
||
| 245 | * @access public |
||
| 246 | * @author DevImageCms |
||
| 247 | * @param (int) id discount: |
||
| 248 | * @return (boolean) success: result of deleting |
||
| 249 | * @copyright (c) 2013, ImageCMS |
||
| 250 | */ |
||
| 251 | public function deleteDiscount($id) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * create discount |
||
| 258 | * @access public |
||
| 259 | * @author DevImageCms |
||
| 260 | * @param array $postArray input params: |
||
| 261 | * @return array $data params: |
||
| 262 | * - (boolean) success: result of create |
||
| 263 | * - (string) errors: message of error |
||
| 264 | * @copyright (c) 2013, ImageCMS |
||
| 265 | */ |
||
| 266 | public function create($postArray) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * validation data |
||
| 354 | * @access public |
||
| 355 | * @author DevImageCms |
||
| 356 | * @param array $postArray |
||
| 357 | * @param integer $id (optional) need to be specified on discount editing |
||
| 358 | * @copyright (c) 2013, ImageCMS |
||
| 359 | */ |
||
| 360 | public function validation($postArray, $id = null) { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Helper function for checking that user have no discounts already |
||
| 449 | * @param integer $userId id of user |
||
| 450 | * @return boolean true if user have no discounts alreaty, false otherwise |
||
| 451 | */ |
||
| 452 | public static function validateUserDiscount($userId) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Helper function for checking that user-group have no discounts already |
||
| 465 | * @param integer $groupId id of group |
||
| 466 | * @return boolean true if user-group have no discounts alreaty, false otherwise |
||
| 467 | */ |
||
| 468 | public static function validateGroupDiscount($groupId) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Generate key for discount |
||
| 481 | * |
||
| 482 | * @param integer $charsCount |
||
| 483 | * @param integer $digitsCount |
||
| 484 | * @static |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public static function generateDiscountKey($charsCount = 8, $digitsCount = 8) { |
||
| 496 | |||
| 497 | } |