b_marquee_newbb()   C
last analyzed

Complexity

Conditions 13
Paths 26

Size

Total Lines 102
Code Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 81
c 0
b 0
f 0
nc 26
nop 3
dl 0
loc 102
rs 5.7078

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
/**
4
 * ****************************************************************************
5
 * marquee - MODULE FOR XOOPS
6
 * Copyright (c) Hervé Thouzard (https://www.herve-thouzard.com)
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          Hervé Thouzard (https://www.herve-thouzard.com)
16
 * @license            GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @author             Hervé Thouzard (https://www.herve-thouzard.com)
18
 * ****************************************************************************
19
 *
20
 * @param $limit
21
 * @param $dateFormat
22
 * @param $itemsSize
23
 *
24
 * @return array
25
 */
26
27
use XoopsModules\Newbb;
0 ignored issues
show
Bug introduced by
The type XoopsModules\Newbb 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...
28
29
// Script to list recent posts from Newbb 1 & 2
30
/**
31
 * @param $limit
32
 * @param $dateFormat
33
 * @param $itemsSize
34
 * @return array|false
35
 */
36
function b_marquee_newbb($limit, $dateFormat, $itemsSize)
37
{
38
    //    require_once XOOPS_ROOT_PATH . '/modules/marquee/class/Utility.php';
39
    $block = [];
40
    /** @var \XoopsModuleHandler $moduleHandler */
41
    $moduleHandler = xoops_getHandler('module');
42
    $newbb         = $moduleHandler->getByDirname('newbb');
43
    $newbbVersion  = (int)$newbb->getInfo('version');
44
    if ($newbbVersion >= 2) {
45
        $order        = 't.topic_time';
46
        $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
0 ignored issues
show
Bug introduced by
The type XoopsModules\Newbb\Helper 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...
47
        /** @var \XoopsModuleHandler $moduleHandler */
48
        $moduleHandler = xoops_getHandler('module');
49
        $newbb         = $moduleHandler->getByDirname('newbb');
50
        if (null === $newbbConfig) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newbbConfig seems to be never defined.
Loading history...
51
            /** @var \XoopsConfigHandler $configHandler */
52
            $configHandler = xoops_getHandler('config');
53
            $newbbConfig   = $configHandler->getConfigsByCat(0, $newbb->getVar('mid'));
0 ignored issues
show
Unused Code introduced by
The assignment to $newbbConfig is dead and can be removed.
Loading history...
54
        }
55
        if (null === $access_forums) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $access_forums seems to be never defined.
Loading history...
56
            $access_forums = $forumHandler->getForums(0, 'access'); // get all accessible forums
57
        }
58
        $validForums   = array_keys($access_forums);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $access_forums does not seem to be defined for all execution paths leading up to this point.
Loading history...
59
        $forumCriteria = ' AND t.forum_id IN (' . implode(',', $validForums) . ')';
60
        unset($access_forums);
61
        $approveCriteria = ' AND t.approved = 1 AND p.approved = 1';
62
        $db              = \XoopsDatabaseFactory::getDatabaseConnection();
63
        $query           = 'SELECT t.*, f.forum_name, f.allow_subject_prefix, p.post_id, p.icon, p.uid, p.poster_name, u.uname, u.name FROM '
64
                           . $db->prefix('bb_topics')
65
                           . ' t, '
66
                           . $db->prefix('bb_forums')
67
                           . ' f, '
68
                           . $db->prefix('bb_posts')
69
                           . ' p LEFT JOIN '
70
                           . $db->prefix('users')
71
                           . ' u ON u.uid = p.uid WHERE f.forum_id=t.forum_id '
72
                           . $forumCriteria
73
                           . $approveCriteria
74
                           . ' AND t.topic_last_post_id=p.post_id ORDER BY '
75
                           . $order
76
                           . ' DESC';
77
        $result          = $db->query($query, $limit, 0);
78
        if (!$result) {
79
            return false;
80
        }
81
        $rows = [];
82
        while (false !== ($row = $db->fetchArray($result))) {
83
            $rows[] = $row;
84
        }
85
        if (count($rows) < 1) {
86
            return false;
87
        }
88
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
89
        foreach ($rows as $arr) {
90
            $title = htmlspecialchars($arr['topic_title'], ENT_QUOTES | ENT_HTML5);
91
            if ($itemsSize > 0) {
92
                $title = xoops_substr($title, 0, $itemsSize + 3);
93
            }
94
            $block[] = [
95
                'date'     => formatTimestamp($arr['topic_time'], $dateFormat),
96
                'category' => $arr['forum_name'],
97
                'author'   => $arr['uid'],
98
                'title'    => $title,
99
                'link'     => "<a href='" . XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $arr['topic_id'] . '&amp;post_id=' . $arr['post_id'] . '#forumpost' . $arr['post_id'] . "'>" . $title . '</a>',
100
            ];
101
        }
102
    } else { // Newbb 1
103
        $db    = \XoopsDatabaseFactory::getDatabaseConnection();
104
        $myts  = \MyTextSanitizer::getInstance();
105
        $order = 't.topic_time';
106
        $time  = $tmpuser = '';
107
        $query = 'SELECT t.topic_id, t.topic_title, t.topic_last_post_id, t.topic_time, t.topic_views, t.topic_replies, t.forum_id, f.forum_name FROM '
108
                 . $db->prefix('bb_topics')
109
                 . ' t, '
110
                 . $db->prefix('bb_forums')
111
                 . ' f WHERE f.forum_id=t.forum_id AND f.forum_type <> 1 ORDER BY '
112
                 . $order
113
                 . ' DESC';
114
        if (!$result = $db->query($query, $limit, 0)) {
115
            return false;
116
        }
117
        while (false !== ($arr = $db->fetchArray($result))) {
118
            $lastpostername = $db->query('SELECT post_id, uid FROM ' . $db->prefix('bb_posts') . ' WHERE post_id = ' . $arr['topic_last_post_id']);
119
            while (false !== ($tmpdb = $db->fetchArray($lastpostername))) {
120
                $tmpuser = \XoopsUser::getUnameFromId($tmpdb['uid']);
121
                $time    = formatTimestamp($arr['topic_time'], $dateFormat);
122
            }
123
            $title = htmlspecialchars($arr['topic_title'], ENT_QUOTES | ENT_HTML5);
124
            if ($itemsSize > 0) {
125
                $title = xoops_substr($title, 0, $itemsSize + 3);
126
            }
127
            $block[] = [
128
                'date'     => $time,
129
                'category' => $arr['forum_name'],
130
                'author'   => $tmpuser,
131
                'title'    => $title,
132
                'link'     => "<a href='" . XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $arr['topic_id'] . '&amp;forum=' . $arr['forum_id'] . '&amp;post_id=' . $arr['topic_last_post_id'] . '#forumpost' . $arr['topic_last_post_id'] . "'>" . $title . '</a>',
133
            ];
134
        }
135
    }
136
137
    return $block;
138
}
139