Total Complexity | 48 |
Total Lines | 362 |
Duplicated Lines | 9.94 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like efqTree 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 efqTree, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class efqTree |
||
25 | { |
||
26 | public $table; |
||
27 | //table with parent-child structure |
||
28 | public $id; |
||
29 | //name of unique id for records in table $table |
||
30 | public $pid; |
||
31 | // name of parent id used in table $table |
||
32 | public $order; |
||
33 | //specifies the order of query results |
||
34 | public $title; |
||
35 | // name of a field in table $table which will be used when selection box and paths are generated |
||
36 | public $db; |
||
37 | public $dirid; |
||
38 | |||
39 | //constructor of class XoopsTree |
||
40 | //sets the names of table, unique id, and parend id |
||
41 | /** |
||
42 | * efqTree constructor. |
||
43 | * @param $table_name |
||
44 | * @param $id_name |
||
45 | * @param $pid_name |
||
46 | */ |
||
47 | public function __construct($table_name, $id_name, $pid_name) |
||
48 | { |
||
49 | $this->db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
|
|||
50 | $this->table = $table_name; |
||
51 | $this->id = $id_name; |
||
52 | $this->pid = $pid_name; |
||
53 | $this->dirid = 0; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param int $dirid |
||
58 | */ |
||
59 | public function setDir($dirid = 0) |
||
60 | { |
||
61 | $this->dirid = $dirid; |
||
62 | } |
||
63 | |||
64 | // returns an array of first child objects for a given id($sel_id) |
||
65 | |||
66 | /** |
||
67 | * @param $sel_id |
||
68 | * @param string $order |
||
69 | * @return array |
||
70 | */ |
||
71 | public function getFirstChild($sel_id, $order = '') |
||
72 | { |
||
73 | $arr = array(); |
||
74 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
75 | if ($order !== '') { |
||
76 | $sql .= " ORDER BY $order"; |
||
77 | } |
||
78 | $result = $this->db->query($sql); |
||
79 | $count = $this->db->getRowsNum($result); |
||
80 | if ($count == 0) { |
||
81 | return $arr; |
||
82 | } |
||
83 | while ($myrow = $this->db->fetchArray($result)) { |
||
84 | array_push($arr, $myrow); |
||
85 | } |
||
86 | |||
87 | return $arr; |
||
88 | } |
||
89 | |||
90 | // returns an array of all FIRST child ids of a given id($sel_id) |
||
91 | |||
92 | /** |
||
93 | * @param $sel_id |
||
94 | * @return array |
||
95 | */ |
||
96 | public function getFirstChildId($sel_id) |
||
97 | { |
||
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 (list($id) = $this->db->fetchRow($result)) { |
||
105 | array_push($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 | /** |
||
114 | * @param $sel_id |
||
115 | * @param string $order |
||
116 | * @param array $idarray |
||
117 | * @return array |
||
118 | */ |
||
119 | View Code Duplication | public function getAllChildId($sel_id, $order = '', $idarray = array()) |
|
120 | { |
||
121 | $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
122 | if ($order !== '') { |
||
123 | $sql .= " ORDER BY $order"; |
||
124 | } |
||
125 | $result = $this->db->query($sql); |
||
126 | $count = $this->db->getRowsNum($result); |
||
127 | if ($count == 0) { |
||
128 | return $idarray; |
||
129 | } |
||
130 | while (list($r_id) = $this->db->fetchRow($result)) { |
||
131 | array_push($idarray, $r_id); |
||
132 | $idarray = $this->getAllChildId($r_id, $order, $idarray); |
||
133 | } |
||
134 | |||
135 | return $idarray; |
||
136 | } |
||
137 | |||
138 | //returns an array of ALL parent ids for a given id($sel_id) |
||
139 | |||
140 | /** |
||
141 | * @param $sel_id |
||
142 | * @param string $order |
||
143 | * @param array $idarray |
||
144 | * @return array |
||
145 | */ |
||
146 | public function getAllParentId($sel_id, $order = '', $idarray = array()) |
||
147 | { |
||
148 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . ''; |
||
149 | if ($order !== '') { |
||
150 | $sql .= " ORDER BY $order"; |
||
151 | } |
||
152 | $result = $this->db->query($sql); |
||
153 | list($r_id) = $this->db->fetchRow($result); |
||
154 | if ($r_id == 0) { |
||
155 | return $idarray; |
||
156 | } |
||
157 | array_push($idarray, $r_id); |
||
158 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
159 | |||
160 | return $idarray; |
||
161 | } |
||
162 | |||
163 | //generates path from the root id to a given id($sel_id) |
||
164 | // the path is delimetered with "/" |
||
165 | /** |
||
166 | * @param $sel_id |
||
167 | * @param $title |
||
168 | * @param string $path |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getPathFromId($sel_id, $title, $path = '') |
||
172 | { |
||
173 | $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"); |
||
174 | if ($this->db->getRowsNum($result) == 0) { |
||
175 | return $path; |
||
176 | } |
||
177 | list($parentid, $name) = $this->db->fetchRow($result); |
||
178 | $myts = MyTextSanitizer::getInstance(); |
||
179 | $name = $myts->htmlSpecialChars($name); |
||
180 | $path = '/' . $name . $path . ''; |
||
181 | if ($parentid == 0) { |
||
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 a option with value 0 |
||
192 | /** |
||
193 | * @param $title |
||
194 | * @param string $order |
||
195 | * @param int $preset_id |
||
196 | * @param int $none |
||
197 | * @param string $sel_name |
||
198 | * @param string $onchange |
||
199 | * @param int $multiple |
||
200 | */ |
||
201 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '', $multiple = 0) |
||
250 | } |
||
251 | |||
252 | //generates nicely formatted linked path from the root id to a given id |
||
253 | |||
254 | /** |
||
255 | * @param $sel_id |
||
256 | * @param $title |
||
257 | * @param $funcURL |
||
258 | * @param string $path |
||
259 | * @param string $outputvar |
||
260 | * @return string |
||
261 | */ |
||
262 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '', $outputvar = 'catid') |
||
263 | { |
||
264 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
265 | $result = $this->db->query($sql); |
||
266 | if ($this->db->getRowsNum($result) == 0) { |
||
267 | return $path; |
||
268 | } |
||
269 | list($parentid, $name) = $this->db->fetchRow($result); |
||
270 | $myts = MyTextSanitizer::getInstance(); |
||
271 | $name = $myts->htmlSpecialChars($name); |
||
272 | $path = "<a href='" . $funcURL . '&' . $outputvar . '=' . $sel_id . '\'>' . $name . '</a> : ' . $path . ''; |
||
273 | if ($parentid == 0) { |
||
274 | return $path; |
||
275 | } |
||
276 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path, $outputvar); |
||
277 | |||
278 | return $path; |
||
279 | } |
||
280 | |||
281 | //generates nicely formatted path from the root id to a given id |
||
282 | |||
283 | /** |
||
284 | * @param $sel_id |
||
285 | * @param $title |
||
286 | * @param string $path |
||
287 | * @return string |
||
288 | */ |
||
289 | public function getUnformattedPathFromId($sel_id, $title, $path = '') |
||
290 | { |
||
291 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
292 | $result = $this->db->query($sql); |
||
293 | if ($this->db->getRowsNum($result) == 0) { |
||
294 | return $path; |
||
295 | } |
||
296 | list($parentid, $name) = $this->db->fetchRow($result); |
||
297 | $myts = MyTextSanitizer::getInstance(); |
||
298 | $name = $myts->htmlSpecialChars($name); |
||
299 | if ($path !== '') { |
||
300 | $path = '' . $name . ' : ' . $path . ''; |
||
301 | } else { |
||
302 | $path = '' . $name . ''; |
||
303 | } |
||
304 | |||
305 | if ($parentid == 0) { |
||
306 | return $path; |
||
307 | } |
||
308 | $path = $this->getUnformattedPathFromId($parentid, $title, $path); |
||
309 | |||
310 | return $path; |
||
311 | } |
||
312 | |||
313 | //generates id path from the root id to a given id |
||
314 | // the path is delimetered with "/" |
||
315 | /** |
||
316 | * @param $sel_id |
||
317 | * @param string $path |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getIdPathFromId($sel_id, $path = '') |
||
321 | { |
||
322 | $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"); |
||
323 | if ($this->db->getRowsNum($result) == 0) { |
||
324 | return $path; |
||
325 | } |
||
326 | list($parentid) = $this->db->fetchRow($result); |
||
327 | $path = '/' . $sel_id . $path . ''; |
||
328 | if ($parentid == 0) { |
||
329 | return $path; |
||
330 | } |
||
331 | $path = $this->getIdPathFromId($parentid, $path); |
||
332 | |||
333 | return $path; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param int $sel_id |
||
338 | * @param string $order |
||
339 | * @param array $parray |
||
340 | * @return array |
||
341 | */ |
||
342 | View Code Duplication | public function getAllChild($sel_id = 0, $order = '', $parray = array()) |
|
343 | { |
||
344 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
345 | if ($order !== '') { |
||
346 | $sql .= " ORDER BY $order"; |
||
347 | } |
||
348 | $result = $this->db->query($sql); |
||
349 | $count = $this->db->getRowsNum($result); |
||
350 | if ($count == 0) { |
||
351 | return $parray; |
||
352 | } |
||
353 | while ($row = $this->db->fetchArray($result)) { |
||
354 | array_push($parray, $row); |
||
355 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
356 | } |
||
357 | |||
358 | return $parray; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param int $sel_id |
||
363 | * @param string $order |
||
364 | * @param array $parray |
||
365 | * @param string $r_prefix |
||
366 | * @return array |
||
367 | */ |
||
368 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
||
386 | } |
||
387 | } |
||
388 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths