Passed
Pull Request — master (#19)
by Michael
02:30
created

Tree   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 144
dl 0
loc 327
rs 9.0399
c 0
b 0
f 0
wmc 42

11 Methods

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

How to fix   Complexity   

Complex Class

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