XoopsTree   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 328
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 144
c 2
b 0
f 0
dl 0
loc 328
rs 9.0399
wmc 42

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdPathFromId() 0 15 3
A getPathFromId() 0 17 3
A getAllParentId() 0 16 3
B makeMySelBox() 0 38 9
A getNicePathFromId() 0 19 4
A getChildTreeArray() 0 19 4
A __construct() 0 9 1
A getFirstChildId() 0 14 3
A getFirstChild() 0 16 4
A getAllChildId() 0 18 4
A getAllChild() 0 18 4

How to fix   Complexity   

Complex Class

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 declare(strict_types=1);
2
3
namespace XoopsModules\News;
4
5
/**
6
 * XOOPS tree handler
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @since           2.0.0
18
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
19
 */
20
21
/**
22
 * Abstract base class for forms
23
 *
24
 * @author     Kazumi Ono <[email protected]>
25
 * @author     John Neill <[email protected]>
26
 * @copyright  copyright (c) XOOPS.org
27
 */
28
class XoopsTree
29
{
30
    public $table; //table with parent-child structure
31
    public $id; //name of unique id for records in table $table
32
    public $pid; // name of parent id used in table $table
33
    public $order; //specifies the order of query results
34
    public $title; // name of a field in table $table which will be used when  selection box and paths are generated
35
    public $db;
36
    //constructor of class XoopsTree
37
    //sets the names of table, unique id, and parend id
38
39
    /**
40
     * @param $table_name
41
     * @param $id_name
42
     * @param $pid_name
43
     */
44
    public function __construct($table_name, $id_name, $pid_name)
45
    {
46
        $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1);
47
        $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php" . ". Called from {$trace[0]['file']}line {$trace[0]['line']}");
48
        /** @var \XoopsMySQLDatabase $db */
49
        $this->db    = \XoopsDatabaseFactory::getDatabaseConnection();
50
        $this->table = $table_name;
51
        $this->id    = $id_name;
52
        $this->pid   = $pid_name;
53
    }
54
55
    // returns an array of first child objects for a given id($sel_id)
56
57
    /**
58
     * @param        $sel_id
59
     * @param string $order
60
     *
61
     * @return array
62
     */
63
    public function getFirstChild($sel_id, $order = '')
64
    {
65
        $sel_id = (int)$sel_id;
66
        $arr    = [];
67
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id;
68
        if ('' !== $order) {
69
            $sql .= " ORDER BY $order";
70
        }
71
        $result = $this->db->query($sql);
72
        if ($this->db->isResultSet($result)) {
73
            while (false !== ($myrow = $this->db->fetchArray($result))) {
74
                $arr[] = $myrow;
75
            }
76
        }
77
78
        return $arr;
79
    }
80
81
    // returns an array of all FIRST child ids of a given id($sel_id)
82
83
    /**
84
     * @param $sel_id
85
     *
86
     * @return array
87
     */
88
    public function getFirstChildId($sel_id)
89
    {
90
        $sel_id  = (int)$sel_id;
91
        $idarray = [];
92
        $result  = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id);
93
        $count   = $this->db->getRowsNum($result);
94
        if (0 == $count) {
95
            return $idarray;
96
        }
97
        while ([$id] = $this->db->fetchRow($result)) {
98
            $idarray[] = $id;
99
        }
100
101
        return $idarray;
102
    }
103
104
    //returns an array of ALL child ids for a given id($sel_id)
105
106
    /**
107
     * @param        $sel_id
108
     * @param string $order
109
     * @param array  $idarray
110
     *
111
     * @return array
112
     */
113
    public function getAllChildId($sel_id, $order = '', $idarray = [])
114
    {
115
        $sel_id = (int)$sel_id;
116
        $sql    = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id;
117
        if ('' !== $order) {
118
            $sql .= " ORDER BY $order";
119
        }
120
        $result = $this->db->query($sql);
121
        $count  = $this->db->getRowsNum($result);
122
        if (0 == $count) {
123
            return $idarray;
124
        }
125
        while ([$r_id] = $this->db->fetchRow($result)) {
126
            $idarray[] = $r_id;
127
            $idarray   = $this->getAllChildId($r_id, $order, $idarray);
128
        }
129
130
        return $idarray;
131
    }
132
133
    //returns an array of ALL parent ids for a given id($sel_id)
134
135
    /**
136
     * @param        $sel_id
137
     * @param string $order
138
     * @param array  $idarray
139
     *
140
     * @return array
141
     */
142
    public function getAllParentId($sel_id, $order = '', $idarray = [])
143
    {
144
        $sel_id = (int)$sel_id;
145
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id;
146
        if ('' !== $order) {
147
            $sql .= " ORDER BY $order";
148
        }
149
        $result = $this->db->query($sql);
150
        [$r_id] = $this->db->fetchRow($result);
151
        if (0 == $r_id) {
152
            return $idarray;
153
        }
154
        $idarray[] = $r_id;
155
        $idarray   = $this->getAllParentId($r_id, $order, $idarray);
156
157
        return $idarray;
158
    }
159
160
    //generates path from the root id to a given id($sel_id)
161
    // the path is delimetered with "/"
162
163
    /**
164
     * @param        $sel_id
165
     * @param        $title
166
     * @param string $path
167
     *
168
     * @return string
169
     */
170
    public function getPathFromId($sel_id, $title, $path = '')
171
    {
172
        $sel_id = (int)$sel_id;
173
        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
174
        if (0 == $this->db->getRowsNum($result)) {
175
            return $path;
176
        }
177
        [$parentid, $name] = $this->db->fetchRow($result);
178
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
179
        $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5);
180
        $path = '/' . $name . $path;
181
        if (0 == $parentid) {
182
            return $path;
183
        }
184
        $path = $this->getPathFromId($parentid, $title, $path);
185
186
        return $path;
187
    }
188
189
    //makes a nicely ordered selection box
190
    //$preset_id is used to specify a preselected item
191
    //set $none to 1 to add an option with value 0
192
193
    /**
194
     * @param        $title
195
     * @param string $order
196
     * @param int    $preset_id
197
     * @param int    $none
198
     * @param string $sel_name
199
     * @param string $onchange
200
     */
201
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = ''): void
202
    {
203
        if ('' == $sel_name) {
204
            $sel_name = $this->id;
205
        }
206
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
207
        echo "<select name='" . $sel_name . "'";
208
        if ('' !== $onchange) {
209
            echo " onchange='" . $onchange . "'";
210
        }
211
        echo ">\n";
212
        $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
213
        if ('' !== $order) {
214
            $sql .= " ORDER BY $order";
215
        }
216
        $result = $this->db->query($sql);
217
        if ($none) {
218
            echo "<option value='0'>----</option>\n";
219
        }
220
        while ([$catid, $name] = $this->db->fetchRow($result)) {
221
            $sel = '';
222
            if ($catid == $preset_id) {
223
                $sel = ' selected';
224
            }
225
            echo "<option value='$catid'$sel>$name</option>\n";
226
            $sel = '';
227
            $arr = $this->getChildTreeArray($catid, $order);
228
            foreach ($arr as $option) {
229
                $option['prefix'] = \str_replace('.', '--', $option['prefix']);
230
                $catpath          = $option['prefix'] . '&nbsp;' . \htmlspecialchars($option[$title], \ENT_QUOTES | \ENT_HTML5);
231
                if ($option[$this->id] == $preset_id) {
232
                    $sel = ' selected';
233
                }
234
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
235
                $sel = '';
236
            }
237
        }
238
        echo "</select>\n";
239
    }
240
241
    //generates nicely formatted linked path from the root id to a given id
242
243
    /**
244
     * @param        $sel_id
245
     * @param        $title
246
     * @param        $funcURL
247
     * @param string $path
248
     *
249
     * @return string
250
     */
251
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
252
    {
253
        $path   = !empty($path) ? '&nbsp;:&nbsp;' . $path : $path;
254
        $sel_id = (int)$sel_id;
255
        $sql    = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
256
        $result = $this->db->query($sql);
257
        if (0 == $this->db->getRowsNum($result)) {
258
            return $path;
259
        }
260
        [$parentid, $name] = $this->db->fetchRow($result);
261
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
262
        $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5);
263
        $path = "<a href='" . $funcURL . '&amp;' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path;
264
        if (0 == $parentid) {
265
            return $path;
266
        }
267
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
268
269
        return $path;
270
    }
271
272
    //generates id path from the root id to a given id
273
    // the path is delimetered with "/"
274
275
    /**
276
     * @param        $sel_id
277
     * @param string $path
278
     *
279
     * @return string
280
     */
281
    public function getIdPathFromId($sel_id, $path = '')
282
    {
283
        $sel_id = (int)$sel_id;
284
        $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
285
        if (0 == $this->db->getRowsNum($result)) {
286
            return $path;
287
        }
288
        [$parentid] = $this->db->fetchRow($result);
289
        $path = '/' . $sel_id . $path;
290
        if (0 == $parentid) {
291
            return $path;
292
        }
293
        $path = $this->getIdPathFromId($parentid, $path);
294
295
        return $path;
296
    }
297
298
    /**
299
     * Enter description here...
300
     *
301
     * @param int|mixed    $sel_id
302
     * @param string|mixed $order
303
     * @param array|mixed  $parray
304
     *
305
     * @return mixed
306
     */
307
    public function getAllChild($sel_id = 0, $order = '', $parray = [])
308
    {
309
        $sel_id = (int)$sel_id;
310
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id;
311
        if ('' !== $order) {
312
            $sql .= " ORDER BY $order";
313
        }
314
        $result = $this->db->query($sql);
315
        $count  = $this->db->getRowsNum($result);
316
        if (0 == $count) {
317
            return $parray;
318
        }
319
        while (false !== ($row = $this->db->fetchArray($result))) {
320
            $parray[] = $row;
321
            $parray   = $this->getAllChild($row[$this->id], $order, $parray);
322
        }
323
324
        return $parray;
325
    }
326
327
    /**
328
     * Enter description here...
329
     *
330
     * @param int|mixed    $sel_id
331
     * @param string|mixed $order
332
     * @param array|mixed  $parray
333
     * @param string|mixed $r_prefix
334
     *
335
     * @return mixed
336
     */
337
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '')
338
    {
339
        $sel_id = (int)$sel_id;
340
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id;
341
        if ('' !== $order) {
342
            $sql .= " ORDER BY $order";
343
        }
344
        $result = $this->db->query($sql);
345
        $count  = $this->db->getRowsNum($result);
346
        if (0 == $count) {
347
            return $parray;
348
        }
349
        while (false !== ($row = $this->db->fetchArray($result))) {
350
            $row['prefix'] = $r_prefix . '.';
351
            $parray[]      = $row;
352
            $parray        = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
353
        }
354
355
        return $parray;
356
    }
357
}
358