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 discount_api 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 discount_api, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class discount_api extends MY_Controller |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * __construct base object loaded |
||
26 | * @access public |
||
27 | * @author DevImageCms |
||
28 | * @copyright (c) 2013, ImageCMS |
||
29 | */ |
||
30 | public function __construct() { |
||
35 | |||
36 | /** |
||
37 | * is in project gift certificate |
||
38 | * @deprecated since version 4.5.2 |
||
39 | * @copyright (c) 2013, ImageCMS |
||
40 | */ |
||
41 | public function isGiftCertificat() { |
||
50 | |||
51 | /** |
||
52 | * get gift certificate in json format |
||
53 | * @access public |
||
54 | * @author DevImageCms |
||
55 | * @param (string) key optional |
||
56 | * @param (float) totalPrice optional |
||
57 | * @return string |
||
58 | * @copyright (c) 2013, ImageCMS |
||
59 | */ |
||
60 | public function getGiftCertificate($key = null, $totalPrice = null) { |
||
80 | |||
81 | /** |
||
82 | * get discount in json format |
||
83 | * @access public |
||
84 | * @author DevImageCms |
||
85 | * @param array $option |
||
86 | * @param array $typeReturn params: |
||
87 | * - (float) price: |
||
88 | * - (int) userId: |
||
89 | * - (bool) ignoreCart: ignore cart Data: |
||
90 | * - (bool) new: for redeclare singelton: |
||
91 | * @return json |
||
92 | * @internal param $ (bool) typeReturn optional |
||
93 | * @copyright (c) 2013, ImageCMS |
||
94 | */ |
||
95 | public function getDiscount($option = [], $typeReturn = null) { |
||
125 | |||
126 | /** |
||
127 | * get discount product |
||
128 | * @access public |
||
129 | * @author DevImageCms |
||
130 | * @param float|null $product |
||
131 | * @param array [pid,vid], (float) price optional |
||
132 | * @return array |
||
133 | * @copyright (c) 2013, ImageCMS |
||
134 | */ |
||
135 | public function getDiscountProduct($product, $price = null) { |
||
149 | |||
150 | /** |
||
151 | * get all discount information without maximized |
||
152 | * @access public |
||
153 | * @author DevImageCms |
||
154 | * @param array $option params: |
||
155 | * - (float) price: |
||
156 | * - (int) userId: |
||
157 | * - (bool) ignoreCart: ignore cart Data: |
||
158 | * - (bool) new: for redeclare singelton: |
||
159 | * @return array |
||
160 | * @copyright (c) 2013, ImageCMS |
||
161 | */ |
||
162 | public function getUserDiscount($option = []) { |
||
185 | |||
186 | /** |
||
187 | * If discount for current user exist or nots |
||
188 | * @return boolean |
||
189 | */ |
||
190 | public function discountsExists() { |
||
196 | |||
197 | public function userDiscountExists() { |
||
200 | |||
201 | public function groupDiscountExists() { |
||
204 | |||
205 | /** |
||
206 | * get comulativ discount sorting |
||
207 | * @access private |
||
208 | * @author DevImageCms |
||
209 | * @param (float) price optional |
||
210 | * @param (float) userId optional |
||
211 | * @param (bool) new optional |
||
212 | * @return array |
||
213 | * @copyright (c) 2013, ImageCMS |
||
214 | */ |
||
215 | private function getComulativDiscount($option) { |
||
225 | |||
226 | /** |
||
227 | * get one discount |
||
228 | * @access public |
||
229 | * @author DevImageCms |
||
230 | * @param string $key: criteria |
||
231 | * @param int $id: id discount |
||
232 | * @return string|false |
||
233 | * @copyright (c) 2013, ImageCMS |
||
234 | */ |
||
235 | public function getDiscountBy($key, $id) { |
||
257 | |||
258 | /** |
||
259 | * get all active discount |
||
260 | * @access public |
||
261 | * @author DevImageCms |
||
262 | * @param (float) price optional |
||
263 | * @param (float) userId optional |
||
264 | * @param (bool) new optional |
||
265 | * @return string |
||
266 | * @copyright (c) 2013, ImageCMS |
||
267 | */ |
||
268 | public function getAllDiscount() { |
||
273 | |||
274 | /** |
||
275 | * apply discount for all products |
||
276 | * @access public |
||
277 | * @author DevImageCms |
||
278 | * @return (int) cnt: count update products |
||
279 | * @copyright (c) 2013, ImageCMS |
||
280 | */ |
||
281 | public function applyProductDiscount() { |
||
313 | |||
314 | /** |
||
315 | * is in project gift certificate |
||
316 | * @deprecated since version 4.5.2 |
||
317 | * @copyright (c) 2013, ImageCMS |
||
318 | */ |
||
319 | public function is_gift_certificat() { |
||
322 | |||
323 | /** |
||
324 | * render gift input |
||
325 | * @deprecated since version 4.5.2 |
||
326 | * @copyright (c) 2013, ImageCMS |
||
327 | */ |
||
328 | public function render_gift_input($mes = null) { |
||
335 | |||
336 | /** |
||
337 | * get gift certificate in tpl |
||
338 | * @deprecated since version 4.5.2 |
||
339 | * @copyright (c) 2013, ImageCMS |
||
340 | */ |
||
341 | public function render_gift_succes() { |
||
345 | |||
346 | /** |
||
347 | * get all discount information without maximized |
||
348 | * @deprecated since version 4.5.2 |
||
349 | * @copyright (c) 2013, ImageCMS |
||
350 | * @param array $option |
||
351 | * @return array |
||
352 | */ |
||
353 | public function get_user_discount_api($option = []) { |
||
356 | |||
357 | /** |
||
358 | * get discount product |
||
359 | * @deprecated since version 4.5.2 |
||
360 | * @copyright (c) 2013, ImageCMS |
||
361 | * @param $product |
||
362 | * @param null $typeReturn |
||
363 | * @param null $price |
||
364 | * @return array |
||
365 | */ |
||
366 | public function get_discount_product_api($product, $typeReturn = null, $price = null) { |
||
369 | |||
370 | /** |
||
371 | * get discount in tpl format from json |
||
372 | * @deprecated since version 4.5.2 |
||
373 | * @copyright (c) 2013, ImageCMS |
||
374 | */ |
||
375 | public function get_discount_tpl_from_json_api() { |
||
380 | |||
381 | /** |
||
382 | * get discount in json format |
||
383 | * @deprecated since version 4.5.2 |
||
384 | * @copyright (c) 2013, ImageCMS |
||
385 | * @param array $option |
||
386 | * @return json |
||
387 | */ |
||
388 | public function get_discount_api($option = []) { |
||
391 | |||
392 | /** |
||
393 | * get gift certificate in json format |
||
394 | * @deprecated since version 4.5.2 |
||
395 | * @copyright (c) 2013, ImageCMS |
||
396 | * @param null|string $key |
||
397 | * @param null|string $totalPrice |
||
398 | * @return string |
||
399 | */ |
||
400 | public function get_gift_certificate($key = null, $totalPrice = null) { |
||
403 | |||
404 | } |