| Total Complexity | 42 |
| Total Lines | 327 |
| 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 |
||
| 35 | class Tree |
||
| 36 | { |
||
| 37 | public $table; //table with parent-child structure |
||
| 38 | public $id; //name of unique id for records in table $table |
||
| 39 | public $pid; // name of parent id used in table $table |
||
| 40 | public $order; //specifies the order of query results |
||
| 41 | public $title; // name of a field in table $table which will be used when selection box and paths are generated |
||
| 42 | public $db; |
||
| 43 | //constructor of class XoopsTree |
||
| 44 | //sets the names of table, unique id, and parend id |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param $tableName |
||
| 48 | * @param $idName |
||
| 49 | * @param $pidName |
||
| 50 | */ |
||
| 51 | public function __construct($tableName, $idName, $pidName) |
||
| 52 | { |
||
| 53 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 54 | $this->table = $tableName; |
||
| 55 | $this->id = $idName; |
||
| 56 | $this->pid = $pidName; |
||
| 57 | } |
||
| 58 | |||
| 59 | // returns an array of first child objects for a given id($selectId) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param $selectId |
||
| 63 | * @param string $order |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function getFirstChild($selectId, $order = '') |
||
| 68 | { |
||
| 69 | $selectId = (int)$selectId; |
||
| 70 | $arr = []; |
||
| 71 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId . ' '; |
||
| 72 | if ('' !== $order) { |
||
| 73 | $sql .= " ORDER BY $order"; |
||
| 74 | } |
||
| 75 | $result = $this->db->query($sql); |
||
| 76 | $count = $this->db->getRowsNum($result); |
||
| 77 | if (0 == $count) { |
||
| 78 | return $arr; |
||
| 79 | } |
||
| 80 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 81 | $arr[] = $myrow; |
||
| 82 | } |
||
| 83 | |||
| 84 | return $arr; |
||
| 85 | } |
||
| 86 | |||
| 87 | // returns an array of all FIRST child ids of a given id($selectId) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param $selectId |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function getFirstChildId($selectId) |
||
| 95 | { |
||
| 96 | $selectId = (int)$selectId; |
||
| 97 | $idarray = []; |
||
| 98 | $result = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId . ''); |
||
| 99 | $count = $this->db->getRowsNum($result); |
||
| 100 | if (0 == $count) { |
||
| 101 | return $idarray; |
||
| 102 | } |
||
| 103 | while (list($id) = $this->db->fetchRow($result)) { |
||
| 104 | $idarray[] = $id; |
||
| 105 | } |
||
| 106 | |||
| 107 | return $idarray; |
||
| 108 | } |
||
| 109 | |||
| 110 | //returns an array of ALL child ids for a given id($selectId) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param $selectId |
||
| 114 | * @param string $order |
||
| 115 | * @param array $idarray |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function getAllChildId($selectId, $order = '', array $idarray = []) |
||
| 137 | } |
||
| 138 | |||
| 139 | //returns an array of ALL parent ids for a given id($selectId) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $selectId |
||
| 143 | * @param string $order |
||
| 144 | * @param array $idarray |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function getAllParentId($selectId, $order = '', array $idarray = []) |
||
| 149 | { |
||
| 150 | $selectId = (int)$selectId; |
||
| 151 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $selectId . ''; |
||
| 152 | if ('' !== $order) { |
||
| 153 | $sql .= " ORDER BY $order"; |
||
| 154 | } |
||
| 155 | $result = $this->db->query($sql); |
||
| 156 | [$r_id] = $this->db->fetchRow($result); |
||
| 157 | if (0 == $r_id) { |
||
| 158 | return $idarray; |
||
| 159 | } |
||
| 160 | $idarray[] = $r_id; |
||
| 161 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
| 162 | |||
| 163 | return $idarray; |
||
| 164 | } |
||
| 165 | |||
| 166 | //generates path from the root id to a given id($selectId) |
||
| 167 | // the path is delimetered with "/" |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param $selectId |
||
| 171 | * @param $title |
||
| 172 | * @param string $path |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function getPathFromId($selectId, $title, $path = '') |
||
| 177 | { |
||
| 178 | $selectId = (int)$selectId; |
||
| 179 | $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$selectId"); |
||
| 180 | if (0 == $this->db->getRowsNum($result)) { |
||
| 181 | return $path; |
||
| 182 | } |
||
| 183 | [$parentid, $name] = $this->db->fetchRow($result); |
||
| 184 | $myts = \MyTextSanitizer::getInstance(); |
||
|
|
|||
| 185 | $name = htmlspecialchars($name); |
||
| 186 | $path = '/' . $name . $path . ''; |
||
| 187 | if (0 == $parentid) { |
||
| 188 | return $path; |
||
| 189 | } |
||
| 190 | $path = $this->getPathFromId($parentid, $title, $path); |
||
| 191 | |||
| 192 | return $path; |
||
| 193 | } |
||
| 194 | |||
| 195 | //makes a nicely ordered selection box |
||
| 196 | //$preset_id is used to specify a preselected item |
||
| 197 | //set $none to 1 to add a option with value 0 |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param $title |
||
| 201 | * @param string $order |
||
| 202 | * @param int $preset_id |
||
| 203 | * @param int $none |
||
| 204 | * @param string $sel_name |
||
| 205 | * @param string $onchange |
||
| 206 | */ |
||
| 207 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 245 | } |
||
| 246 | |||
| 247 | //generates nicely formatted linked path from the root id to a given id |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param $selectId |
||
| 251 | * @param $title |
||
| 252 | * @param $funcURL |
||
| 253 | * @param string $path |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getNicePathFromId($selectId, $title, $funcURL, $path = '') |
||
| 258 | { |
||
| 259 | $path = !empty($path) ? $path : $path; |
||
| 260 | $selectId = (int)$selectId; |
||
| 261 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$selectId"; |
||
| 262 | $result = $this->db->query($sql); |
||
| 263 | if (0 == $this->db->getRowsNum($result)) { |
||
| 264 | return $path; |
||
| 265 | } |
||
| 266 | [$parentid, $name] = $this->db->fetchRow($result); |
||
| 267 | $myts = \MyTextSanitizer::getInstance(); |
||
| 268 | $name = htmlspecialchars($name); |
||
| 269 | $path = "<li><a href='" . $funcURL . '&' . $this->id . '=' . $selectId . "'>" . $name . '</a></li>' . $path . ''; |
||
| 270 | if (0 == $parentid) { |
||
| 271 | return $path; |
||
| 272 | } |
||
| 273 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
||
| 274 | |||
| 275 | return $path; |
||
| 276 | } |
||
| 277 | |||
| 278 | //generates id path from the root id to a given id |
||
| 279 | // the path is delimetered with "/" |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param $selectId |
||
| 283 | * @param string $path |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getIdPathFromId($selectId, $path = '') |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Enter description here... |
||
| 306 | * |
||
| 307 | * @param int $selectId |
||
| 308 | * @param string $order |
||
| 309 | * @param array $parray |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function getAllChild($selectId = 0, $order = '', array $parray = []) |
||
| 314 | { |
||
| 315 | $selectId = (int)$selectId; |
||
| 316 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId . ' '; |
||
| 317 | if ('' !== $order) { |
||
| 318 | $sql .= " ORDER BY $order"; |
||
| 319 | } |
||
| 320 | $result = $this->db->query($sql); |
||
| 321 | $count = $this->db->getRowsNum($result); |
||
| 322 | if (0 == $count) { |
||
| 323 | return $parray; |
||
| 324 | } |
||
| 325 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 326 | $parray[] = $row; |
||
| 327 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
| 328 | } |
||
| 329 | |||
| 330 | return $parray; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Enter description here... |
||
| 335 | * |
||
| 336 | * @param int $selectId |
||
| 337 | * @param string $order |
||
| 338 | * @param array $parray |
||
| 339 | * @param string $r_prefix |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | public function getChildTreeArray($selectId = 0, $order = '', array $parray = [], $r_prefix = '') |
||
| 362 | } |
||
| 363 | } |
||
| 364 |