| Total Complexity | 42 |
| Total Lines | 321 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 32 | class Tree |
||
| 33 | { |
||
| 34 | public $table; //table with parent-child structure |
||
| 35 | public $id; //name of unique id for records in table $table |
||
| 36 | public $pid; // name of parent id used in table $table |
||
| 37 | public $order; //specifies the order of query results |
||
| 38 | public $title; // name of a field in table $table which will be used when selection box and paths are generated |
||
| 39 | public $db; |
||
| 40 | //constructor of class XoopsTree |
||
| 41 | //sets the names of table, unique id, and parend id |
||
| 42 | |||
| 43 | /** |
||
| 44 | * WflinksXoopsTree constructor. |
||
| 45 | * @param $table_name |
||
| 46 | * @param $id_name |
||
| 47 | * @param $pid_name |
||
| 48 | */ |
||
| 49 | public function __construct($table_name, $id_name, $pid_name) |
||
| 50 | { |
||
| 51 | $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
||
|
|
|||
| 52 | // $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php" . ". Called from {$trace[0]['file']}line {$trace[0]['line']}"); |
||
| 53 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 54 | $this->table = $table_name; |
||
| 55 | $this->id = $id_name; |
||
| 56 | $this->pid = $pid_name; |
||
| 57 | } |
||
| 58 | |||
| 59 | // returns an array of first child objects for a given id($sel_id) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param $sel_id |
||
| 63 | * @param string $order |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getFirstChild($sel_id, $order = '') |
||
| 67 | { |
||
| 68 | $sel_id = (int)$sel_id; |
||
| 69 | $arr = []; |
||
| 70 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' '; |
||
| 71 | if ('' != $order) { |
||
| 72 | $sql .= " ORDER BY $order"; |
||
| 73 | } |
||
| 74 | $result = $this->db->query($sql); |
||
| 75 | $count = $this->db->getRowsNum($result); |
||
| 76 | if (0 == $count) { |
||
| 77 | return $arr; |
||
| 78 | } |
||
| 79 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 80 | $arr[] = $myrow; |
||
| 81 | } |
||
| 82 | |||
| 83 | return $arr; |
||
| 84 | } |
||
| 85 | |||
| 86 | // returns an array of all FIRST child ids of a given id($sel_id) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param $sel_id |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function getFirstChildId($sel_id) |
||
| 106 | } |
||
| 107 | |||
| 108 | //returns an array of ALL child ids for a given id($sel_id) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param $sel_id |
||
| 112 | * @param string $order |
||
| 113 | * @param array $idarray |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getAllChildId($sel_id, $order = '', $idarray = []) |
||
| 117 | { |
||
| 118 | $sel_id = (int)$sel_id; |
||
| 119 | $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
| 120 | if ('' !== $order) { |
||
| 121 | $sql .= " ORDER BY $order"; |
||
| 122 | } |
||
| 123 | $result = $this->db->query($sql); |
||
| 124 | $count = $this->db->getRowsNum($result); |
||
| 125 | if (0 == $count) { |
||
| 126 | return $idarray; |
||
| 127 | } |
||
| 128 | while (list($r_id) = $this->db->fetchRow($result)) { |
||
| 129 | $idarray[] = $r_id; |
||
| 130 | $idarray = $this->getAllChildId($r_id, $order, $idarray); |
||
| 131 | } |
||
| 132 | |||
| 133 | return $idarray; |
||
| 134 | } |
||
| 135 | |||
| 136 | //returns an array of ALL parent ids for a given id($sel_id) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $sel_id |
||
| 140 | * @param string $order |
||
| 141 | * @param array $idarray |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function getAllParentId($sel_id, $order = '', $idarray = []) |
||
| 145 | { |
||
| 146 | $sel_id = (int)$sel_id; |
||
| 147 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . ''; |
||
| 148 | if ('' !== $order) { |
||
| 149 | $sql .= " ORDER BY $order"; |
||
| 150 | } |
||
| 151 | $result = $this->db->query($sql); |
||
| 152 | list($r_id) = $this->db->fetchRow($result); |
||
| 153 | if (0 == $r_id) { |
||
| 154 | return $idarray; |
||
| 155 | } |
||
| 156 | $idarray[] = $r_id; |
||
| 157 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
| 158 | |||
| 159 | return $idarray; |
||
| 160 | } |
||
| 161 | |||
| 162 | //generates path from the root id to a given id($sel_id) |
||
| 163 | // the path is delimetered with "/" |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param $sel_id |
||
| 167 | * @param $title |
||
| 168 | * @param string $path |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getPathFromId($sel_id, $title, $path = '') |
||
| 172 | { |
||
| 173 | $sel_id = (int)$sel_id; |
||
| 174 | $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"); |
||
| 175 | if (0 == $this->db->getRowsNum($result)) { |
||
| 176 | return $path; |
||
| 177 | } |
||
| 178 | list($parentid, $name) = $this->db->fetchRow($result); |
||
| 179 | $myts = \MyTextSanitizer::getInstance(); |
||
| 180 | $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5); |
||
| 181 | $path = '/' . $name . $path . ''; |
||
| 182 | if (0 == $parentid) { |
||
| 183 | return $path; |
||
| 184 | } |
||
| 185 | $path = $this->getPathFromId($parentid, $title, $path); |
||
| 186 | |||
| 187 | return $path; |
||
| 188 | } |
||
| 189 | |||
| 190 | //makes a nicely ordered selection box |
||
| 191 | //$preset_id is used to specify a preselected item |
||
| 192 | //set $none to 1 to add a option with value 0 |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param $title |
||
| 196 | * @param string $order |
||
| 197 | * @param int $preset_id |
||
| 198 | * @param int $none |
||
| 199 | * @param string $sel_name |
||
| 200 | * @param string $onchange |
||
| 201 | */ |
||
| 202 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 203 | { |
||
| 204 | if ('' === $sel_name) { |
||
| 205 | $sel_name = $this->id; |
||
| 206 | } |
||
| 207 | $myts = \MyTextSanitizer::getInstance(); |
||
| 208 | echo "<select name='" . $sel_name . "'"; |
||
| 209 | if ('' !== $onchange) { |
||
| 210 | echo " onchange='" . $onchange . "'"; |
||
| 211 | } |
||
| 212 | echo ">\n"; |
||
| 213 | $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
| 214 | if ('' !== $order) { |
||
| 215 | $sql .= " ORDER BY $order"; |
||
| 216 | } |
||
| 217 | $result = $this->db->query($sql); |
||
| 218 | if ($none) { |
||
| 219 | echo "<option value='0'>----</option>\n"; |
||
| 220 | } |
||
| 221 | while (list($catid, $name) = $this->db->fetchRow($result)) { |
||
| 222 | $sel = ''; |
||
| 223 | if ($catid == $preset_id) { |
||
| 224 | $sel = ' selected'; |
||
| 225 | } |
||
| 226 | echo "<option value='$catid'$sel>$name</option>\n"; |
||
| 227 | $sel = ''; |
||
| 228 | $arr = $this->getChildTreeArray($catid, $order); |
||
| 229 | foreach ($arr as $option) { |
||
| 230 | $option['prefix'] = \str_replace('.', '--', $option['prefix']); |
||
| 231 | $catpath = $option['prefix'] . ' ' . htmlspecialchars($option[$title], ENT_QUOTES | ENT_HTML5); |
||
| 232 | if ($option[$this->id] == $preset_id) { |
||
| 233 | $sel = ' selected'; |
||
| 234 | } |
||
| 235 | echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n"; |
||
| 236 | $sel = ''; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | echo "</select>\n"; |
||
| 240 | } |
||
| 241 | |||
| 242 | //generates nicely formatted linked path from the root id to a given id |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param $sel_id |
||
| 246 | * @param $title |
||
| 247 | * @param $funcURL |
||
| 248 | * @param string $path |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
| 252 | { |
||
| 253 | $path = !empty($path) ? ' : ' . $path : $path; |
||
| 254 | $sel_id = (int)$sel_id; |
||
| 255 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
| 256 | $result = $this->db->query($sql); |
||
| 257 | if (0 == $this->db->getRowsNum($result)) { |
||
| 258 | return $path; |
||
| 259 | } |
||
| 260 | list($parentid, $name) = $this->db->fetchRow($result); |
||
| 261 | $myts = \MyTextSanitizer::getInstance(); |
||
| 262 | $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5); |
||
| 263 | $path = "<a href='" . $funcURL . '&' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . ''; |
||
| 264 | if (0 == $parentid) { |
||
| 265 | return $path; |
||
| 266 | } |
||
| 267 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
||
| 268 | |||
| 269 | return $path; |
||
| 270 | } |
||
| 271 | |||
| 272 | //generates id path from the root id to a given id |
||
| 273 | // the path is delimetered with "/" |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $sel_id |
||
| 277 | * @param string $path |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getIdPathFromId($sel_id, $path = '') |
||
| 281 | { |
||
| 282 | $sel_id = (int)$sel_id; |
||
| 283 | $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"); |
||
| 284 | if (0 == $this->db->getRowsNum($result)) { |
||
| 285 | return $path; |
||
| 286 | } |
||
| 287 | list($parentid) = $this->db->fetchRow($result); |
||
| 288 | $path = '/' . $sel_id . $path . ''; |
||
| 289 | if (0 == $parentid) { |
||
| 290 | return $path; |
||
| 291 | } |
||
| 292 | $path = $this->getIdPathFromId($parentid, $path); |
||
| 293 | |||
| 294 | return $path; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Enter description here... |
||
| 299 | * |
||
| 300 | * @param int|unknown_type $sel_id |
||
| 301 | * @param string|unknown_type $order |
||
| 302 | * @param array|unknown_type $parray |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | public function getAllChild($sel_id = 0, $order = '', $parray = []) |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Enter description here... |
||
| 327 | * |
||
| 328 | * @param int|unknown_type $sel_id |
||
| 329 | * @param string|unknown_type $order |
||
| 330 | * @param array|unknown_type $parray |
||
| 331 | * @param string|unknown_type $r_prefix |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '') |
||
| 353 | } |
||
| 354 | } |
||
| 355 |