Passed
Push — master ( 6d0bec...700d12 )
by Michael
05:09 queued 02:42
created

XoopsTopic   F

Complexity

Total Complexity 75

Size/Duplication

Total Lines 389
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 75
eloc 173
c 1
b 0
f 0
dl 0
loc 389
rs 2.4

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllChildTopicsId() 0 6 1
A getFirstChildTopics() 0 12 4
A setTopicImgurl() 0 3 1
A makeTopic() 0 4 2
A getAllChildTopics() 0 12 4
A delete() 0 4 1
A prefix() 0 7 2
A __construct() 0 11 3
A topic_imgurl() 0 15 5
A setTopicTitle() 0 3 1
A getChildTopicsTreeArray() 0 12 4
A getNiceTopicPathFromId() 0 6 1
A getTopic() 0 6 1
A topic_title() 0 15 5
A getTopicsList() 0 13 2
A topic_id() 0 3 1
A topicExists() 0 10 2
A usePermission() 0 4 1
A topic_pid() 0 3 1
A makeTopicSelBox() 0 9 3
F store() 0 97 29
A setTopicPid() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like XoopsTopic 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 XoopsTopic, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace XoopsModules\News;
4
5
/**
6
 * XOOPS news topic
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
 * @package         kernel
18
 * @since           2.0.0
19
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
20
 * @deprecated
21
 */
22
23
//$GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopstopic.php' is deprecated since XOOPS 2.5.4, please create your own class instead.");
24
25
// require_once XOOPS_ROOT_PATH . '/modules/news/class/xoopstree.php';
26
27
use XoopsModules\News;
28
29
/**
30
 * Class XoopsTopic
31
 */
32
class XoopsTopic
33
{
34
    public $table;
35
    public $topic_id;
36
    public $topic_pid;
37
    public $topic_title;
38
    public $topic_imgurl;
39
    public $prefix; // only used in topic tree
40
    public $use_permission = false;
41
    public $mid; // module id used for setting permission
42
43
    /**
44
     * @param     $table
45
     * @param int $topicid
46
     */
47
    public function __construct($table, $topicid = 0)
48
    {
49
        /** @var \XoopsMySQLDatabase $db */
50
        $this->db    = \XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
        $this->table = $table;
52
        if (\is_array($topicid)) {
0 ignored issues
show
introduced by
The condition is_array($topicid) is always false.
Loading history...
53
            $this->makeTopic($topicid);
54
        } elseif (0 != $topicid) {
55
            $this->getTopic((int)$topicid);
56
        } else {
57
            $this->topic_id = $topicid;
58
        }
59
    }
60
61
    /**
62
     * @param $value
63
     */
64
    public function setTopicTitle($value)
65
    {
66
        $this->topic_title = $value;
67
    }
68
69
    /**
70
     * @param $value
71
     */
72
    public function setTopicImgurl($value)
73
    {
74
        $this->topic_imgurl = $value;
75
    }
76
77
    /**
78
     * @param $value
79
     */
80
    public function setTopicPid($value)
81
    {
82
        $this->topic_pid = $value;
83
    }
84
85
    /**
86
     * @param $topicid
87
     */
88
    public function getTopic($topicid)
89
    {
90
        $topicid = (int)$topicid;
91
        $sql     = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . '';
92
        $array   = $this->db->fetchArray($this->db->query($sql));
93
        $this->makeTopic($array);
94
    }
95
96
    /**
97
     * @param $array
98
     */
99
    public function makeTopic($array)
100
    {
101
        foreach ($array as $key => $value) {
102
            $this->$key = $value;
103
        }
104
    }
105
106
    /**
107
     * @param $mid
108
     */
109
    public function usePermission($mid)
110
    {
111
        $this->mid            = $mid;
112
        $this->use_permission = true;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function store()
119
    {
120
        $myts   = \MyTextSanitizer::getInstance();
121
        $title  = '';
122
        $imgurl = '';
123
        if (isset($this->topic_title) && '' !== $this->topic_title) {
124
            $title = $myts->addSlashes($this->topic_title);
125
        }
126
        if (isset($this->topic_imgurl) && '' !== $this->topic_imgurl) {
127
            $imgurl = $myts->addSlashes($this->topic_imgurl);
128
        }
129
        if (!isset($this->topic_pid) || !\is_numeric($this->topic_pid)) {
130
            $this->topic_pid = 0;
131
        }
132
        if (empty($this->topic_id)) {
133
            $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
134
            $sql            = \sprintf("INSERT INTO `%s` (topic_id, topic_pid, topic_imgurl, topic_title) VALUES (%u, %u, '%s', '%s')", $this->table, $this->topic_id, $this->topic_pid, $imgurl, $title);
135
        } else {
136
            $sql = \sprintf("UPDATE `%s` SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s' WHERE topic_id = %u", $this->table, $this->topic_pid, $imgurl, $title, $this->topic_id);
137
        }
138
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
139
            // TODO: Replace with something else
140
            //            ErrorHandler::show('0022');
141
142
            $helper = \XoopsModules\News\Helper::getHelper($dirname);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dirname seems to be never defined.
Loading history...
143
            $helper->redirect('admin/index.php', 5, $this->db->error());
144
        }
145
        if (true === $this->use_permission) {
146
            if (empty($this->topic_id)) {
147
                $this->topic_id = $this->db->getInsertId();
148
            }
149
            $xt            = new \XoopsTree($this->table, 'topic_id', 'topic_pid');
150
            $parent_topics = $xt->getAllParentId($this->topic_id);
151
            if (!empty($this->m_groups) && \is_array($this->m_groups)) {
0 ignored issues
show
Bug Best Practice introduced by
The property m_groups does not exist on XoopsModules\News\XoopsTopic. Did you maybe forget to declare it?
Loading history...
152
                foreach ($this->m_groups as $m_g) {
153
                    $moderate_topics = \XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
0 ignored issues
show
Bug introduced by
The type XoopsPerms was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
154
                    $add             = true;
155
                    // only grant this permission when the group has this permission in all parent topics of the created topic
156
                    foreach ($parent_topics as $p_topic) {
157
                        if (!\in_array($p_topic, $moderate_topics)) {
158
                            $add = false;
159
                            continue;
160
                        }
161
                    }
162
                    if (true === $add) {
163
                        $xp = new \XoopsPerms();
164
                        $xp->setModuleId($this->mid);
165
                        $xp->setName('ModInTopic');
166
                        $xp->setItemId($this->topic_id);
167
                        $xp->store();
168
                        $xp->addGroup($m_g);
169
                    }
170
                }
171
            }
172
            if (!empty($this->s_groups) && \is_array($this->s_groups)) {
0 ignored issues
show
Bug Best Practice introduced by
The property s_groups does not exist on XoopsModules\News\XoopsTopic. Did you maybe forget to declare it?
Loading history...
173
                foreach ($s_groups as $s_g) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $s_groups seems to be never defined.
Loading history...
174
                    $submit_topics = \XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
175
                    $add           = true;
176
                    foreach ($parent_topics as $p_topic) {
177
                        if (!\in_array($p_topic, $submit_topics)) {
178
                            $add = false;
179
                            continue;
180
                        }
181
                    }
182
                    if (true === $add) {
183
                        $xp = new \XoopsPerms();
184
                        $xp->setModuleId($this->mid);
185
                        $xp->setName('SubmitInTopic');
186
                        $xp->setItemId($this->topic_id);
187
                        $xp->store();
188
                        $xp->addGroup($s_g);
189
                    }
190
                }
191
            }
192
            if (!empty($this->r_groups) && \is_array($this->r_groups)) {
0 ignored issues
show
Bug Best Practice introduced by
The property r_groups does not exist on XoopsModules\News\XoopsTopic. Did you maybe forget to declare it?
Loading history...
193
                foreach ($r_groups as $r_g) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $r_groups seems to be never defined.
Loading history...
194
                    $read_topics = \XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
195
                    $add         = true;
196
                    foreach ($parent_topics as $p_topic) {
197
                        if (!\in_array($p_topic, $read_topics)) {
198
                            $add = false;
199
                            continue;
200
                        }
201
                    }
202
                    if (true === $add) {
203
                        $xp = new \XoopsPerms();
204
                        $xp->setModuleId($this->mid);
205
                        $xp->setName('ReadInTopic');
206
                        $xp->setItemId($this->topic_id);
207
                        $xp->store();
208
                        $xp->addGroup($r_g);
209
                    }
210
                }
211
            }
212
        }
213
214
        return true;
215
    }
216
217
    public function delete()
218
    {
219
        $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->table, $this->topic_id);
220
        $this->db->query($sql);
221
    }
222
223
    /**
224
     * @return int
225
     */
226
    public function topic_id()
227
    {
228
        return $this->topic_id;
229
    }
230
231
    public function topic_pid()
232
    {
233
        return $this->topic_pid;
234
    }
235
236
    /**
237
     * @param string $format
238
     *
239
     * @return mixed
240
     */
241
    public function topic_title($format = 'S')
242
    {
243
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
244
        switch ($format) {
245
            case 'S':
246
            case 'E':
247
                $title = htmlspecialchars($this->topic_title, ENT_QUOTES | ENT_HTML5);
248
                break;
249
            case 'P':
250
            case 'F':
251
                $title = htmlspecialchars($this->topic_title, ENT_QUOTES | ENT_HTML5);
252
                break;
253
        }
254
255
        return $title;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
256
    }
257
258
    /**
259
     * @param string $format
260
     *
261
     * @return mixed
262
     */
263
    public function topic_imgurl($format = 'S')
264
    {
265
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
266
        switch ($format) {
267
            case 'S':
268
            case 'E':
269
                $imgurl = htmlspecialchars($this->topic_imgurl, ENT_QUOTES | ENT_HTML5);
270
                break;
271
            case 'P':
272
            case 'F':
273
                $imgurl = htmlspecialchars($this->topic_imgurl, ENT_QUOTES | ENT_HTML5);
274
                break;
275
        }
276
277
        return $imgurl;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imgurl does not seem to be defined for all execution paths leading up to this point.
Loading history...
278
    }
279
280
    /**
281
     * @return null
282
     */
283
    public function prefix()
284
    {
285
        if (isset($this->prefix)) {
286
            return $this->prefix;
287
        }
288
289
        return null;
290
    }
291
292
    /**
293
     * @return array
294
     */
295
    public function getFirstChildTopics()
296
    {
297
        $ret       = [];
298
        $xt        = new \XoopsTree($this->table, 'topic_id', 'topic_pid');
299
        $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title');
300
        if (\is_array($topic_arr) && \count($topic_arr)) {
301
            foreach ($topic_arr as $topic) {
302
                $ret[] = new self($this->table, $topic);
303
            }
304
        }
305
306
        return $ret;
307
    }
308
309
    /**
310
     * @return array
311
     */
312
    public function getAllChildTopics()
313
    {
314
        $ret       = [];
315
        $xt        = new \XoopsTree($this->table, 'topic_id', 'topic_pid');
316
        $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title');
317
        if (\is_array($topic_arr) && \count($topic_arr)) {
318
            foreach ($topic_arr as $topic) {
319
                $ret[] = new self($this->table, $topic);
320
            }
321
        }
322
323
        return $ret;
324
    }
325
326
    /**
327
     * @return array
328
     */
329
    public function getChildTopicsTreeArray()
330
    {
331
        $ret       = [];
332
        $xt        = new \XoopsTree($this->table, 'topic_id', 'topic_pid');
333
        $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title');
334
        if (\is_array($topic_arr) && \count($topic_arr)) {
335
            foreach ($topic_arr as $topic) {
336
                $ret[] = new self($this->table, $topic);
337
            }
338
        }
339
340
        return $ret;
341
    }
342
343
    /**
344
     * @param int    $none
345
     * @param        $seltopic
346
     * @param string $selname
347
     * @param string $onchange
348
     */
349
    public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '')
350
    {
351
        $xt = new \XoopsModules\News\ObjectTree($this->table, 'topic_id', 'topic_pid');
352
        if (-1 != $seltopic) {
353
            $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);
0 ignored issues
show
Bug introduced by
The method makeMySelBox() does not exist on XoopsModules\News\ObjectTree. Did you maybe mean makeSelBox()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

353
            $xt->/** @scrutinizer ignore-call */ 
354
                 makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
354
        } elseif (!empty($this->topic_id)) {
355
            $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange);
356
        } else {
357
            $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange);
358
        }
359
    }
360
361
    //generates nicely formatted linked path from the root id to a given id
362
363
    /**
364
     * @param $funcURL
365
     *
366
     * @return mixed
367
     */
368
    public function getNiceTopicPathFromId($funcURL)
369
    {
370
        $xt  = new \XoopsModules\News\ObjectTree($this->table, 'topic_id', 'topic_pid');
371
        $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);
0 ignored issues
show
Bug introduced by
The method getNicePathFromId() does not exist on XoopsModules\News\ObjectTree. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

371
        /** @scrutinizer ignore-call */ 
372
        $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
372
373
        return $ret;
374
    }
375
376
    /**
377
     * @return mixed
378
     */
379
    public function getAllChildTopicsId()
380
    {
381
        $xt  = new \XoopsModules\News\ObjectTree($this->table, 'topic_id', 'topic_pid');
382
        $ret = $xt->getAllChildId($this->topic_id, 'topic_title');
0 ignored issues
show
Bug introduced by
The method getAllChildId() does not exist on XoopsModules\News\ObjectTree. Did you maybe mean getAllChild()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

382
        /** @scrutinizer ignore-call */ 
383
        $ret = $xt->getAllChildId($this->topic_id, 'topic_title');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
383
384
        return $ret;
385
    }
386
387
    /**
388
     * @return array
389
     */
390
    public function getTopicsList()
391
    {
392
        $result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM ' . $this->table);
393
        $ret    = [];
394
        $myts   = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
395
        while (false !== ($myrow = $this->db->fetchArray($result))) {
396
            $ret[$myrow['topic_id']] = [
397
                'title' => htmlspecialchars($myrow['topic_title'], ENT_QUOTES | ENT_HTML5),
398
                'pid'   => $myrow['topic_pid'],
399
            ];
400
        }
401
402
        return $ret;
403
    }
404
405
    /**
406
     * @param $pid
407
     * @param $title
408
     *
409
     * @return bool
410
     */
411
    public function topicExists($pid, $title)
412
    {
413
        $sql = 'SELECT COUNT(*) FROM ' . $this->table . ' WHERE topic_pid = ' . (int)$pid . " AND topic_title = '" . \trim($title) . "'";
414
        $rs  = $this->db->query($sql);
415
        [$count] = $this->db->fetchRow($rs);
416
        if ($count > 0) {
417
            return true;
418
        }
419
420
        return false;
421
    }
422
}
423