Total Complexity | 86 |
Total Lines | 496 |
Duplicated Lines | 0 % |
Changes | 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 declare(strict_types=1); |
||
30 | class Tree |
||
31 | { |
||
32 | public $table; |
||
33 | public $id; |
||
34 | public $pid; |
||
35 | public $order; |
||
36 | public $title; |
||
37 | /** |
||
38 | * @var \XoopsMySQLDatabase |
||
39 | */ |
||
40 | public $db; |
||
41 | |||
42 | /** |
||
43 | * @param $table_name |
||
44 | * @param $id_name |
||
45 | * @param $pid_name |
||
46 | */ |
||
47 | public function __construct( |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param int $sel_id |
||
62 | * @param string $order |
||
63 | */ |
||
64 | public function getFirstChild($sel_id, $order = ''): array |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param $sel_id |
||
95 | * @return array |
||
96 | */ |
||
97 | public function getFirstChildId($sel_id): array |
||
98 | { |
||
99 | $idarray = []; |
||
100 | $sel_id = (int)$sel_id; |
||
101 | $sql = 'SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
102 | $result = $this->db->query($sql); |
||
103 | if (!$this->db->isResultSet($result)) { |
||
104 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
105 | } |
||
106 | |||
107 | $categories = Utility::getMyItemIds('adslight_view'); |
||
108 | if (\is_array($categories) && $categories !== []) { |
||
109 | $result .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
110 | } |
||
111 | |||
112 | $count = $this->db->getRowsNum($result); |
||
113 | if (0 === $count) { |
||
114 | return $idarray; |
||
115 | } |
||
116 | while (false !== [$id] = $this->db->fetchRow($result)) { |
||
117 | $idarray[] = $id; |
||
118 | } |
||
119 | |||
120 | return $idarray; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param $sel_id |
||
125 | * @param string $order |
||
126 | * @param array $idarray |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getAllChildId($sel_id, $order = '', $idarray = []): array |
||
130 | { |
||
131 | $sel_id = (int)$sel_id; |
||
132 | $sql = 'SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
133 | |||
134 | $categories = Utility::getMyItemIds('adslight_view'); |
||
135 | if (\is_array($categories) && $categories !== []) { |
||
136 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
137 | } |
||
138 | |||
139 | if ('' !== $order) { |
||
140 | $sql .= " ORDER BY {$order}"; |
||
141 | } |
||
142 | $result = $this->db->query($sql); |
||
143 | if (!$this->db->isResultSet($result)) { |
||
144 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), \E_USER_ERROR); |
||
145 | } |
||
146 | $count = $this->db->getRowsNum($result); |
||
147 | if (0 === $count) { |
||
148 | return $idarray; |
||
149 | } |
||
150 | while (false !== [$r_id] = $this->db->fetchRow($result)) { |
||
151 | $idarray[] = $r_id; |
||
152 | $idarray = $this->getAllChildId($r_id, $order, $idarray); |
||
153 | } |
||
154 | |||
155 | return $idarray; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param $sel_id |
||
160 | * @param string $order |
||
161 | * @param array $idarray |
||
162 | * @return array |
||
163 | */ |
||
164 | public function getAllParentId($sel_id, $order = '', $idarray = []): array |
||
165 | { |
||
166 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id; |
||
167 | |||
168 | $categories = Utility::getMyItemIds('adslight_view'); |
||
169 | if (\is_array($categories) && $categories !== []) { |
||
170 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
171 | } |
||
172 | |||
173 | if ('' !== $order) { |
||
174 | $sql .= " ORDER BY {$order}"; |
||
175 | } |
||
176 | $result = $this->db->query($sql); |
||
177 | if (!$this->db->isResultSet($result)) { |
||
178 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
179 | } |
||
180 | [$r_id] = $this->db->fetchRow($result); |
||
181 | if (0 === $r_id) { |
||
182 | return $idarray; |
||
183 | } |
||
184 | $idarray[] = $r_id; |
||
185 | |||
186 | return $this->getAllParentId($r_id, $order, $idarray); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param $sel_id |
||
191 | * @param $title |
||
192 | * @param string $path |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getPathFromId($sel_id, $title, $path = ''): string |
||
196 | { |
||
197 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id; |
||
198 | // $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id) . "'"); |
||
199 | |||
200 | $categories = Utility::getMyItemIds('adslight_view'); |
||
201 | if (\is_array($categories) && $categories !== []) { |
||
202 | // $result .= ' AND cid IN (' . implode(',', $categories) . ') '; |
||
203 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
204 | } |
||
205 | |||
206 | $result = $this->db->query($sql); |
||
207 | if (!$this->db->isResultSet($result)) { |
||
208 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
209 | } |
||
210 | |||
211 | if (0 === $this->db->getRowsNum($result)) { |
||
212 | return $path; |
||
213 | } |
||
214 | [$parentid, $name] = $this->db->fetchRow($result); |
||
215 | \MyTextSanitizer::getInstance(); |
||
216 | $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5); |
||
217 | $path = '/' . $name . $path; |
||
218 | if (0 === $parentid) { |
||
219 | return $path; |
||
220 | } |
||
221 | |||
222 | return $this->getPathFromId($parentid, $title, $path); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param $title |
||
227 | * @param string $order |
||
228 | * @param int $preset_id |
||
229 | * @param int $none |
||
230 | * @param string $sel_name |
||
231 | * @param string $onchange |
||
232 | */ |
||
233 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = ''): void |
||
234 | { |
||
235 | if ('' === $sel_name) { |
||
236 | $sel_name = $this->id; |
||
237 | } |
||
238 | $myts = \MyTextSanitizer::getInstance(); |
||
239 | echo '<select name="' . $sel_name . '"'; |
||
240 | if ('' !== $onchange) { |
||
241 | echo ' onchange="' . $onchange . '"'; |
||
242 | } |
||
243 | echo '>'; |
||
244 | |||
245 | $sql = 'SELECT SQL_CACHE cid, title FROM ' . $this->table . ' WHERE pid=0'; |
||
246 | $categories = Utility::getMyItemIds('adslight_submit'); |
||
247 | |||
248 | if (\is_array($categories) && $categories !== []) { |
||
249 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
250 | } |
||
251 | |||
252 | if ('' !== $order) { |
||
253 | $sql .= " ORDER BY {$order}"; |
||
254 | } |
||
255 | |||
256 | $result = $this->db->query($sql); |
||
257 | if (!$this->db->isResultSet($result)) { |
||
258 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
259 | } |
||
260 | if (0 !== $none) { |
||
261 | echo '<option value="0">----</option>'; |
||
262 | } |
||
263 | while (false !== [$catid, $name] = $this->db->fetchRow($result)) { |
||
264 | $sel = ''; |
||
265 | if ($catid === $preset_id) { |
||
266 | $sel = ' selected'; |
||
267 | } |
||
268 | echo "<option value=\"{$catid}\"{$sel}>{$name}</option>"; |
||
269 | $sel = ''; |
||
270 | $arr = $this->getChildTreeArray($catid, $order); |
||
271 | foreach ($arr as $option) { |
||
272 | $option['prefix'] = \str_replace('.', '--', $option['prefix']); |
||
273 | $catpath = $option['prefix'] . ' ' . $myts->displayTarea($option[$title]); |
||
274 | if ($option['cid'] === $preset_id) { |
||
275 | $sel = ' selected'; |
||
276 | } |
||
277 | echo "<option value=\"{$option['cid']}\"{$sel}>{$catpath}</option>"; |
||
278 | $sel = ''; |
||
279 | } |
||
280 | } |
||
281 | echo '</select>'; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param $sel_id |
||
286 | * @param $title |
||
287 | * @param $funcURL |
||
288 | * @param string $path |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = ''): string |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @param $sel_id |
||
318 | * @param string $path |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getIdPathFromId($sel_id, $path = ''): string |
||
322 | { |
||
323 | $sel_id = (int)$sel_id; |
||
324 | $sql = 'SELECT SQL_CACHE ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id; |
||
325 | $result = $this->db->query($sql); |
||
326 | if (!$this->db->isResultSet($result)) { |
||
327 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
328 | } |
||
329 | if (0 === $this->db->getRowsNum($result)) { |
||
330 | return $path; |
||
331 | } |
||
332 | [$parentid] = $this->db->fetchRow($result); |
||
333 | $path = "/{$sel_id}{$path}"; |
||
334 | if (0 === $parentid) { |
||
335 | return $path; |
||
336 | } |
||
337 | |||
338 | return $this->getIdPathFromId($parentid, $path); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param int $sel_id |
||
343 | * @param string $order |
||
344 | * @param array $parray |
||
345 | */ |
||
346 | public function getAllChild($sel_id = 0, $order = '', $parray = []): array |
||
347 | { |
||
348 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
349 | |||
350 | $categories = Utility::getMyItemIds('adslight_view'); |
||
351 | if (\is_array($categories) && $categories !== []) { |
||
352 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
353 | } |
||
354 | |||
355 | if ('' !== $order) { |
||
356 | $sql .= " ORDER BY {$order}"; |
||
357 | } |
||
358 | |||
359 | $result = $this->db->query($sql); |
||
360 | if (!$this->db->isResultSet($result)) { |
||
361 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), \E_USER_ERROR); |
||
362 | } |
||
363 | $count = $this->db->getRowsNum($result); |
||
364 | if (0 === $count) { |
||
365 | return $parray; |
||
366 | } |
||
367 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
368 | $parray[] = $row; |
||
369 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
370 | } |
||
371 | |||
372 | return $parray; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @param int $sel_id |
||
377 | * @param string $order |
||
378 | * @param array $parray |
||
379 | * @param string $r_prefix |
||
380 | */ |
||
381 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = ''): array |
||
382 | { |
||
383 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
384 | |||
385 | $categories = Utility::getMyItemIds('adslight_view'); |
||
386 | if (\is_array($categories) && $categories !== []) { |
||
387 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
388 | } |
||
389 | |||
390 | if ('' !== $order) { |
||
391 | $sql .= " ORDER BY {$order}"; |
||
392 | } |
||
393 | $result = $this->db->query($sql); |
||
394 | if (!$this->db->isResultSet($result)) { |
||
395 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), \E_USER_ERROR); |
||
396 | } |
||
397 | $count = $this->db->getRowsNum($result); |
||
398 | if (0 === $count) { |
||
399 | return $parray; |
||
400 | } |
||
401 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
402 | $row['prefix'] = $r_prefix . '.'; |
||
403 | $parray[] = $row; |
||
404 | $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']); |
||
405 | } |
||
406 | |||
407 | return $parray; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @param $title |
||
412 | * @param string $order |
||
413 | * @param int $preset_id |
||
414 | * @param int $none |
||
415 | * @param string $sel_name |
||
416 | * @param string $onchange |
||
417 | */ |
||
418 | public function makeAdSelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = ''): void |
||
468 | } |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * @param int $sel_id |
||
473 | * @param string $order |
||
474 | * @param array $parray |
||
475 | * @param string $r_prefix |
||
476 | */ |
||
477 | public function getChildTreeMapArray($sel_id = 0, $order = '', $parray = [], $r_prefix = ''): array |
||
505 | } |
||
506 | |||
507 | public function getCategoryList(): array |
||
508 | { |
||
509 | global $xoopsDB; |
||
510 | $sql = 'SELECT SQL_CACHE cid, pid, title FROM ' . $this->table; |
||
511 | $result = $xoopsDB->query($sql); |
||
512 | if (!$xoopsDB->isResultSet($result)) { |
||
528 |