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