| Total Complexity | 73 |
| Total Lines | 466 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 namespace XoopsModules\Adslight; |
||
| 28 | class ClassifiedsTree |
||
| 29 | { |
||
| 30 | public $table; |
||
| 31 | public $id; |
||
| 32 | public $pid; |
||
| 33 | public $order; |
||
| 34 | public $title; |
||
| 35 | public $db; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param $table_name |
||
| 39 | * @param $id_name |
||
| 40 | * @param $pid_name |
||
| 41 | */ |
||
| 42 | public function __construct($table_name, $id_name, $pid_name) |
||
| 43 | { |
||
| 44 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
|
|
|||
| 45 | $this->table = $table_name; |
||
| 46 | $this->id = $id_name; |
||
| 47 | $this->pid = $pid_name; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param int $sel_id |
||
| 52 | * @param string $order |
||
| 53 | * |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | public function getFirstChild($sel_id, $order = '') |
||
| 57 | { |
||
| 58 | $arr = []; |
||
| 59 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . (int)$sel_id . ''; |
||
| 60 | |||
| 61 | $categories = Adslight\Utility::getMyItemIds('adslight_view'); |
||
| 62 | if (is_array($categories) && count($categories) > 0) { |
||
| 63 | $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') '; |
||
| 64 | } |
||
| 65 | |||
| 66 | if ('' != $order) { |
||
| 67 | $sql .= " ORDER BY $order"; |
||
| 68 | } |
||
| 69 | |||
| 70 | $result = $this->db->query($sql); |
||
| 71 | $count = $this->db->getRowsNum($result); |
||
| 72 | if (0 == $count) { |
||
| 73 | return $arr; |
||
| 74 | } |
||
| 75 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 76 | array_push($arr, $myrow); |
||
| 77 | } |
||
| 78 | |||
| 79 | return $arr; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param $sel_id |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function getFirstChildId($sel_id) |
||
| 88 | { |
||
| 89 | $idarray = []; |
||
| 90 | $sel_id = (int)$sel_id; |
||
| 91 | $result = $this->db->query('SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id); |
||
| 92 | |||
| 93 | $categories = Adslight\Utility::getMyItemIds('adslight_view'); |
||
| 94 | if (is_array($categories) && count($categories) > 0) { |
||
| 95 | $result .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') '; |
||
| 96 | } |
||
| 97 | |||
| 98 | $count = $this->db->getRowsNum($result); |
||
| 99 | if (0 == $count) { |
||
| 100 | return $idarray; |
||
| 101 | } |
||
| 102 | while (list($id) = $this->db->fetchRow($result)) { |
||
| 103 | array_push($idarray, $id); |
||
| 104 | } |
||
| 105 | |||
| 106 | return $idarray; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param $sel_id |
||
| 111 | * @param string $order |
||
| 112 | * @param array $idarray |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getAllChildId($sel_id, $order = '', $idarray = []) |
||
| 117 | { |
||
| 118 | $sel_id = (int)$sel_id; |
||
| 119 | $sql = 'SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
| 120 | |||
| 121 | $categories = Adslight\Utility::getMyItemIds('adslight_view'); |
||
| 122 | if (is_array($categories) && count($categories) > 0) { |
||
| 123 | $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') '; |
||
| 124 | } |
||
| 125 | |||
| 126 | if ('' != $order) { |
||
| 127 | $sql .= " ORDER BY {$order}"; |
||
| 128 | } |
||
| 129 | $result = $this->db->query($sql); |
||
| 130 | $count = $this->db->getRowsNum($result); |
||
| 131 | if (0 == $count) { |
||
| 132 | return $idarray; |
||
| 133 | } |
||
| 134 | while (list($r_id) = $this->db->fetchRow($result)) { |
||
| 135 | array_push($idarray, $r_id); |
||
| 136 | $idarray = $this->getAllChildId($r_id, $order, $idarray); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $idarray; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param $sel_id |
||
| 144 | * @param string $order |
||
| 145 | * @param array $idarray |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function getAllParentId($sel_id, $order = '', $idarray = []) |
||
| 150 | { |
||
| 151 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id; |
||
| 152 | |||
| 153 | $categories = Adslight\Utility::getMyItemIds('adslight_view'); |
||
| 154 | if (is_array($categories) && count($categories) > 0) { |
||
| 155 | $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') '; |
||
| 156 | } |
||
| 157 | |||
| 158 | if ('' != $order) { |
||
| 159 | $sql .= " ORDER BY {$order}"; |
||
| 160 | } |
||
| 161 | $result = $this->db->query($sql); |
||
| 162 | list($r_id) = $this->db->fetchRow($result); |
||
| 163 | if (0 == $r_id) { |
||
| 164 | return $idarray; |
||
| 165 | } |
||
| 166 | array_push($idarray, $r_id); |
||
| 167 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
| 168 | |||
| 169 | return $idarray; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param $sel_id |
||
| 174 | * @param $title |
||
| 175 | * @param string $path |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getPathFromId($sel_id, $title, $path = '') |
||
| 180 | { |
||
| 181 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id . ''; |
||
| 182 | // $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id) . "'"); |
||
| 183 | |||
| 184 | $categories = Adslight\Utility::getMyItemIds('adslight_view'); |
||
| 185 | if (is_array($categories) && count($categories) > 0) { |
||
| 186 | // $result .= ' AND cid IN (' . implode(',', $categories) . ') '; |
||
| 187 | $sql .= ' AND cid IN (' . implode(',', $categories) . ') '; |
||
| 188 | } |
||
| 189 | |||
| 190 | $result = $this->db->query($sql); |
||
| 191 | |||
| 192 | if (0 == $this->db->getRowsNum($result)) { |
||
| 193 | return $path; |
||
| 194 | } |
||
| 195 | list($parentid, $name) = $this->db->fetchRow($result); |
||
| 196 | $myts = \MyTextSanitizer::getInstance(); |
||
| 197 | $name = $myts->htmlSpecialChars($name); |
||
| 198 | $path = '/' . $name . $path . ''; |
||
| 199 | if (0 == $parentid) { |
||
| 200 | return $path; |
||
| 201 | } |
||
| 202 | $path = $this->getPathFromId($parentid, $title, $path); |
||
| 203 | |||
| 204 | return $path; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param $title |
||
| 209 | * @param string $order |
||
| 210 | * @param int $preset_id |
||
| 211 | * @param int $none |
||
| 212 | * @param string $sel_name |
||
| 213 | * @param string $onchange |
||
| 214 | */ |
||
| 215 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param $sel_id |
||
| 265 | * @param $title |
||
| 266 | * @param $funcURL |
||
| 267 | * @param string $path |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
| 272 | { |
||
| 273 | $sql = 'SELECT SQL_CACHE ' . $this->pid . ", {$title} FROM " . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id; |
||
| 274 | $result = $this->db->query($sql); |
||
| 275 | if (0 == $this->db->getRowsNum($result)) { |
||
| 276 | return $path; |
||
| 277 | } |
||
| 278 | list($parentid, $name) = $this->db->fetchRow($result); |
||
| 279 | $myts = \MyTextSanitizer::getInstance(); |
||
| 280 | $name = $myts->htmlSpecialChars($name); |
||
| 281 | |||
| 282 | $arrow = '<img src="' . XOOPS_URL . '/modules/adslight/assets/images/arrow.gif" alt="»" >'; |
||
| 283 | |||
| 284 | $path = " {$arrow} <a title=\"" . _ADSLIGHT_ANNONCES . " {$name}\" href=\"{$funcURL}" . $this->id . '=' . (int)$sel_id . "\">{$name}</a>{$path}"; |
||
| 285 | |||
| 286 | if (0 == $parentid) { |
||
| 287 | return $path; |
||
| 288 | } |
||
| 289 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
||
| 290 | |||
| 291 | return $path; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param $sel_id |
||
| 296 | * @param string $path |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getIdPathFromId($sel_id, $path = '') |
||
| 301 | { |
||
| 302 | $sel_id = (int)$sel_id; |
||
| 303 | $result = $this->db->query('SELECT SQL_CACHE ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id); |
||
| 304 | if (0 == $this->db->getRowsNum($result)) { |
||
| 305 | return $path; |
||
| 306 | } |
||
| 307 | list($parentid) = $this->db->fetchRow($result); |
||
| 308 | $path = "/{$sel_id}{$path}"; |
||
| 309 | if (0 == $parentid) { |
||
| 310 | return $path; |
||
| 311 | } |
||
| 312 | $path = $this->getIdPathFromId($parentid, $path); |
||
| 313 | |||
| 314 | return $path; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param int $sel_id |
||
| 319 | * @param string $order |
||
| 320 | * @param array $parray |
||
| 321 | * |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | public function getAllChild($sel_id = 0, $order = '', $parray = []) |
||
| 325 | { |
||
| 326 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . (int)$sel_id; |
||
| 327 | |||
| 328 | $categories = Adslight\Utility::getMyItemIds('adslight_view'); |
||
| 329 | if (is_array($categories) && count($categories) > 0) { |
||
| 330 | $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') '; |
||
| 331 | } |
||
| 332 | |||
| 333 | if ('' != $order) { |
||
| 334 | $sql .= " ORDER BY {$order}"; |
||
| 335 | } |
||
| 336 | |||
| 337 | $result = $this->db->query($sql); |
||
| 338 | $count = $this->db->getRowsNum($result); |
||
| 339 | if (0 == $count) { |
||
| 340 | return $parray; |
||
| 341 | } |
||
| 342 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 343 | array_push($parray, $row); |
||
| 344 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
| 345 | } |
||
| 346 | |||
| 347 | return $parray; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param int $sel_id |
||
| 352 | * @param string $order |
||
| 353 | * @param array $parray |
||
| 354 | * @param string $r_prefix |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '') |
||
| 359 | { |
||
| 360 | global $moduleDirName; |
||
| 361 | |||
| 362 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . (int)$sel_id; |
||
| 363 | |||
| 364 | $categories = Adslight\Utility::getMyItemIds('adslight_view'); |
||
| 365 | if (is_array($categories) && count($categories) > 0) { |
||
| 366 | $sql .= ' AND cid IN (' . implode(',', $categories) . ') '; |
||
| 367 | } |
||
| 368 | |||
| 369 | if ('' != $order) { |
||
| 370 | $sql .= " ORDER BY {$order}"; |
||
| 371 | } |
||
| 372 | $result = $this->db->query($sql); |
||
| 373 | $count = $this->db->getRowsNum($result); |
||
| 374 | if (0 == $count) { |
||
| 375 | return $parray; |
||
| 376 | } |
||
| 377 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 378 | $row['prefix'] = $r_prefix . '.'; |
||
| 379 | array_push($parray, $row); |
||
| 380 | $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']); |
||
| 381 | } |
||
| 382 | |||
| 383 | return $parray; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param $title |
||
| 388 | * @param string $order |
||
| 389 | * @param int $preset_id |
||
| 390 | * @param int $none |
||
| 391 | * @param string $sel_name |
||
| 392 | * @param string $onchange |
||
| 393 | */ |
||
| 394 | public function makeAdSelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 395 | { |
||
| 396 | global $myts, $xoopsDB; |
||
| 397 | $pathIcon16 = \Xmf\Module\Admin::iconUrl('', 16); |
||
| 398 | // require XOOPS_ROOT_PATH . '/modules/adslight/include/gtickets.php'; |
||
| 399 | |||
| 400 | if ('' == $sel_name) { |
||
| 401 | $sel_name = $this->id; |
||
| 402 | } |
||
| 403 | |||
| 404 | $sql = 'SELECT ' . $this->id . ', ' . $title . ', cat_order FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
| 405 | if ('' != $order) { |
||
| 406 | $sql .= " ORDER BY {$order}"; |
||
| 407 | } |
||
| 408 | $result = $xoopsDB->query($sql); |
||
| 409 | while (list($catid, $name, $cat_order) = $xoopsDB->fetchRow($result)) { |
||
| 410 | echo '<table class="width100 bnone outer"><tr> |
||
| 411 | <th class="left">'; |
||
| 412 | if ('cat_order' === $GLOBALS['xoopsModuleConfig']['adslight_csortorder']) { |
||
| 413 | echo "({$cat_order})"; |
||
| 414 | } |
||
| 415 | echo " {$name} </th> |
||
| 416 | <th class=\"center width10\"><a href=\"category.php?op=AdsNewCat&cid={$catid}\"><img src=\"{$pathIcon16}/add.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" . _AM_ADSLIGHT_ADDSUBCAT . '" title="' . _AM_ADSLIGHT_ADDSUBCAT . "\"></a></th> |
||
| 417 | <th class=\"center width10\"><a href=\"category.php?op=AdsModCat&cid={$catid}\"><img src=\"{$pathIcon16}/edit.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" . _AM_ADSLIGHT_MODIFSUBCAT . '" title="' . _AM_ADSLIGHT_MODIFSUBCAT . "\"></a></th> |
||
| 418 | <th class=\"center width10\"><a href=\"category.php?op=AdsDelCat&cid={$catid}\"><img src=\"{$pathIcon16}/delete.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" . _AM_ADSLIGHT_DELSUBCAT . '" title="' . _AM_ADSLIGHT_DELSUBCAT . '"></a></th> |
||
| 419 | </tr>'; |
||
| 420 | |||
| 421 | $arr = $this->getChildTreeMapArray($catid, $order); |
||
| 422 | $class = 'odd'; |
||
| 423 | foreach ($arr as $option) { |
||
| 424 | echo "<tr class=\"{$class}\"><td>"; |
||
| 425 | |||
| 426 | $option['prefix'] = str_replace('.', ' - ', $option['prefix']); |
||
| 427 | $catpath = $option['prefix'] . ' ' . $myts->htmlSpecialChars($option[$title]); |
||
| 428 | $cat_orderS = $option['cat_order']; |
||
| 429 | if ('cat_order' == $GLOBALS['xoopsModuleConfig']['adslight_csortorder']) { |
||
| 430 | echo "({$cat_orderS})"; |
||
| 431 | } |
||
| 432 | echo '' . $catpath . '</a></td> |
||
| 433 | <td align="center"><a href="category.php?op=AdsNewCat&cid=' . $option[$this->id] . '"><img src="' . $pathIcon16 . '/add.png' . '" border=0 width=18 height=18 alt="' . _AM_ADSLIGHT_ADDSUBCAT . '"title="' . _AM_ADSLIGHT_ADDSUBCAT . '"></a></td> |
||
| 434 | <td align="center"><a href="category.php?op=AdsModCat&cid=' . $option[$this->id] . '"><img src="' . $pathIcon16 . '/edit.png' . '" border=0 width=18 height=18 alt="' . _AM_ADSLIGHT_MODIFSUBCAT . '" title ="' . _AM_ADSLIGHT_MODIFSUBCAT . '"></a></td> |
||
| 435 | <td align="center"><a href="category.php?op=AdsDelCat&cid=' . $option[$this->id] . '"><img src="' . $pathIcon16 . '/delete.png' . '" border=0 width=18 height=18 alt="' . _AM_ADSLIGHT_DELSUBCAT . '" title="' . _AM_ADSLIGHT_DELSUBCAT . '"></a></td>'; |
||
| 436 | |||
| 437 | $class = ('even' == $class) ? 'odd' : 'even'; |
||
| 438 | } |
||
| 439 | echo '</td></tr></table><br>'; |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param int $sel_id |
||
| 445 | * @param string $order |
||
| 446 | * @param array $parray |
||
| 447 | * @param string $r_prefix |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | public function getChildTreeMapArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '') |
||
| 452 | { |
||
| 453 | global $xoopsDB; |
||
| 454 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . (int)$sel_id . ''; |
||
| 455 | |||
| 456 | $categories = Adslight\Utility::getMyItemIds('adslight_view'); |
||
| 457 | if (is_array($categories) && count($categories) > 0) { |
||
| 458 | $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') '; |
||
| 459 | } |
||
| 460 | |||
| 461 | if ('' != $order) { |
||
| 462 | $sql .= " ORDER BY {$order}"; |
||
| 463 | } |
||
| 464 | $result = $xoopsDB->query($sql); |
||
| 465 | $count = $xoopsDB->getRowsNum($result); |
||
| 466 | if (0 == $count) { |
||
| 467 | return $parray; |
||
| 468 | } |
||
| 469 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 470 | $row['prefix'] = $r_prefix . '.'; |
||
| 471 | array_push($parray, $row); |
||
| 472 | $parray = $this->getChildTreeMapArray($row[$this->id], $order, $parray, $row['prefix']); |
||
| 473 | } |
||
| 474 | |||
| 475 | return $parray; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | public function getCategoryList() |
||
| 494 | } |
||
| 495 | } |
||
| 496 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths