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 MartinGroupHandler 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 MartinGroupHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
141 | class MartinGroupHandler extends XoopsObjectHandler |
||
142 | { |
||
143 | /** |
||
144 | * create a new hotel city |
||
145 | * @param bool $isNew flag the new objects as "new"? |
||
146 | * @return object group |
||
147 | */ |
||
148 | public function &create($isNew = true) |
||
157 | |||
158 | /** |
||
159 | * retrieve a hotel city |
||
160 | * |
||
161 | * @param int $id groupid of the group |
||
162 | * @return mixed reference to the {@link group} object, FALSE if failed |
||
163 | */ |
||
164 | View Code Duplication | public function &get($id) |
|
181 | |||
182 | /** |
||
183 | * @get rows |
||
184 | * @license http://www.blags.org/ |
||
185 | * @created :2010年06月20日 13时09分 |
||
186 | * @copyright 1997-2010 The Martin Group |
||
187 | * @author Martin <[email protected]> |
||
188 | * @param $sql |
||
189 | * @param null $key |
||
190 | * @return array |
||
191 | */ |
||
192 | View Code Duplication | public function GetRows($sql, $key = null) |
|
207 | |||
208 | /** |
||
209 | * @得到列表 |
||
210 | * @method: |
||
211 | * @license http://www.blags.org/ |
||
212 | * @created :2010年05月23日 14时59分 |
||
213 | * @copyright 1997-2010 The Martin Group |
||
214 | * @author Martin <[email protected]> |
||
215 | * @param int $limit |
||
216 | * @param int $start |
||
217 | * @param string $sort |
||
218 | * @param string $order |
||
219 | * @param bool $id_as_key |
||
220 | * @return array |
||
221 | */ |
||
222 | View Code Duplication | public function &getGroups($limit = 0, $start = 0, $sort = 'group_add_time', $order = 'DESC', $id_as_key = true) |
|
234 | |||
235 | /** |
||
236 | * insert a new group in the database |
||
237 | * |
||
238 | * @param object $group reference to the {@link group} object |
||
239 | * @param bool $force |
||
240 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
241 | */ |
||
242 | public function insert(&$group, $force = false) |
||
297 | |||
298 | /** |
||
299 | * @删除一个城市 |
||
300 | * @method:delete(group_id) |
||
301 | * @license http://www.blags.org/ |
||
302 | * @created :2010年05月21日 20时40分 |
||
303 | * @copyright 1997-2010 The Martin Group |
||
304 | * @author Martin <[email protected]> |
||
305 | * @param object $group |
||
306 | * @param bool $force |
||
307 | * @return bool|void |
||
308 | */ |
||
309 | View Code Duplication | public function delete(&$group, $force = false) |
|
337 | |||
338 | /** |
||
339 | * delete hotel cities matching a set of conditions |
||
340 | * |
||
341 | * @param object $criteria {@link CriteriaElement} |
||
342 | * @return bool FALSE if deletion failed |
||
343 | */ |
||
344 | View Code Duplication | public function deleteAll($criteria = null) |
|
356 | |||
357 | /** |
||
358 | * count hotel cities matching a condition |
||
359 | * |
||
360 | * @param object $criteria {@link CriteriaElement} to match |
||
361 | * @return int count of categories |
||
362 | */ |
||
363 | View Code Duplication | public function getCount($criteria = null) |
|
377 | |||
378 | /** |
||
379 | * @得到城市 |
||
380 | * @license http://www.blags.org/ |
||
381 | * @created :2010年05月21日 20时40分 |
||
382 | * @copyright 1997-2010 The Martin Group |
||
383 | * @author Martin <[email protected]> |
||
384 | * @param null $criteria |
||
385 | * @param bool $id_as_key |
||
386 | * @return array |
||
387 | */ |
||
388 | View Code Duplication | public function &getObjects($criteria = null, $id_as_key = false) |
|
431 | |||
432 | /** |
||
433 | * @get room list |
||
434 | * @license http://www.blags.org/ |
||
435 | * @created :2010年06月03日 20时05分 |
||
436 | * @copyright 1997-2010 The Martin Group |
||
437 | * @author Martin <[email protected]> |
||
438 | * @param $group_id |
||
439 | * @return array|bool |
||
440 | */ |
||
441 | View Code Duplication | public function getRoomList($group_id) |
|
458 | |||
459 | /** |
||
460 | * @param $group_id |
||
461 | * @param $room_ids |
||
462 | * @param $room_counts |
||
463 | * @param $isNew |
||
464 | * @return bool |
||
465 | */ |
||
466 | View Code Duplication | public function InsertGroupRoom($group_id, $room_ids, $room_counts, $isNew) |
|
491 | |||
492 | /** |
||
493 | * @get room by hotel |
||
494 | * @license http://www.blags.org/ |
||
495 | * @created :2010年06月03日 20时05分 |
||
496 | * @copyright 1997-2010 The Martin Group |
||
497 | * @author Martin <[email protected]> |
||
498 | * @param $hotel_id |
||
499 | * @return array |
||
500 | */ |
||
501 | View Code Duplication | public function GetRoomListByHotel($hotel_id) |
|
514 | |||
515 | /** |
||
516 | * @get top group list |
||
517 | * @license http://www.blags.org/ |
||
518 | * @created :2010年06月20日 13时09分 |
||
519 | * @copyright 1997-2010 The Martin Group |
||
520 | * @author Martin <[email protected]> |
||
521 | * @param int $limit |
||
522 | * @return array |
||
523 | */ |
||
524 | View Code Duplication | public function GetGroupList($limit = 6) |
|
531 | |||
532 | /** |
||
533 | * @get Group rooms |
||
534 | * @license http://www.blags.org/ |
||
535 | * @created :2010年06月20日 13时09分 |
||
536 | * @copyright 1997-2010 The Martin Group |
||
537 | * @author Martin <[email protected]> |
||
538 | * @param $group_id |
||
539 | * @return array |
||
540 | */ |
||
541 | View Code Duplication | public function GetGroupRooms($group_id) |
|
556 | |||
557 | /** |
||
558 | * @add user join group |
||
559 | * @method: |
||
560 | * @license http://www.blags.org/ |
||
561 | * @created :2010年06月22日 20时19分 |
||
562 | * @copyright 1997-2010 The Martin Group |
||
563 | * @author Martin <[email protected]> |
||
564 | * @param $Data |
||
565 | * @return |
||
566 | */ |
||
567 | View Code Duplication | public function AddUserGroup($Data) |
|
585 | |||
586 | /** |
||
587 | * @get group join list |
||
588 | * @method: |
||
589 | * @license http://www.blags.org/ |
||
590 | * @created :2010年06月22日 20时19分 |
||
591 | * @copyright 1997-2010 The Martin Group |
||
592 | * @author Martin <[email protected]> |
||
593 | * @param $group_id |
||
594 | * @return array|bool |
||
595 | */ |
||
596 | View Code Duplication | public function getGroupJoinList($group_id) |
|
609 | |||
610 | /** |
||
611 | * @check group join exist |
||
612 | * @license http://www.blags.org/ |
||
613 | * @created :2010年06月22日 20时19分 |
||
614 | * @copyright 1997-2010 The Martin Group |
||
615 | * @author Martin <[email protected]> |
||
616 | * @param $Data |
||
617 | * @return bool |
||
618 | */ |
||
619 | public function CheckJoinExist($Data) |
||
628 | } |
||
629 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.