| Total Complexity | 42 |
| Total Lines | 324 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LexikonTree 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 LexikonTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class LexikonTree |
||
| 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 LexikonTree |
||
| 41 | //sets the names of table, unique id, and parend id |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param $table_name |
||
| 45 | * @param $id_name |
||
| 46 | * @param $pid_name |
||
| 47 | */ |
||
| 48 | public function __construct($table_name, $id_name, $pid_name) |
||
| 49 | { |
||
| 50 | // $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php"); |
||
| 51 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 52 | $this->table = $table_name; |
||
| 53 | $this->id = $id_name; |
||
| 54 | $this->pid = $pid_name; |
||
| 55 | } |
||
| 56 | |||
| 57 | // returns an array of first child objects for a given id($sel_id) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param $sel_id |
||
| 61 | * @param string $order |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function getFirstChild($sel_id, $order = '') |
||
| 66 | { |
||
| 67 | $sel_id = (int)$sel_id; |
||
| 68 | $arr = []; |
||
| 69 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
| 70 | if ('' != $order) { |
||
| 71 | $sql .= " ORDER BY $order"; |
||
| 72 | } |
||
| 73 | $result = $this->db->query($sql); |
||
| 74 | if ($result instanceof \mysqli_result) { |
||
| 75 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 76 | $arr[] = $myrow; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | return $arr; |
||
| 80 | } |
||
| 81 | |||
| 82 | // returns an array of all FIRST child ids of a given id($sel_id) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param $sel_id |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function getFirstChildId($sel_id) |
||
| 90 | { |
||
| 91 | $sel_id = (int)$sel_id; |
||
| 92 | $idarray = []; |
||
| 93 | $result = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''); |
||
| 94 | $count = $this->db->getRowsNum($result); |
||
| 95 | if (0 == $count) { |
||
| 96 | return $idarray; |
||
| 97 | } |
||
| 98 | while (list($id) = $this->db->fetchRow($result)) { |
||
| 99 | $idarray[] = $id; |
||
| 100 | } |
||
| 101 | |||
| 102 | return $idarray; |
||
| 103 | } |
||
| 104 | |||
| 105 | //returns an array of ALL child ids for a given id($sel_id) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param $sel_id |
||
| 109 | * @param string $order |
||
| 110 | * @param array $idarray |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getAllChildId($sel_id, $order = '', $idarray = []) |
||
| 115 | { |
||
| 116 | $sel_id = (int)$sel_id; |
||
| 117 | $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
| 118 | if ('' != $order) { |
||
| 119 | $sql .= " ORDER BY $order"; |
||
| 120 | } |
||
| 121 | $result = $this->db->query($sql); |
||
| 122 | $count = $this->db->getRowsNum($result); |
||
| 123 | if (0 == $count) { |
||
| 124 | return $idarray; |
||
| 125 | } |
||
| 126 | while (list($r_id) = $this->db->fetchRow($result)) { |
||
| 127 | $idarray[] = $r_id; |
||
| 128 | $idarray = $this->getAllChildId($r_id, $order, $idarray); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $idarray; |
||
| 132 | } |
||
| 133 | |||
| 134 | //returns an array of ALL parent ids for a given id($sel_id) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param $sel_id |
||
| 138 | * @param string $order |
||
| 139 | * @param array $idarray |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function getAllParentId($sel_id, $order = '', $idarray = []) |
||
| 144 | { |
||
| 145 | $sel_id = (int)$sel_id; |
||
| 146 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . ''; |
||
| 147 | if ('' != $order) { |
||
| 148 | $sql .= " ORDER BY $order"; |
||
| 149 | } |
||
| 150 | $result = $this->db->query($sql); |
||
| 151 | [$r_id] = $this->db->fetchRow($result); |
||
| 152 | if (0 == $r_id) { |
||
| 153 | return $idarray; |
||
| 154 | } |
||
| 155 | $idarray[] = $r_id; |
||
| 156 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
| 157 | |||
| 158 | return $idarray; |
||
| 159 | } |
||
| 160 | |||
| 161 | //generates path from the root id to a given id($sel_id) |
||
| 162 | // the path is delimetered with "/" |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param $sel_id |
||
| 166 | * @param $title |
||
| 167 | * @param string $path |
||
| 168 | * |
||
| 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 | [$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 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
| 271 | } |
||
| 272 | |||
| 273 | //generates id path from the root id to a given id |
||
| 274 | // the path is delimetered with "/" |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param $sel_id |
||
| 278 | * @param string $path |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getIdPathFromId($sel_id, $path = '') |
||
| 283 | { |
||
| 284 | $sel_id = (int)$sel_id; |
||
| 285 | $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"); |
||
| 286 | if (0 == $this->db->getRowsNum($result)) { |
||
| 287 | return $path; |
||
| 288 | } |
||
| 289 | [$parentid] = $this->db->fetchRow($result); |
||
| 290 | $path = '/' . $sel_id . $path . ''; |
||
| 291 | if (0 == $parentid) { |
||
| 292 | return $path; |
||
| 293 | } |
||
| 294 | $path = $this->getIdPathFromId($parentid, $path); |
||
| 295 | |||
| 296 | return $path; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Enter description here... |
||
| 301 | * |
||
| 302 | * @param int|mixed $sel_id |
||
| 303 | * @param string|mixed $order |
||
| 304 | * @param array|mixed $parray |
||
| 305 | * |
||
| 306 | * @return array|mixed |
||
| 307 | */ |
||
| 308 | public function getAllChild($sel_id = 0, $order = '', $parray = []) |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Enter description here... |
||
| 330 | * |
||
| 331 | * @param int|mixed $sel_id |
||
| 332 | * @param string|mixed $order |
||
| 333 | * @param array|mixed $parray |
||
| 334 | * @param string|mixed $r_prefix |
||
| 335 | * @return array|mixed |
||
| 336 | */ |
||
| 337 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '') |
||
| 356 | } |
||
| 357 | } |
||
| 358 |