Passed
Push — master ( dcbd4a...4bb7b0 )
by Michael
02:37
created

b_marquee_newbb()   D

Complexity

Conditions 13
Paths 26

Size

Total Lines 109
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 109
rs 4.9922

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
2
/**
3
 * ****************************************************************************
4
 * marquee - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard (http://www.herve-thouzard.com)
6
 *
7
 * You may not change or alter any portion of this comment or credits
8
 * of supporting developers from this source code or any supporting source code
9
 * which is considered copyrighted (c) material of the original comment or credit authors.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * @copyright          Hervé Thouzard (http://www.herve-thouzard.com)
15
 * @license            http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package            marquee
17
 * @author             Hervé Thouzard (http://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
41
    /** @var \XoopsModuleHandler $moduleHandler */
42
    $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

42
    $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
43
    $newbb         = $moduleHandler->getByDirname('newbb');
44
    $newbbVersion  = (int)$newbb->getInfo('version');
45
46
    if ($newbbVersion >= 2) {
47
        $order        = 't.topic_time';
48
        $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...
49
        /** @var \XoopsModuleHandler $moduleHandler */
50
        $moduleHandler = xoops_getHandler('module');
51
        $newbb         = $moduleHandler->getByDirname('newbb');
52
53
        if (null === $newbbConfig) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newbbConfig seems to be never defined.
Loading history...
54
            /** @var \XoopsConfigHandler $configHandler */
55
            $configHandler = xoops_getHandler('config');
56
            $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...
57
        }
58
59
        if (null === $access_forums) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $access_forums seems to be never defined.
Loading history...
60
            $access_forums = $forumHandler->getForums(0, 'access'); // get all accessible forums
61
        }
62
        $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...
63
        $forumCriteria = ' AND t.forum_id IN (' . implode(',', $validForums) . ')';
64
        unset($access_forums);
65
        $approveCriteria = ' AND t.approved = 1 AND p.approved = 1';
66
        $db              = \XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug introduced by
The type XoopsDatabaseFactory 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...
67
        $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 '
68
                           . $db->prefix('bb_topics')
69
                           . ' t, '
70
                           . $db->prefix('bb_forums')
71
                           . ' f, '
72
                           . $db->prefix('bb_posts')
73
                           . ' p LEFT JOIN '
74
                           . $db->prefix('users')
75
                           . ' u ON u.uid = p.uid WHERE f.forum_id=t.forum_id '
76
                           . $forumCriteria
77
                           . $approveCriteria
78
                           . ' AND t.topic_last_post_id=p.post_id ORDER BY '
79
                           . $order
80
                           . ' DESC';
81
        $result          = $db->query($query, $limit, 0);
82
        if (!$result) {
83
            return false;
84
        }
85
        $rows = [];
86
        while (false !== ($row = $db->fetchArray($result))) {
87
            $rows[] = $row;
88
        }
89
        if (count($rows) < 1) {
90
            return false;
91
        }
92
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Bug introduced by
The type MyTextSanitizer 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...
93
94
        foreach ($rows as $arr) {
95
            $title = $myts->htmlSpecialChars($arr['topic_title']);
96
            if ($itemsSize > 0) {
97
                $title = xoops_substr($title, 0, $itemsSize + 3);
0 ignored issues
show
Bug introduced by
The function xoops_substr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

97
                $title = /** @scrutinizer ignore-call */ xoops_substr($title, 0, $itemsSize + 3);
Loading history...
98
            }
99
100
            $block[] = [
101
                'date'     => formatTimestamp($arr['topic_time'], $dateFormat),
0 ignored issues
show
Bug introduced by
The function formatTimestamp was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

101
                'date'     => /** @scrutinizer ignore-call */ formatTimestamp($arr['topic_time'], $dateFormat),
Loading history...
102
                'category' => $arr['forum_name'],
103
                'author'   => $arr['uid'],
104
                'title'    => $title,
105
                '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>'
0 ignored issues
show
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
106
            ];
107
        }
108
    } else { // Newbb 1
109
        $db    = \XoopsDatabaseFactory::getDatabaseConnection();
110
        $myts  = \MyTextSanitizer::getInstance();
111
        $order = 't.topic_time';
112
        $time  = $tmpuser = '';
113
        $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 '
114
                 . $db->prefix('bb_topics')
115
                 . ' t, '
116
                 . $db->prefix('bb_forums')
117
                 . ' f WHERE f.forum_id=t.forum_id AND f.forum_type <> 1 ORDER BY '
118
                 . $order
119
                 . ' DESC';
120
        if (!$result = $db->query($query, $limit, 0)) {
121
            return false;
122
        }
123
        while (false !== ($arr = $db->fetchArray($result))) {
124
            $lastpostername = $db->query('SELECT post_id, uid FROM ' . $db->prefix('bb_posts') . ' WHERE post_id = ' . $arr['topic_last_post_id']);
125
            while (false !== ($tmpdb = $db->fetchArray($lastpostername))) {
126
                $tmpuser = XoopsUser::getUnameFromId($tmpdb['uid']);
0 ignored issues
show
Bug introduced by
The type XoopsUser 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...
127
                $time    = formatTimestamp($arr['topic_time'], $dateFormat);
128
            }
129
            $title = $myts->htmlSpecialChars($arr['topic_title']);
130
            if ($itemsSize > 0) {
131
                $title = xoops_substr($title, 0, $itemsSize + 3);
132
            }
133
134
            $block[] = [
135
                'date'     => $time,
136
                'category' => $arr['forum_name'],
137
                'author'   => $tmpuser,
138
                'title'    => $title,
139
                '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>'
140
            ];
141
        }
142
    }
143
144
    return $block;
145
}
146