Total Complexity | 45 |
Total Lines | 331 |
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 | $result = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''); |
||
100 | $count = $this->db->getRowsNum($result); |
||
101 | if ($count == 0) { |
||
102 | return $idarray; |
||
103 | } |
||
104 | while (false !== (list($id) = $this->db->fetchRow($result))) { |
||
105 | $idarray[] = $id; |
||
106 | } |
||
107 | |||
108 | return $idarray; |
||
109 | } |
||
110 | |||
111 | //returns an array of ALL child ids for a given id($sel_id) |
||
112 | /** |
||
113 | * @param $sel_id |
||
114 | * @param string $order |
||
115 | * @param array $idarray |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getAllChildId($sel_id, $order = '', $idarray = array()) |
||
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 ($count == 0) { |
||
129 | return $idarray; |
||
130 | } |
||
131 | while (false !== (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 | * @param $sel_id |
||
142 | * @param string $order |
||
143 | * @param array $idarray |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getAllParentId($sel_id, $order = '', $idarray = array()) |
||
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 ($r_id == 0) { |
||
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 | * @param $sel_id |
||
169 | * @param $title |
||
170 | * @param string $path |
||
171 | * |
||
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 ($this->db->getRowsNum($result) == 0) { |
||
179 | return $path; |
||
180 | } |
||
181 | list($parentid, $name) = $this->db->fetchRow($result); |
||
182 | $myts = MyTextSanitizer::getInstance(); |
||
183 | $name = $myts->htmlSpecialChars($name); |
||
184 | $path = '/' . $name . $path . ''; |
||
185 | if ($parentid == 0) { |
||
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 | * @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 = '') |
||
205 | { |
||
206 | if ($sel_name == '') { |
||
207 | $sel_name = $this->id; |
||
208 | } |
||
209 | $myts = MyTextSanitizer::getInstance(); |
||
210 | echo "<select name='" . $sel_name . "'"; |
||
211 | if ($onchange != '') { |
||
212 | echo " onchange='" . $onchange . "'"; |
||
213 | } |
||
214 | echo ">\n"; |
||
215 | $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
216 | if ($order != '') { |
||
217 | $sql .= " ORDER BY $order"; |
||
218 | } |
||
219 | $result = $this->db->query($sql); |
||
220 | if ($none) { |
||
221 | echo "<option value='0'>----</option>\n"; |
||
222 | } |
||
223 | while (false !== (list($catid, $name) = $this->db->fetchRow($result))) { |
||
224 | $sel = ''; |
||
225 | if ($catid == $preset_id) { |
||
226 | $sel = " selected"; |
||
227 | } |
||
228 | echo "<option value='$catid'$sel>$name</option>\n"; |
||
229 | $sel = ''; |
||
230 | $arr = $this->getChildTreeArray($catid, $order); |
||
231 | foreach ($arr as $option) { |
||
232 | $option['prefix'] = str_replace('.', '--', $option['prefix']); |
||
233 | $catpath = $option['prefix'] . ' ' . $myts->htmlSpecialChars($option[$title]); |
||
234 | if ($option[$this->id] == $preset_id) { |
||
235 | $sel = " selected"; |
||
236 | } |
||
237 | echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n"; |
||
238 | $sel = ''; |
||
239 | } |
||
240 | } |
||
241 | echo "</select>\n"; |
||
242 | } |
||
243 | |||
244 | //generates nicely formatted linked path from the root id to a given id |
||
245 | /** |
||
246 | * @param $sel_id |
||
247 | * @param $title |
||
248 | * @param $funcURL |
||
249 | * @param string $path |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
254 | { |
||
255 | $path = !empty($path) ? ' : ' . $path : $path; |
||
256 | $sel_id = (int)$sel_id; |
||
257 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
258 | $result = $this->db->query($sql); |
||
259 | if ($this->db->getRowsNum($result) == 0) { |
||
260 | return $path; |
||
261 | } |
||
262 | list($parentid, $name) = $this->db->fetchRow($result); |
||
263 | $myts = MyTextSanitizer::getInstance(); |
||
264 | $name = $myts->htmlSpecialChars($name); |
||
265 | $path = "<a href='" . $funcURL . '&' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . ''; |
||
266 | if ($parentid == 0) { |
||
267 | return $path; |
||
268 | } |
||
269 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
||
270 | |||
271 | return $path; |
||
272 | } |
||
273 | |||
274 | //generates id path from the root id to a given id |
||
275 | // the path is delimetered with "/" |
||
276 | /** |
||
277 | * @param $sel_id |
||
278 | * @param string $path |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | public function getIdPathFromId($sel_id, $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 mixed |
||
307 | */ |
||
308 | public function getAllChild($sel_id = 0, $order = '', $parray = array()) |
||
309 | { |
||
310 | $sel_id = (int)$sel_id; |
||
311 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
312 | if ($order != '') { |
||
313 | $sql .= " ORDER BY $order"; |
||
314 | } |
||
315 | $result = $this->db->query($sql); |
||
316 | if (!$this->db->isResultSet($result)) { |
||
317 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
318 | } |
||
319 | $count = $this->db->getRowsNum($result); |
||
320 | if ($count == 0) { |
||
321 | return $parray; |
||
322 | } |
||
323 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
324 | $parray[] = $row; |
||
325 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
326 | } |
||
327 | |||
328 | return $parray; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Enter description here... |
||
333 | * |
||
334 | * @param int|mixed $sel_id |
||
335 | * @param string|mixed $order |
||
336 | * @param array|mixed $parray |
||
337 | * @param string|mixed $r_prefix |
||
338 | * @return mixed |
||
339 | */ |
||
340 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
||
362 | } |
||
363 | } |
||
364 |