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 ClassifiedsTree 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 ClassifiedsTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ClassifiedsTree |
||
|
|
|||
| 27 | { |
||
| 28 | public $table; |
||
| 29 | public $id; |
||
| 30 | public $pid; |
||
| 31 | public $order; |
||
| 32 | public $title; |
||
| 33 | public $db; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param $table_name |
||
| 37 | * @param $id_name |
||
| 38 | * @param $pid_name |
||
| 39 | */ |
||
| 40 | public function __construct($table_name, $id_name, $pid_name) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param int $sel_id |
||
| 50 | * @param string $order |
||
| 51 | * |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function getFirstChild($sel_id, $order = '') |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param $sel_id |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public function getFirstChildId($sel_id) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param $sel_id |
||
| 109 | * @param string $order |
||
| 110 | * @param array $idarray |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getAllChildId($sel_id, $order = '', $idarray = array()) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $sel_id |
||
| 142 | * @param string $order |
||
| 143 | * @param array $idarray |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function getAllParentId($sel_id, $order = '', $idarray = array()) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param $sel_id |
||
| 172 | * @param $title |
||
| 173 | * @param string $path |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function getPathFromId($sel_id, $title, $path = '') |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param $title |
||
| 207 | * @param string $order |
||
| 208 | * @param int $preset_id |
||
| 209 | * @param int $none |
||
| 210 | * @param string $sel_name |
||
| 211 | * @param string $onchange |
||
| 212 | */ |
||
| 213 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param $sel_id |
||
| 263 | * @param $title |
||
| 264 | * @param $funcURL |
||
| 265 | * @param string $path |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param $sel_id |
||
| 294 | * @param string $path |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function getIdPathFromId($sel_id, $path = '') |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param int $sel_id |
||
| 317 | * @param string $order |
||
| 318 | * @param array $parray |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | public function getAllChild($sel_id = 0, $order = '', $parray = array()) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param int $sel_id |
||
| 350 | * @param string $order |
||
| 351 | * @param array $parray |
||
| 352 | * @param string $r_prefix |
||
| 353 | * |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param $title |
||
| 386 | * @param string $order |
||
| 387 | * @param int $preset_id |
||
| 388 | * @param int $none |
||
| 389 | * @param string $sel_name |
||
| 390 | * @param string $onchange |
||
| 391 | */ |
||
| 392 | public function makeAdSelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 393 | { |
||
| 394 | global $myts, $xoopsDB; |
||
| 395 | $pathIcon16 = \Xmf\Module\Admin::iconUrl('', 16); |
||
| 396 | // require XOOPS_ROOT_PATH . '/modules/adslight/include/gtickets.php'; |
||
| 397 | |||
| 398 | if ('' == $sel_name) { |
||
| 399 | $sel_name = $this->id; |
||
| 400 | } |
||
| 401 | |||
| 402 | $sql = 'SELECT ' . $this->id . ', ' . $title . ', cat_order FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
| 403 | if ('' != $order) { |
||
| 404 | $sql .= " ORDER BY {$order}"; |
||
| 405 | } |
||
| 406 | $result = $xoopsDB->query($sql); |
||
| 407 | while (list($catid, $name, $cat_order) = $xoopsDB->fetchRow($result)) { |
||
| 408 | echo '<table class="width100 bnone outer"><tr> |
||
| 409 | <th class="left">'; |
||
| 410 | if ('cat_order' === $GLOBALS['xoopsModuleConfig']['adslight_csortorder']) { |
||
| 411 | echo "({$cat_order})"; |
||
| 412 | } |
||
| 413 | echo " {$name} </th> |
||
| 414 | <th class=\"center width10\"><a href=\"category.php?op=AdsNewCat&cid={$catid}\"><img src=\"{$pathIcon16}/add.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" |
||
| 415 | . _AM_ADSLIGHT_ADDSUBCAT |
||
| 416 | . "\" title=\"" |
||
| 417 | . _AM_ADSLIGHT_ADDSUBCAT |
||
| 418 | . "\"></a></th> |
||
| 419 | <th class=\"center width10\"><a href=\"category.php?op=AdsModCat&cid={$catid}\"><img src=\"{$pathIcon16}/edit.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" |
||
| 420 | . _AM_ADSLIGHT_MODIFSUBCAT |
||
| 421 | . "\" title=\"" |
||
| 422 | . _AM_ADSLIGHT_MODIFSUBCAT |
||
| 423 | . "\"></a></th> |
||
| 424 | <th class=\"center width10\"><a href=\"category.php?op=AdsDelCat&cid={$catid}\"><img src=\"{$pathIcon16}/delete.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" |
||
| 425 | . _AM_ADSLIGHT_DELSUBCAT |
||
| 426 | . "\" title=\"" |
||
| 427 | . _AM_ADSLIGHT_DELSUBCAT |
||
| 428 | . "\"></a></th> |
||
| 429 | </tr>"; |
||
| 430 | |||
| 431 | $arr = $this->getChildTreeMapArray($catid, $order); |
||
| 432 | $class = 'odd'; |
||
| 433 | foreach ($arr as $option) { |
||
| 434 | echo "<tr class=\"{$class}\"><td>"; |
||
| 435 | |||
| 436 | $option['prefix'] = str_replace('.', ' - ', $option['prefix']); |
||
| 437 | $catpath = $option['prefix'] . ' ' . $myts->htmlSpecialChars($option[$title]); |
||
| 438 | $cat_orderS = $option['cat_order']; |
||
| 439 | if ('cat_order' == $GLOBALS['xoopsModuleConfig']['adslight_csortorder']) { |
||
| 440 | echo "({$cat_orderS})"; |
||
| 441 | } |
||
| 442 | echo '' |
||
| 443 | . $catpath |
||
| 444 | . '</a></td> |
||
| 445 | <td align="center"><a href="category.php?op=AdsNewCat&cid=' |
||
| 446 | . $option[$this->id] |
||
| 447 | . '"><img src="' |
||
| 448 | . $pathIcon16 |
||
| 449 | . '/add.png' |
||
| 450 | . '" border=0 width=18 height=18 alt="' |
||
| 451 | . _AM_ADSLIGHT_ADDSUBCAT |
||
| 452 | . '"title="' |
||
| 453 | . _AM_ADSLIGHT_ADDSUBCAT |
||
| 454 | . '"></a></td> |
||
| 455 | <td align="center"><a href="category.php?op=AdsModCat&cid=' |
||
| 456 | . $option[$this->id] |
||
| 457 | . '"><img src="' |
||
| 458 | . $pathIcon16 |
||
| 459 | . '/edit.png' |
||
| 460 | . '" border=0 width=18 height=18 alt="' |
||
| 461 | . _AM_ADSLIGHT_MODIFSUBCAT |
||
| 462 | . '" title ="' |
||
| 463 | . _AM_ADSLIGHT_MODIFSUBCAT |
||
| 464 | . '"></a></td> |
||
| 465 | <td align="center"><a href="category.php?op=AdsDelCat&cid=' |
||
| 466 | . $option[$this->id] |
||
| 467 | . '"><img src="' |
||
| 468 | . $pathIcon16 |
||
| 469 | . '/delete.png' |
||
| 470 | . '" border=0 width=18 height=18 alt="' |
||
| 471 | . _AM_ADSLIGHT_DELSUBCAT |
||
| 472 | . '" title="' |
||
| 473 | . _AM_ADSLIGHT_DELSUBCAT |
||
| 474 | . '"></a></td>'; |
||
| 475 | |||
| 476 | $class = ('even' == $class) ? 'odd' : 'even'; |
||
| 477 | } |
||
| 478 | echo '</td></tr></table><br>'; |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param int $sel_id |
||
| 484 | * @param string $order |
||
| 485 | * @param array $parray |
||
| 486 | * @param string $r_prefix |
||
| 487 | * |
||
| 488 | * @return array |
||
| 489 | */ |
||
| 490 | public function getChildTreeMapArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return array |
||
| 519 | */ |
||
| 520 | public function getCategoryList() |
||
| 534 | } |
||
| 535 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.