Total Complexity | 52 |
Total Lines | 355 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like XoopsTree 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 XoopsTree, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class XoopsTree |
||
32 | { |
||
33 | public $table; //table with parent-child structure |
||
34 | public $id; //name of unique id for records in table $table |
||
35 | public $pid; // name of parent id used in table $table |
||
36 | public $order; //specifies the order of query results |
||
37 | public $title; // name of a field in table $table which will be used when selection box and paths are generated |
||
38 | /** |
||
39 | * @var \XoopsMySQLDatabase |
||
40 | */ |
||
41 | public $db; |
||
42 | |||
43 | //constructor of class XoopsTree |
||
44 | //sets the names of table, unique id, and parend id |
||
45 | /** |
||
46 | * @param $table_name |
||
47 | * @param $id_name |
||
48 | * @param $pid_name |
||
49 | */ |
||
50 | public function __construct($table_name, $id_name, $pid_name) |
||
57 | } |
||
58 | |||
59 | // returns an array of first child objects for a given id($sel_id) |
||
60 | /** |
||
61 | * @param $sel_id |
||
62 | * @param string $order |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | public function getFirstChild($sel_id, $order = '') |
||
87 | } |
||
88 | |||
89 | // returns an array of all FIRST child ids of a given id($sel_id) |
||
90 | /** |
||
91 | * @param $sel_id |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getFirstChildId($sel_id) |
||
96 | { |
||
97 | $sel_id = (int)$sel_id; |
||
98 | $idarray = array(); |
||
99 | $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
100 | $result = $this->db->query($sql); |
||
101 | if (!$this->db->isResultSet($result)) { |
||
102 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
103 | } |
||
104 | $count = $this->db->getRowsNum($result); |
||
105 | if ($count == 0) { |
||
106 | return $idarray; |
||
107 | } |
||
108 | while (false !== (list($id) = $this->db->fetchRow($result))) { |
||
109 | $idarray[] = $id; |
||
110 | } |
||
111 | |||
112 | return $idarray; |
||
113 | } |
||
114 | |||
115 | //returns an array of ALL child ids for a given id($sel_id) |
||
116 | /** |
||
117 | * @param $sel_id |
||
118 | * @param string $order |
||
119 | * @param array $idarray |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getAllChildId($sel_id, $order = '', $idarray = array()) |
||
124 | { |
||
125 | $sel_id = (int)$sel_id; |
||
126 | $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
127 | if ($order != '') { |
||
128 | $sql .= " ORDER BY $order"; |
||
129 | } |
||
130 | $result = $this->db->query($sql); |
||
131 | if (!$this->db->isResultSet($result)) { |
||
132 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
133 | } |
||
134 | $count = $this->db->getRowsNum($result); |
||
135 | if ($count == 0) { |
||
136 | return $idarray; |
||
137 | } |
||
138 | while (false !== (list($r_id) = $this->db->fetchRow($result))) { |
||
139 | $idarray[] = $r_id; |
||
140 | $idarray = $this->getAllChildId($r_id, $order, $idarray); |
||
141 | } |
||
142 | |||
143 | return $idarray; |
||
144 | } |
||
145 | |||
146 | //returns an array of ALL parent ids for a given id($sel_id) |
||
147 | /** |
||
148 | * @param $sel_id |
||
149 | * @param string $order |
||
150 | * @param array $idarray |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | public function getAllParentId($sel_id, $order = '', $idarray = array()) |
||
155 | { |
||
156 | $sel_id = (int)$sel_id; |
||
157 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . ''; |
||
158 | if ($order != '') { |
||
159 | $sql .= " ORDER BY $order"; |
||
160 | } |
||
161 | $result = $this->db->query($sql); |
||
162 | if (!$this->db->isResultSet($result)) { |
||
163 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
164 | } |
||
165 | list($r_id) = $this->db->fetchRow($result); |
||
166 | if ($r_id == 0) { |
||
167 | return $idarray; |
||
168 | } |
||
169 | $idarray[] = $r_id; |
||
170 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
171 | |||
172 | return $idarray; |
||
173 | } |
||
174 | |||
175 | //generates path from the root id to a given id($sel_id) |
||
176 | // the path is delimetered with "/" |
||
177 | /** |
||
178 | * @param $sel_id |
||
179 | * @param $title |
||
180 | * @param string $path |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getPathFromId($sel_id, $title, $path = '') |
||
185 | { |
||
186 | $sel_id = (int)$sel_id; |
||
187 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
188 | $result = $this->db->query($sql); |
||
189 | if (!$this->db->isResultSet($result)) { |
||
190 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
191 | } |
||
192 | if ($this->db->getRowsNum($result) == 0) { |
||
193 | return $path; |
||
194 | } |
||
195 | list($parentid, $name) = $this->db->fetchRow($result); |
||
196 | $myts = MyTextSanitizer::getInstance(); |
||
197 | $name = $myts->htmlSpecialChars($name); |
||
198 | $path = '/' . $name . $path . ''; |
||
199 | if ($parentid == 0) { |
||
200 | return $path; |
||
201 | } |
||
202 | $path = $this->getPathFromId($parentid, $title, $path); |
||
203 | |||
204 | return $path; |
||
205 | } |
||
206 | |||
207 | //makes a nicely ordered selection box |
||
208 | //$preset_id is used to specify a preselected item |
||
209 | //set $none to 1 to add a option with value 0 |
||
210 | /** |
||
211 | * @param $title |
||
212 | * @param string $order |
||
213 | * @param int $preset_id |
||
214 | * @param int $none |
||
215 | * @param string $sel_name |
||
216 | * @param string $onchange |
||
217 | */ |
||
218 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
219 | { |
||
220 | if ($sel_name == '') { |
||
221 | $sel_name = $this->id; |
||
222 | } |
||
223 | $myts = MyTextSanitizer::getInstance(); |
||
224 | echo "<select name='" . $sel_name . "'"; |
||
225 | if ($onchange != '') { |
||
226 | echo " onchange='" . $onchange . "'"; |
||
227 | } |
||
228 | echo ">\n"; |
||
229 | $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
230 | if ($order != '') { |
||
231 | $sql .= " ORDER BY $order"; |
||
232 | } |
||
233 | $result = $this->db->query($sql); |
||
234 | if (!$this->db->isResultSet($result)) { |
||
235 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
236 | } |
||
237 | if ($none) { |
||
238 | echo "<option value='0'>----</option>\n"; |
||
239 | } |
||
240 | while (false !== (list($catid, $name) = $this->db->fetchRow($result))) { |
||
241 | $sel = ''; |
||
242 | if ($catid == $preset_id) { |
||
243 | $sel = " selected"; |
||
244 | } |
||
245 | echo "<option value='$catid'$sel>$name</option>\n"; |
||
246 | $sel = ''; |
||
247 | $arr = $this->getChildTreeArray($catid, $order); |
||
248 | foreach ($arr as $option) { |
||
249 | $option['prefix'] = str_replace('.', '--', $option['prefix']); |
||
250 | $catpath = $option['prefix'] . ' ' . $myts->htmlSpecialChars($option[$title]); |
||
251 | if ($option[$this->id] == $preset_id) { |
||
252 | $sel = " selected"; |
||
253 | } |
||
254 | echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n"; |
||
255 | $sel = ''; |
||
256 | } |
||
257 | } |
||
258 | echo "</select>\n"; |
||
259 | } |
||
260 | |||
261 | //generates nicely formatted linked path from the root id to a given id |
||
262 | /** |
||
263 | * @param $sel_id |
||
264 | * @param $title |
||
265 | * @param $funcURL |
||
266 | * @param string $path |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
271 | { |
||
272 | $path = !empty($path) ? ' : ' . $path : $path; |
||
273 | $sel_id = (int)$sel_id; |
||
274 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
275 | $result = $this->db->query($sql); |
||
276 | if (!$this->db->isResultSet($result)) { |
||
277 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
278 | } |
||
279 | if ($this->db->getRowsNum($result) == 0) { |
||
280 | return $path; |
||
281 | } |
||
282 | list($parentid, $name) = $this->db->fetchRow($result); |
||
283 | $myts = MyTextSanitizer::getInstance(); |
||
284 | $name = $myts->htmlSpecialChars($name); |
||
285 | $path = "<a href='" . $funcURL . '&' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . ''; |
||
286 | if ($parentid == 0) { |
||
287 | return $path; |
||
288 | } |
||
289 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
||
290 | |||
291 | return $path; |
||
292 | } |
||
293 | |||
294 | //generates id path from the root id to a given id |
||
295 | // the path is delimetered with "/" |
||
296 | /** |
||
297 | * @param $sel_id |
||
298 | * @param string $path |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | public function getIdPathFromId($sel_id, $path = '') |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Enter description here... |
||
325 | * |
||
326 | * @param int|mixed $sel_id |
||
327 | * @param string|mixed $order |
||
328 | * @param array|mixed $parray |
||
329 | * |
||
330 | * @return mixed |
||
331 | */ |
||
332 | public function getAllChild($sel_id = 0, $order = '', $parray = array()) |
||
333 | { |
||
334 | $sel_id = (int)$sel_id; |
||
335 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
336 | if ($order != '') { |
||
337 | $sql .= " ORDER BY $order"; |
||
338 | } |
||
339 | $result = $this->db->query($sql); |
||
340 | if (!$this->db->isResultSet($result)) { |
||
341 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
342 | } |
||
343 | $count = $this->db->getRowsNum($result); |
||
344 | if ($count == 0) { |
||
345 | return $parray; |
||
346 | } |
||
347 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
348 | $parray[] = $row; |
||
349 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
350 | } |
||
351 | |||
352 | return $parray; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Enter description here... |
||
357 | * |
||
358 | * @param int|mixed $sel_id |
||
359 | * @param string|mixed $order |
||
360 | * @param array|mixed $parray |
||
361 | * @param string|mixed $r_prefix |
||
362 | * @return mixed |
||
363 | */ |
||
364 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
||
386 | } |
||
387 | } |
||
388 |