| Total Complexity | 86 |
| Total Lines | 495 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Tree 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 Tree, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 30 | class Tree |
||
| 31 | { |
||
| 32 | public $table; |
||
| 33 | public $id; |
||
| 34 | public $pid; |
||
| 35 | public $order; |
||
| 36 | public $title; |
||
| 37 | /** |
||
| 38 | * @var \XoopsMySQLDatabase |
||
| 39 | */ |
||
| 40 | public $db; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param $table_name |
||
| 44 | * @param $id_name |
||
| 45 | * @param $pid_name |
||
| 46 | */ |
||
| 47 | public function __construct( |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param int $sel_id |
||
| 62 | * @param string $order |
||
| 63 | */ |
||
| 64 | public function getFirstChild($sel_id, $order = ''): array |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param $sel_id |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function getFirstChildId($sel_id): array |
||
| 98 | { |
||
| 99 | $idarray = []; |
||
| 100 | $sel_id = (int)$sel_id; |
||
| 101 | $sql = 'SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
| 102 | $result = $this->db->query($sql); |
||
| 103 | if (!$this->db->isResultSet($result)) { |
||
| 104 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 105 | } |
||
| 106 | |||
| 107 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 108 | if (\is_array($categories) && $categories !== []) { |
||
| 109 | $result .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 110 | } |
||
| 111 | |||
| 112 | $count = $this->db->getRowsNum($result); |
||
| 113 | if (0 === $count) { |
||
| 114 | return $idarray; |
||
| 115 | } |
||
| 116 | while (false !== [$id] = $this->db->fetchRow($result)) { |
||
| 117 | $idarray[] = $id; |
||
| 118 | } |
||
| 119 | |||
| 120 | return $idarray; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param $sel_id |
||
| 125 | * @param string $order |
||
| 126 | * @param array $idarray |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public function getAllChildId($sel_id, $order = '', $idarray = []): array |
||
| 130 | { |
||
| 131 | $sel_id = (int)$sel_id; |
||
| 132 | $sql = 'SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
| 133 | |||
| 134 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 135 | if (\is_array($categories) && $categories !== []) { |
||
| 136 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ('' !== $order) { |
||
| 140 | $sql .= " ORDER BY {$order}"; |
||
| 141 | } |
||
| 142 | $result = $this->db->query($sql); |
||
| 143 | if (!$this->db->isResultSet($result)) { |
||
| 144 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), \E_USER_ERROR); |
||
| 145 | } |
||
| 146 | $count = $this->db->getRowsNum($result); |
||
| 147 | if (0 === $count) { |
||
| 148 | return $idarray; |
||
| 149 | } |
||
| 150 | while (false !== [$r_id] = $this->db->fetchRow($result)) { |
||
| 151 | $idarray[] = $r_id; |
||
| 152 | $idarray = $this->getAllChildId($r_id, $order, $idarray); |
||
| 153 | } |
||
| 154 | |||
| 155 | return $idarray; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param $sel_id |
||
| 160 | * @param string $order |
||
| 161 | * @param array $idarray |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function getAllParentId($sel_id, $order = '', $idarray = []): array |
||
| 165 | { |
||
| 166 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id; |
||
| 167 | |||
| 168 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 169 | if (\is_array($categories) && $categories !== []) { |
||
| 170 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 171 | } |
||
| 172 | |||
| 173 | if ('' !== $order) { |
||
| 174 | $sql .= " ORDER BY {$order}"; |
||
| 175 | } |
||
| 176 | $result = $this->db->query($sql); |
||
| 177 | if (!$this->db->isResultSet($result)) { |
||
| 178 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 179 | } |
||
| 180 | [$r_id] = $this->db->fetchRow($result); |
||
| 181 | if (0 === $r_id) { |
||
| 182 | return $idarray; |
||
| 183 | } |
||
| 184 | $idarray[] = $r_id; |
||
| 185 | |||
| 186 | return $this->getAllParentId($r_id, $order, $idarray); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param $sel_id |
||
| 191 | * @param $title |
||
| 192 | * @param string $path |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function getPathFromId($sel_id, $title, $path = ''): string |
||
| 196 | { |
||
| 197 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id . ''; |
||
| 198 | // $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id) . "'"); |
||
| 199 | |||
| 200 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 201 | if (\is_array($categories) && $categories !== []) { |
||
| 202 | // $result .= ' AND cid IN (' . implode(',', $categories) . ') '; |
||
| 203 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
| 204 | } |
||
| 205 | |||
| 206 | $result = $this->db->query($sql); |
||
| 207 | if (!$this->db->isResultSet($result)) { |
||
| 208 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 209 | } |
||
| 210 | |||
| 211 | if (0 === $this->db->getRowsNum($result)) { |
||
| 212 | return $path; |
||
| 213 | } |
||
| 214 | [$parentid, $name] = $this->db->fetchRow($result); |
||
| 215 | \MyTextSanitizer::getInstance(); |
||
| 216 | $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5); |
||
| 217 | $path = '/' . $name . $path . ''; |
||
| 218 | if (0 === $parentid) { |
||
| 219 | return $path; |
||
| 220 | } |
||
| 221 | |||
| 222 | return $this->getPathFromId($parentid, $title, $path); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param $title |
||
| 227 | * @param string $order |
||
| 228 | * @param int $preset_id |
||
| 229 | * @param int $none |
||
| 230 | * @param string $sel_name |
||
| 231 | * @param string $onchange |
||
| 232 | */ |
||
| 233 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = ''): void |
||
| 234 | { |
||
| 235 | if ('' === $sel_name) { |
||
| 236 | $sel_name = $this->id; |
||
| 237 | } |
||
| 238 | $myts = \MyTextSanitizer::getInstance(); |
||
| 239 | echo '<select name="' . $sel_name . '"'; |
||
| 240 | if ('' !== $onchange) { |
||
| 241 | echo ' onchange="' . $onchange . '"'; |
||
| 242 | } |
||
| 243 | echo '>'; |
||
| 244 | |||
| 245 | $sql = 'SELECT SQL_CACHE cid, title FROM ' . $this->table . ' WHERE pid=0'; |
||
| 246 | $categories = Utility::getMyItemIds('adslight_submit'); |
||
| 247 | |||
| 248 | if (\is_array($categories) && $categories !== []) { |
||
| 249 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
| 250 | } |
||
| 251 | |||
| 252 | if ('' !== $order) { |
||
| 253 | $sql .= " ORDER BY {$order}"; |
||
| 254 | } |
||
| 255 | |||
| 256 | $result = $this->db->query($sql); |
||
| 257 | if (!$this->db->isResultSet($result)) { |
||
| 258 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 259 | } |
||
| 260 | if (0 !== $none) { |
||
| 261 | echo '<option value="0">----</option>'; |
||
| 262 | } |
||
| 263 | while (false !== [$catid, $name] = $this->db->fetchRow($result)) { |
||
| 264 | $sel = ''; |
||
| 265 | if ($catid === $preset_id) { |
||
| 266 | $sel = ' selected'; |
||
| 267 | } |
||
| 268 | echo "<option value=\"{$catid}\"{$sel}>{$name}</option>"; |
||
| 269 | $sel = ''; |
||
| 270 | $arr = $this->getChildTreeArray($catid, $order); |
||
| 271 | foreach ($arr as $option) { |
||
| 272 | $option['prefix'] = \str_replace('.', '--', $option['prefix']); |
||
| 273 | $catpath = $option['prefix'] . ' ' . $myts->displayTarea($option[$title]); |
||
| 274 | if ($option['cid'] === $preset_id) { |
||
| 275 | $sel = ' selected'; |
||
| 276 | } |
||
| 277 | echo "<option value=\"{$option['cid']}\"{$sel}>{$catpath}</option>"; |
||
| 278 | $sel = ''; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | echo '</select>'; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param $sel_id |
||
| 286 | * @param $title |
||
| 287 | * @param $funcURL |
||
| 288 | * @param string $path |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = ''): string |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param $sel_id |
||
| 318 | * @param string $path |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function getIdPathFromId($sel_id, $path = ''): string |
||
| 322 | { |
||
| 323 | $sel_id = (int)$sel_id; |
||
| 324 | $sql = 'SELECT SQL_CACHE ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id; |
||
| 325 | $result = $this->db->query($sql); |
||
| 326 | if (!$this->db->isResultSet($result)) { |
||
| 327 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 328 | } |
||
| 329 | if (0 === $this->db->getRowsNum($result)) { |
||
| 330 | return $path; |
||
| 331 | } |
||
| 332 | [$parentid] = $this->db->fetchRow($result); |
||
| 333 | $path = "/{$sel_id}{$path}"; |
||
| 334 | if (0 === $parentid) { |
||
| 335 | return $path; |
||
| 336 | } |
||
| 337 | |||
| 338 | return $this->getIdPathFromId($parentid, $path); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param int $sel_id |
||
| 343 | * @param string $order |
||
| 344 | * @param array $parray |
||
| 345 | */ |
||
| 346 | public function getAllChild($sel_id = 0, $order = '', $parray = []): array |
||
| 347 | { |
||
| 348 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
| 349 | |||
| 350 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 351 | if (\is_array($categories) && $categories !== []) { |
||
| 352 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 353 | } |
||
| 354 | |||
| 355 | if ('' !== $order) { |
||
| 356 | $sql .= " ORDER BY {$order}"; |
||
| 357 | } |
||
| 358 | |||
| 359 | $result = $this->db->query($sql); |
||
| 360 | if (!$this->db->isResultSet($result)) { |
||
| 361 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), \E_USER_ERROR); |
||
| 362 | } |
||
| 363 | $count = $this->db->getRowsNum($result); |
||
| 364 | if (0 === $count) { |
||
| 365 | return $parray; |
||
| 366 | } |
||
| 367 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 368 | $parray[] = $row; |
||
| 369 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
| 370 | } |
||
| 371 | |||
| 372 | return $parray; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param int $sel_id |
||
| 377 | * @param string $order |
||
| 378 | * @param array $parray |
||
| 379 | * @param string $r_prefix |
||
| 380 | */ |
||
| 381 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = ''): array |
||
| 382 | { |
||
| 383 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
| 384 | |||
| 385 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 386 | if (\is_array($categories) && $categories !== []) { |
||
| 387 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
| 388 | } |
||
| 389 | |||
| 390 | if ('' !== $order) { |
||
| 391 | $sql .= " ORDER BY {$order}"; |
||
| 392 | } |
||
| 393 | $result = $this->db->query($sql); |
||
| 394 | if (!$this->db->isResultSet($result)) { |
||
| 395 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), \E_USER_ERROR); |
||
| 396 | } |
||
| 397 | $count = $this->db->getRowsNum($result); |
||
| 398 | if (0 === $count) { |
||
| 399 | return $parray; |
||
| 400 | } |
||
| 401 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 402 | $row['prefix'] = $r_prefix . '.'; |
||
| 403 | $parray[] = $row; |
||
| 404 | $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']); |
||
| 405 | } |
||
| 406 | |||
| 407 | return $parray; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param $title |
||
| 412 | * @param string $order |
||
| 413 | * @param int $preset_id |
||
| 414 | * @param int $none |
||
| 415 | * @param string $sel_name |
||
| 416 | * @param string $onchange |
||
| 417 | */ |
||
| 418 | public function makeAdSelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = ''): void |
||
| 419 | { |
||
| 420 | global $myts, $xoopsDB; |
||
| 421 | $helper = Helper::getInstance(); |
||
| 422 | $pathIcon16 = Admin::iconUrl('', '16'); |
||
| 423 | // require_once XOOPS_ROOT_PATH . '/modules/adslight/include/gtickets.php'; |
||
| 424 | |||
| 425 | if ('' === $sel_name) { |
||
| 426 | $sel_name = $this->id; |
||
| 427 | } |
||
| 428 | |||
| 429 | $sql = 'SELECT ' . $this->id . ', ' . $title . ', cat_order FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
| 430 | if ('' !== $order) { |
||
| 431 | $sql .= " ORDER BY {$order}"; |
||
| 432 | } |
||
| 433 | $result = $xoopsDB->query($sql); |
||
| 434 | if (!$xoopsDB->isResultSet($result)) { |
||
| 435 | \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR); |
||
| 436 | } |
||
| 437 | while (false !== [$catid, $name, $cat_order] = $xoopsDB->fetchRow($result)) { |
||
| 438 | echo '<table class="width100 bnone outer"><tr> |
||
| 439 | <th class="left">'; |
||
| 440 | if ('cat_order' === $helper->getConfig('adslight_csortorder')) { |
||
| 441 | echo "({$cat_order})"; |
||
| 442 | } |
||
| 443 | echo " {$name} </th> |
||
| 444 | <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> |
||
| 445 | <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> |
||
| 446 | <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> |
||
| 447 | </tr>'; |
||
| 448 | |||
| 449 | $arr = $this->getChildTreeMapArray($catid, $order); |
||
| 450 | $class = 'odd'; |
||
| 451 | foreach ($arr as $option) { |
||
| 452 | echo "<tr class=\"{$class}\"><td>"; |
||
| 453 | |||
| 454 | $option['prefix'] = \str_replace('.', ' - ', $option['prefix']); |
||
| 455 | $catpath = $option['prefix'] . ' ' . \htmlspecialchars($option[$title], \ENT_QUOTES | \ENT_HTML5); |
||
| 456 | $cat_orderS = $option['cat_order']; |
||
| 457 | if ('cat_order' === $helper->getConfig('adslight_csortorder')) { |
||
| 458 | echo "({$cat_orderS})"; |
||
| 459 | } |
||
| 460 | echo '' . $catpath . '</a></td> |
||
| 461 | <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> |
||
| 462 | <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> |
||
| 463 | <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>'; |
||
| 464 | |||
| 465 | $class = 'even' === $class ? 'odd' : 'even'; |
||
| 466 | } |
||
| 467 | echo '</td></tr></table><br>'; |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param int $sel_id |
||
| 473 | * @param string $order |
||
| 474 | * @param array $parray |
||
| 475 | * @param string $r_prefix |
||
| 476 | */ |
||
| 477 | public function getChildTreeMapArray($sel_id = 0, $order = '', $parray = [], $r_prefix = ''): array |
||
| 478 | { |
||
| 479 | global $xoopsDB; |
||
| 480 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' '; |
||
| 481 | |||
| 482 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 483 | if (\is_array($categories) && $categories !== []) { |
||
| 484 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 485 | } |
||
| 486 | |||
| 487 | if ('' !== $order) { |
||
| 488 | $sql .= " ORDER BY {$order}"; |
||
| 489 | } |
||
| 490 | $result = $xoopsDB->query($sql); |
||
| 491 | if (!$xoopsDB->isResultSet($result)) { |
||
| 492 | \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR); |
||
| 493 | } |
||
| 494 | $count = $xoopsDB->getRowsNum($result); |
||
| 495 | if (0 === $count) { |
||
| 496 | return $parray; |
||
| 497 | } |
||
| 498 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 499 | $row['prefix'] = $r_prefix . '.'; |
||
| 500 | $parray[] = $row; |
||
| 501 | $parray = $this->getChildTreeMapArray($row[$this->id], $order, $parray, $row['prefix']); |
||
| 502 | } |
||
| 503 | |||
| 504 | return $parray; |
||
| 505 | } |
||
| 506 | |||
| 507 | public function getCategoryList(): array |
||
| 525 | } |
||
| 526 | } |
||
| 527 |