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 Wishlist_model 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 Wishlist_model, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Wishlist_model extends CI_Model |
||
11 | { |
||
|
|||
12 | |||
13 | public function __construct() { |
||
16 | |||
17 | /** |
||
18 | * Get module settings |
||
19 | * @return array |
||
20 | */ |
||
21 | public function getSettings() { |
||
25 | |||
26 | /** |
||
27 | * Save settings |
||
28 | * @param array $settings |
||
29 | * @return boolean |
||
30 | */ |
||
31 | public function setSettings($settings) { |
||
35 | |||
36 | /** |
||
37 | * get wish lists |
||
38 | * |
||
39 | * @param integer|null $userID filter by user id |
||
40 | * @return array |
||
41 | */ |
||
42 | public function getWishLists($userID = NULL) { |
||
52 | |||
53 | /** |
||
54 | * get all users |
||
55 | * |
||
56 | * @return array|boolean |
||
57 | */ |
||
58 | public function getAllUsers() { |
||
69 | |||
70 | /** |
||
71 | * get user by id |
||
72 | * |
||
73 | * @param integer $id |
||
74 | * @return array|bool |
||
75 | */ |
||
76 | public function getUserByID($id) { |
||
87 | |||
88 | /** |
||
89 | * get wish list by user id |
||
90 | * |
||
91 | * @param integer $user_id |
||
92 | * @param array $access |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getWLsByUserId($user_id, $access = ['shared']) { |
||
102 | |||
103 | /** |
||
104 | * get user wish list |
||
105 | * |
||
106 | * @param integer $user_id |
||
107 | * @param integer $list_id |
||
108 | * @param array $access |
||
109 | * @return array |
||
110 | */ |
||
111 | public function getUserWishList($user_id, $list_id, $access = ['public', 'shared', 'private']) { |
||
147 | |||
148 | /** |
||
149 | * get user wish list by hash |
||
150 | * |
||
151 | * @param integer $hash |
||
152 | * @param array $access |
||
153 | * @return array |
||
154 | */ |
||
155 | public function getUserWishListByHash($hash, $access = ['public', 'shared', 'private']) { |
||
184 | |||
185 | /** |
||
186 | * delete item from list |
||
187 | * |
||
188 | * @param integer $variant_id |
||
189 | * @param integer $wish_list_id |
||
190 | * @return boolean |
||
191 | */ |
||
192 | public function deleteItem($variant_id, $wish_list_id) { |
||
207 | |||
208 | /** |
||
209 | * delete items by ids |
||
210 | * |
||
211 | * @param array $ids |
||
212 | * @return array |
||
213 | */ |
||
214 | public function deleteItemsByIDs($ids) { |
||
218 | |||
219 | /** |
||
220 | * get user wish list by id |
||
221 | * |
||
222 | * @param integer $user_id |
||
223 | * @param array $access |
||
224 | * @return array |
||
225 | */ |
||
226 | public function getUserWishListsByID($user_id, $access = ['public', 'shared', 'private']) { |
||
270 | |||
271 | /** |
||
272 | * delete wish list by id |
||
273 | * |
||
274 | * @param integer$id |
||
275 | * @return boolean |
||
276 | */ |
||
277 | public function delWishListById($id) { |
||
283 | |||
284 | /** |
||
285 | * delete wish list products by wish list id |
||
286 | * |
||
287 | * @param integer $id |
||
288 | * @return boolean |
||
289 | */ |
||
290 | public function delWishListProductsByWLId($id) { |
||
295 | |||
296 | /** |
||
297 | * get user wish list products |
||
298 | * |
||
299 | * @param integer $userID |
||
300 | * @return array |
||
301 | */ |
||
302 | public function getUserWishProducts($userID = null) { |
||
323 | |||
324 | /** |
||
325 | * |
||
326 | * |
||
327 | * @param integer $userID |
||
328 | * @return array |
||
329 | */ |
||
330 | public function getAllUserWLs($userID = null) { |
||
351 | |||
352 | /** |
||
353 | * get most popular products |
||
354 | * |
||
355 | * @param integer $limit |
||
356 | * @return array |
||
357 | */ |
||
358 | public function getMostPopularProducts($limit = 10) { |
||
371 | |||
372 | /** |
||
373 | * insert wish list |
||
374 | * |
||
375 | * @param string $title |
||
376 | * @param string $access |
||
377 | * @param integer $user_id |
||
378 | * @return boolean |
||
379 | */ |
||
380 | public function insertWishList($title, $access, $user_id) { |
||
386 | |||
387 | /** |
||
388 | * update wish list |
||
389 | * |
||
390 | * @param integer $id |
||
391 | * @param array|null $data |
||
392 | * @return boolean |
||
393 | */ |
||
394 | public function updateWishList($id, $data) { |
||
404 | |||
405 | /** |
||
406 | * update wish lists items comments |
||
407 | * |
||
408 | * @param integer $wish_list_id |
||
409 | * @param array $comments |
||
410 | * @return boolean |
||
411 | */ |
||
412 | public function updateWishListItemsComments($wish_list_id, $comments) { |
||
421 | |||
422 | /** |
||
423 | * insert user |
||
424 | * |
||
425 | * @param integer $user_id |
||
426 | * @param string $user_image |
||
427 | * @param integer $user_birthday |
||
428 | * @param string $user_name |
||
429 | * @return boolean |
||
430 | */ |
||
431 | public function insertUser($user_id, $user_image, $user_birthday, $user_name = null) { |
||
441 | |||
442 | /** |
||
443 | * add item to wish list |
||
444 | * |
||
445 | * @param integer $varId |
||
446 | * @param string $listId |
||
447 | * @param string $listName |
||
448 | * @param integer $user_id |
||
449 | * @return boolean |
||
450 | */ |
||
451 | public function addItem($varId, $listId, $listName, $user_id = null) { |
||
472 | |||
473 | /** |
||
474 | * create user wish list if not exist |
||
475 | * |
||
476 | * @param integer $user_id |
||
477 | * @param string $user_name |
||
478 | * @return boolean |
||
479 | */ |
||
480 | public function createUserIfNotExist($user_id, $user_name = null) { |
||
503 | |||
504 | /** |
||
505 | * update user |
||
506 | * |
||
507 | * @param integer $userID |
||
508 | * @param string $user_name |
||
509 | * @param integer $user_birthday |
||
510 | * @param string $description |
||
511 | * @return boolean |
||
512 | */ |
||
513 | public function updateUser($userID, $user_name, $user_birthday, $description) { |
||
520 | |||
521 | /** |
||
522 | * create wish list |
||
523 | * |
||
524 | * @param string $listName |
||
525 | * @param integer $user_id |
||
526 | * @param string $access |
||
527 | * @param string $description |
||
528 | * @return boolean |
||
529 | */ |
||
530 | public function createWishList($listName, $user_id, $access = 'shared', $description) { |
||
553 | |||
554 | /** |
||
555 | * update WishList item |
||
556 | * |
||
557 | * @param integer $varId |
||
558 | * @param integer $wish_list_id |
||
559 | * @param array $data |
||
560 | * @return boolean |
||
561 | */ |
||
562 | public function updateWishListItem($varId, $wish_list_id, $data) { |
||
568 | |||
569 | /** |
||
570 | * get user wish list count |
||
571 | * |
||
572 | * @param integer $user_id |
||
573 | * @return integer |
||
574 | */ |
||
575 | public function getUserWishListCount($user_id) { |
||
585 | |||
586 | /** |
||
587 | * get user wish list items count |
||
588 | * |
||
589 | * @param integer $user_id |
||
590 | * @return integer |
||
591 | */ |
||
592 | public function getUserWishListItemsCount($user_id) { |
||
602 | |||
603 | /** |
||
604 | * add list rewiev |
||
605 | * |
||
606 | * @param string $hash |
||
607 | * @return boolean |
||
608 | */ |
||
609 | public function addReview($hash) { |
||
622 | |||
623 | /** |
||
624 | * get most view wish lists |
||
625 | * |
||
626 | * @param integer $limit |
||
627 | * @return boolean |
||
628 | */ |
||
629 | public function getMostViewedWishLists($limit = 10) { |
||
637 | |||
638 | /** |
||
639 | * |
||
640 | * @param integer $userID |
||
641 | * @param string $file_name |
||
642 | * @return boolean |
||
643 | */ |
||
644 | public function setUserImage($userID, $file_name) { |
||
652 | |||
653 | /** |
||
654 | * |
||
655 | * @param integer $userID |
||
656 | * @return boolean |
||
657 | */ |
||
658 | public function delUser($userID) { |
||
667 | |||
668 | /** |
||
669 | * install module(create db tables, set default values) |
||
670 | */ |
||
671 | public function install() { |
||
794 | |||
795 | public function insertPaterns() { |
||
805 | |||
806 | /** |
||
807 | * deinstall module |
||
808 | */ |
||
809 | public function deinstall() { |
||
823 | |||
824 | } |