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 MartinHotelServiceHandler 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 MartinHotelServiceHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
88 | class MartinHotelServiceHandler extends XoopsObjectHandler |
||
89 | { |
||
90 | /** |
||
91 | * create a new hotel city |
||
92 | * @param bool $isNew flag the new objects as "new"? |
||
93 | * @return object hotelservice |
||
94 | */ |
||
95 | public function &create($isNew = true) |
||
104 | |||
105 | /** |
||
106 | * retrieve a hotel city |
||
107 | * |
||
108 | * @param int $id hotelserviceid of the hotelservice |
||
109 | * @return mixed reference to the {@link hotelservice} object, FALSE if failed |
||
110 | */ |
||
111 | View Code Duplication | public function &get($id) |
|
128 | |||
129 | /** |
||
130 | * @得到列表 |
||
131 | * @method: |
||
132 | * @license http://www.blags.org/ |
||
133 | * @created :2010年05月23日 14时59分 |
||
134 | * @copyright 1997-2010 The Martin Group |
||
135 | * @author Martin <[email protected]> |
||
136 | * @param int $limit |
||
137 | * @param int $start |
||
138 | * @param string $sort |
||
139 | * @param string $order |
||
140 | * @param bool $id_as_key |
||
141 | * @return array |
||
142 | */ |
||
143 | View Code Duplication | public function &getHotelServices($limit = 0, $start = 0, $sort = 'service_id', $order = 'ASC', $id_as_key = true) |
|
155 | |||
156 | /** |
||
157 | * insert a new hotelservice in the database |
||
158 | * |
||
159 | * @param object $hotelservice reference to the {@link hotelservice} object |
||
160 | * @param bool $force |
||
161 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
162 | */ |
||
163 | View Code Duplication | public function insert(&$hotelservice, $force = false) |
|
218 | |||
219 | /** |
||
220 | * @删除一个城市 |
||
221 | * @method:delete(service_id) |
||
222 | * @license http://www.blags.org/ |
||
223 | * @created :2010年05月21日 20时40分 |
||
224 | * @copyright 1997-2010 The Martin Group |
||
225 | * @author Martin <[email protected]> |
||
226 | * @param object $hotelservice |
||
227 | * @param bool $force |
||
228 | * @return bool|void |
||
229 | */ |
||
230 | View Code Duplication | public function delete(&$hotelservice, $force = false) |
|
250 | |||
251 | /** |
||
252 | * delete hotel cities matching a set of conditions |
||
253 | * |
||
254 | * @param object $criteria {@link CriteriaElement} |
||
255 | * @return bool FALSE if deletion failed |
||
256 | */ |
||
257 | View Code Duplication | public function deleteAll($criteria = null) |
|
269 | |||
270 | /** |
||
271 | * count hotel cities matching a condition |
||
272 | * |
||
273 | * @param object $criteria {@link CriteriaElement} to match |
||
274 | * @return int count of categories |
||
275 | */ |
||
276 | View Code Duplication | public function getCount($criteria = null) |
|
290 | |||
291 | /** |
||
292 | * @get objects |
||
293 | * @license http://www.blags.org/ |
||
294 | * @created :2010年05月21日 20时40分 |
||
295 | * @copyright 1997-2010 The Martin Group |
||
296 | * @author Martin <[email protected]> |
||
297 | * @param null $criteria |
||
298 | * @param bool $id_as_key |
||
299 | * @return array |
||
300 | */ |
||
301 | public function &getObjects($criteria = null, $id_as_key = false) |
||
343 | |||
344 | /** |
||
345 | * @get hotel service list |
||
346 | * @license http://www.blags.org/ |
||
347 | * @created :2010年05月30日 20时48分 |
||
348 | * @copyright 1997-2010 The Martin Group |
||
349 | * @author Martin <[email protected]> |
||
350 | * @param $pageSize |
||
351 | * @param $Limit |
||
352 | * @return array |
||
353 | */ |
||
354 | public function getHotelServiceRelations($pageSize, $Limit) |
||
368 | |||
369 | /** |
||
370 | * @get relation |
||
371 | * @license http://www.blags.org/ |
||
372 | * @created :2010年05月30日 20时48分 |
||
373 | * @copyright 1997-2010 The Martin Group |
||
374 | * @author Martin <[email protected]> |
||
375 | * @param $hotel_id |
||
376 | * @param $service_id |
||
377 | * @return bool |
||
378 | */ |
||
379 | public function getHotelServiceRelation($hotel_id, $service_id) |
||
391 | |||
392 | /** |
||
393 | * delete hotel service relation |
||
394 | * @license http://www.blags.org/ |
||
395 | * @created :2010年05月30日 20时48分 |
||
396 | * @copyright 1997-2010 The Martin Group |
||
397 | * @author Martin <[email protected]> |
||
398 | * @param $hotel_id |
||
399 | * @param $service_id |
||
400 | * @return bool |
||
401 | */ |
||
402 | public function DeleteServiceRelation($hotel_id, $service_id) |
||
412 | |||
413 | /** |
||
414 | * @Insert Relation |
||
415 | * @license http://www.blags.org/ |
||
416 | * @created :2010年05月30日 20时48分 |
||
417 | * @copyright 1997-2010 The Martin Group |
||
418 | * @author Martin <[email protected]> |
||
419 | * @param $RelationData |
||
420 | * @param bool $IsOld |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function InsertRelation($RelationData, $IsOld = false) |
||
444 | |||
445 | /** |
||
446 | * 检测是否存在 |
||
447 | * @license http://www.blags.org/ |
||
448 | * @created :2010年05月30日 20时48分 |
||
449 | * @copyright 1997-2010 The Martin Group |
||
450 | * @author Martin <[email protected]> |
||
451 | * @param $hotel_id |
||
452 | * @param $service_id |
||
453 | * @return bool |
||
454 | */ |
||
455 | public function CheckRelationExist($hotel_id, $service_id) |
||
464 | |||
465 | /** |
||
466 | * @get hotel list |
||
467 | * @license http://www.blags.org/ |
||
468 | * @created :2010年05月30日 20时48分 |
||
469 | * @copyright 1997-2010 The Martin Group |
||
470 | * @author Martin <[email protected]> |
||
471 | * @param int $hotel_id |
||
472 | * @return array |
||
473 | */ |
||
474 | View Code Duplication | public function getHotelList($hotel_id = 0) |
|
487 | |||
488 | /** |
||
489 | * @get service list |
||
490 | * @license http://www.blags.org/ |
||
491 | * @created :2010年05月30日 20时48分 |
||
492 | * @copyright 1997-2010 The Martin Group |
||
493 | * @author Martin <[email protected]> |
||
494 | * @param int $service_id |
||
495 | * @return array |
||
496 | */ |
||
497 | View Code Duplication | public function getServiceList($service_id = 0) |
|
509 | |||
510 | /** |
||
511 | * @get relation count |
||
512 | * @license http://www.blags.org/ |
||
513 | * @created :2010年05月30日 20时48分 |
||
514 | * @copyright 1997-2010 The Martin Group |
||
515 | * @author Martin <[email protected]> |
||
516 | * */ |
||
517 | public function GetRelationCount() |
||
528 | |||
529 | /** |
||
530 | * @get hotel service |
||
531 | * @license http://www.blags.org/ |
||
532 | * @created :2010年06月16日 22时31分 |
||
533 | * @copyright 1997-2010 The Martin Group |
||
534 | * @author Martin <[email protected]> |
||
535 | * @param $hotel_id |
||
536 | * @return array|bool |
||
537 | */ |
||
538 | public function getHotelService($hotel_id) |
||
559 | } |
||
560 |
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.