| Total Complexity | 42 |
| Total Lines | 324 |
| 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) |
||
| 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 | if ($result) { |
||
| 77 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 78 | $arr[] = $myrow; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | return $arr; |
||
| 82 | } |
||
| 83 | |||
| 84 | // returns an array of all FIRST child ids of a given id($selectId) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $selectId |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function getFirstChildId($selectId) |
||
| 92 | { |
||
| 93 | $selectId = (int)$selectId; |
||
| 94 | $idarray = []; |
||
| 95 | $result = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId . ''); |
||
| 96 | $count = $this->db->getRowsNum($result); |
||
| 97 | if (0 == $count) { |
||
| 98 | return $idarray; |
||
| 99 | } |
||
| 100 | while (list($id) = $this->db->fetchRow($result)) { |
||
| 101 | $idarray[] = $id; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $idarray; |
||
| 105 | } |
||
| 106 | |||
| 107 | //returns an array of ALL child ids for a given id($selectId) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param $selectId |
||
| 111 | * @param string $order |
||
| 112 | * @param array $idarray |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getAllChildId($selectId, $order = '', array $idarray = []) |
||
| 134 | } |
||
| 135 | |||
| 136 | //returns an array of ALL parent ids for a given id($selectId) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $selectId |
||
| 140 | * @param string $order |
||
| 141 | * @param array $idarray |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public function getAllParentId($selectId, $order = '', array $idarray = []) |
||
| 146 | { |
||
| 147 | $selectId = (int)$selectId; |
||
| 148 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $selectId . ''; |
||
| 149 | if ('' !== $order) { |
||
| 150 | $sql .= " ORDER BY $order"; |
||
| 151 | } |
||
| 152 | $result = $this->db->query($sql); |
||
| 153 | [$r_id] = $this->db->fetchRow($result); |
||
| 154 | if (0 == $r_id) { |
||
| 155 | return $idarray; |
||
| 156 | } |
||
| 157 | $idarray[] = $r_id; |
||
| 158 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
| 159 | |||
| 160 | return $idarray; |
||
| 161 | } |
||
| 162 | |||
| 163 | //generates path from the root id to a given id($selectId) |
||
| 164 | // the path is delimetered with "/" |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param $selectId |
||
| 168 | * @param $title |
||
| 169 | * @param string $path |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getPathFromId($selectId, $title, $path = '') |
||
| 174 | { |
||
| 175 | $selectId = (int)$selectId; |
||
| 176 | $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$selectId"); |
||
| 177 | if (0 == $this->db->getRowsNum($result)) { |
||
| 178 | return $path; |
||
| 179 | } |
||
| 180 | [$parentid, $name] = $this->db->fetchRow($result); |
||
| 181 | $myts = \MyTextSanitizer::getInstance(); |
||
|
|
|||
| 182 | $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5); |
||
| 183 | $path = '/' . $name . $path . ''; |
||
| 184 | if (0 == $parentid) { |
||
| 185 | return $path; |
||
| 186 | } |
||
| 187 | $path = $this->getPathFromId($parentid, $title, $path); |
||
| 188 | |||
| 189 | return $path; |
||
| 190 | } |
||
| 191 | |||
| 192 | //makes a nicely ordered selection box |
||
| 193 | //$preset_id is used to specify a preselected item |
||
| 194 | //set $none to 1 to add an option with value 0 |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param $title |
||
| 198 | * @param string $order |
||
| 199 | * @param int $preset_id |
||
| 200 | * @param int $none |
||
| 201 | * @param string $sel_name |
||
| 202 | * @param string $onchange |
||
| 203 | */ |
||
| 204 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 242 | } |
||
| 243 | |||
| 244 | //generates nicely formatted linked path from the root id to a given id |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param $selectId |
||
| 248 | * @param $title |
||
| 249 | * @param $funcURL |
||
| 250 | * @param string $path |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getNicePathFromId($selectId, $title, $funcURL, $path = '') |
||
| 255 | { |
||
| 256 | $path = !empty($path) ? $path : $path; |
||
| 257 | $selectId = (int)$selectId; |
||
| 258 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$selectId"; |
||
| 259 | $result = $this->db->query($sql); |
||
| 260 | if (0 == $this->db->getRowsNum($result)) { |
||
| 261 | return $path; |
||
| 262 | } |
||
| 263 | [$parentid, $name] = $this->db->fetchRow($result); |
||
| 264 | $myts = \MyTextSanitizer::getInstance(); |
||
| 265 | $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5); |
||
| 266 | $path = "<li><a href='" . $funcURL . '&' . $this->id . '=' . $selectId . "'>" . $name . '</a></li>' . $path . ''; |
||
| 267 | if (0 == $parentid) { |
||
| 268 | return $path; |
||
| 269 | } |
||
| 270 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
||
| 271 | |||
| 272 | return $path; |
||
| 273 | } |
||
| 274 | |||
| 275 | //generates id path from the root id to a given id |
||
| 276 | // the path is delimetered with "/" |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param $selectId |
||
| 280 | * @param string $path |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getIdPathFromId($selectId, $path = '') |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Enter description here... |
||
| 303 | * |
||
| 304 | * @param int $selectId |
||
| 305 | * @param string $order |
||
| 306 | * @param array $parray |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function getAllChild($selectId = 0, $order = '', array $parray = []) |
||
| 311 | { |
||
| 312 | $selectId = (int)$selectId; |
||
| 313 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId . ' '; |
||
| 314 | if ('' !== $order) { |
||
| 315 | $sql .= " ORDER BY $order"; |
||
| 316 | } |
||
| 317 | $result = $this->db->query($sql); |
||
| 318 | $count = $this->db->getRowsNum($result); |
||
| 319 | if (0 == $count) { |
||
| 320 | return $parray; |
||
| 321 | } |
||
| 322 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 323 | $parray[] = $row; |
||
| 324 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $parray; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Enter description here... |
||
| 332 | * |
||
| 333 | * @param int $selectId |
||
| 334 | * @param string $order |
||
| 335 | * @param array $parray |
||
| 336 | * @param string $r_prefix |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | public function getChildTreeArray($selectId = 0, $order = '', array $parray = [], $r_prefix = '') |
||
| 359 | } |
||
| 360 | } |
||
| 361 |