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 Todo 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 Todo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Todo { |
||
9 | |||
10 | private $user, $pass, $uid; //For user registration |
||
11 | |||
12 | /** |
||
13 | * @var MY_Controller |
||
14 | */ |
||
15 | protected $CI; |
||
16 | |||
17 | /** |
||
18 | * Constructor |
||
19 | */ |
||
20 | 2 | public function __construct() |
|
24 | |||
25 | // -------------------------------------------------------------------------- |
||
26 | |||
27 | /** |
||
28 | * Get User From Id |
||
29 | * |
||
30 | * Retrieve a user's username from their userid |
||
31 | * @param int $user_id |
||
32 | * @return string |
||
33 | */ |
||
34 | 10 | public function get_user_from_id($user_id) |
|
44 | |||
45 | // -------------------------------------------------------------------------- |
||
46 | |||
47 | /** |
||
48 | * Crypt Pass |
||
49 | * |
||
50 | * Hashes passwords |
||
51 | * @param string $password |
||
52 | * @return string |
||
53 | */ |
||
54 | 1 | public function crypt_pass($password) |
|
58 | |||
59 | // -------------------------------------------------------------------------- |
||
60 | |||
61 | /** |
||
62 | * Add Reg |
||
63 | * |
||
64 | * Submits a new user to the database |
||
65 | * @return integer |
||
66 | */ |
||
67 | public function add_reg() |
||
110 | |||
111 | // -------------------------------------------------------------------------- |
||
112 | |||
113 | /** |
||
114 | * Get Categories |
||
115 | * |
||
116 | * Retrieves list of category types from the database |
||
117 | * @return array |
||
118 | */ |
||
119 | 3 | View Code Duplication | public function get_category_list() |
132 | |||
133 | // -------------------------------------------------------------------------- |
||
134 | |||
135 | /** |
||
136 | * Get Group List |
||
137 | * |
||
138 | * An alias for the private get_groups method |
||
139 | * @param int |
||
140 | * @return array |
||
141 | */ |
||
142 | 2 | public function get_group_list($user_id) |
|
146 | |||
147 | // -------------------------------------------------------------------------- |
||
148 | |||
149 | /** |
||
150 | * Add Category |
||
151 | * |
||
152 | * Submits a new category to the database |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function add_category() |
||
193 | |||
194 | // -------------------------------------------------------------------------- |
||
195 | |||
196 | /** |
||
197 | * Add Group |
||
198 | * |
||
199 | * Submits a new group to the database |
||
200 | * @return bool |
||
201 | */ |
||
202 | public function add_group() |
||
230 | |||
231 | // -------------------------------------------------------------------------- |
||
232 | |||
233 | /** |
||
234 | * Get Category Select |
||
235 | * |
||
236 | * Generates select options for categories when adding a new task |
||
237 | * @return string |
||
238 | */ |
||
239 | 2 | View Code Duplication | public function get_category_select() |
251 | |||
252 | // -------------------------------------------------------------------------- |
||
253 | |||
254 | /** |
||
255 | * Get Priority Select |
||
256 | * |
||
257 | * Generates select options for priorities when adding a new task |
||
258 | * @return string |
||
259 | */ |
||
260 | 1 | public function get_priority_select() |
|
274 | |||
275 | // -------------------------------------------------------------------------- |
||
276 | |||
277 | /** |
||
278 | * Get Group Select |
||
279 | * |
||
280 | * Generates select options for groups when adding a friend |
||
281 | * @param int $user_id |
||
282 | * @return string |
||
283 | */ |
||
284 | 2 | View Code Duplication | public function get_group_select($user_id) |
296 | |||
297 | // -------------------------------------------------------------------------- |
||
298 | |||
299 | /** |
||
300 | * Validate Pass |
||
301 | * |
||
302 | * Validate Password Change |
||
303 | * @return mixed |
||
304 | */ |
||
305 | 3 | public function validate_pass() |
|
339 | |||
340 | // -------------------------------------------------------------------------- |
||
341 | |||
342 | /** |
||
343 | * Update Pass |
||
344 | * |
||
345 | * Updates user's password in the database |
||
346 | */ |
||
347 | public function update_pass() |
||
354 | |||
355 | // -------------------------------------------------------------------------- |
||
356 | |||
357 | /** |
||
358 | * Redirect 303 |
||
359 | * |
||
360 | * Shortcut function for 303 redirect |
||
361 | * @param string $url |
||
362 | */ |
||
363 | 2 | public function redirect_303($url) |
|
373 | |||
374 | // -------------------------------------------------------------------------- |
||
375 | |||
376 | /** |
||
377 | * Set Timezone |
||
378 | * |
||
379 | * Sets the timezone based on the user's settings |
||
380 | * @param int $uid |
||
381 | * @param string $timezone |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function set_timezone($uid, $timezone) |
||
392 | |||
393 | // -------------------------------------------------------------------------- |
||
394 | |||
395 | /** |
||
396 | * Get Priorities |
||
397 | * |
||
398 | * Retreives list of priority types from the database |
||
399 | * @return array |
||
400 | */ |
||
401 | 2 | public function get_priorities() |
|
410 | |||
411 | // -------------------------------------------------------------------------- |
||
412 | |||
413 | /** |
||
414 | * Get Groups |
||
415 | * |
||
416 | * Retrieves user's groups from db |
||
417 | * @param int $user_id |
||
418 | * @return array |
||
419 | */ |
||
420 | 4 | View Code Duplication | private function get_groups($user_id) |
434 | |||
435 | // -------------------------------------------------------------------------- |
||
436 | |||
437 | /** |
||
438 | * Get User Account By Id |
||
439 | * |
||
440 | * Retrieves user's account info from db |
||
441 | * @param int $user_id |
||
442 | * @return array |
||
443 | */ |
||
444 | 3 | public function get_user_account_by_id($user_id) |
|
462 | |||
463 | // -------------------------------------------------------------------------- |
||
464 | |||
465 | /** |
||
466 | * Get User Group |
||
467 | * |
||
468 | * Gets the current user's primary group |
||
469 | * @return int |
||
470 | */ |
||
471 | 5 | public function get_user_group() |
|
489 | |||
490 | // -------------------------------------------------------------------------- |
||
491 | |||
492 | /** |
||
493 | * Get Friend List |
||
494 | * |
||
495 | * Gets the friends of the current user |
||
496 | * @return array |
||
497 | */ |
||
498 | 3 | public function get_friend_list() |
|
531 | |||
532 | // -------------------------------------------------------------------------- |
||
533 | |||
534 | /** |
||
535 | * Get Friends in Group |
||
536 | * |
||
537 | * Returns members of a group |
||
538 | * @param int $group_id |
||
539 | * @return array |
||
540 | */ |
||
541 | 1 | public function get_friends_in_group($group_id) |
|
552 | |||
553 | // -------------------------------------------------------------------------- |
||
554 | |||
555 | /** |
||
556 | * Update group |
||
557 | * |
||
558 | * Updates a group's membership |
||
559 | */ |
||
560 | public function update_group() |
||
587 | |||
588 | // -------------------------------------------------------------------------- |
||
589 | |||
590 | /** |
||
591 | * Del group |
||
592 | * |
||
593 | * Deletes a friend group |
||
594 | * @param int $group_id |
||
595 | * @return int |
||
596 | */ |
||
597 | public function del_group($group_id) |
||
625 | |||
626 | // -------------------------------------------------------------------------- |
||
627 | |||
628 | /** |
||
629 | * Del Cat |
||
630 | * |
||
631 | * Deletes a task category |
||
632 | * @param int $cat_id |
||
633 | * @return int |
||
634 | */ |
||
635 | public function del_cat($cat_id) |
||
654 | |||
655 | // -------------------------------------------------------------------------- |
||
656 | |||
657 | /** |
||
658 | * Get group name by id |
||
659 | * |
||
660 | * Gets a group name from the group id |
||
661 | * @param int $group_id |
||
662 | * @return string |
||
663 | */ |
||
664 | 2 | public function get_group_name_by_id($group_id) |
|
675 | |||
676 | |||
677 | // -------------------------------------------------------------------------- |
||
678 | |||
679 | /** |
||
680 | * Kanji Num |
||
681 | * |
||
682 | * Converts arabic to chinese number |
||
683 | * @param int $orig_number |
||
684 | * @return string |
||
685 | */ |
||
686 | 11 | public function kanji_num($orig_number) |
|
754 | |||
755 | // -------------------------------------------------------------------------- |
||
756 | |||
757 | /** |
||
758 | * Get Category |
||
759 | * |
||
760 | * Returns a category from id |
||
761 | * @param int $cat_id |
||
762 | * @return array |
||
763 | */ |
||
764 | 3 | public function get_category($cat_id) |
|
776 | |||
777 | // -------------------------------------------------------------------------- |
||
778 | |||
779 | /** |
||
780 | * Get Friend Requests |
||
781 | * |
||
782 | * Retrieves number of friend requests for the current user |
||
783 | * @return int |
||
784 | */ |
||
785 | 1 | public function get_friend_requests() |
|
803 | |||
804 | /** |
||
805 | * Authenticate the user |
||
806 | * |
||
807 | * @return string |
||
808 | */ |
||
809 | 3 | public function verify_user() |
|
844 | } |
||
845 | // End of libraries/Todo.php |