news_search()   F
last analyzed

Complexity

Conditions 27
Paths 420

Size

Total Lines 108
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 27
eloc 72
c 1
b 0
f 0
nc 420
nop 5
dl 0
loc 108
rs 0.8055

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
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @author         XOOPS Development Team
16
 */
17
18
use XoopsModules\News;
19
20
/**
21
 * @param $queryarray
22
 * @param $andor
23
 * @param $limit
24
 * @param $offset
25
 * @param $userid
26
 *
27
 * @return array
28
 */
29
function news_search($queryarray, $andor, $limit, $offset, $userid)
30
{
31
    global $xoopsDB, $xoopsUser;
32
    $restricted = News\Utility::getModuleOption('restrictindex');
33
    $highlight  = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $highlight is dead and can be removed.
Loading history...
34
    $highlight  = News\Utility::getModuleOption('keywordshighlight'); // keywords highlighting
35
36
    /** @var \XoopsModuleHandler $moduleHandler */
37
    $moduleHandler = xoops_getHandler('module');
38
    $module        = $moduleHandler->getByDirname('news');
39
    $modid         = $module->getVar('mid');
40
    $searchparam   = '';
41
42
    /** @var \XoopsGroupPermHandler $grouppermHandler */
43
    $grouppermHandler = xoops_getHandler('groupperm');
44
    if (is_object($xoopsUser)) {
45
        $groups = $xoopsUser->getGroups();
46
    } else {
47
        $groups = XOOPS_GROUP_ANONYMOUS;
48
    }
49
50
    $sql = 'SELECT storyid, topicid, uid, title, created FROM ' . $xoopsDB->prefix('news_stories') . ' WHERE (published>0 AND published<=' . time() . ') AND (expired = 0 OR expired > ' . time() . ') ';
51
52
    if (0 != $userid) {
53
        $sql .= ' AND uid=' . $userid . ' ';
54
    }
55
    // because count() returns 1 even if a supplied variable
56
    // is not an array, we must check if $querryarray is really an array
57
    if (is_array($queryarray) && $count = count($queryarray)) {
58
        $sql .= " AND ((hometext LIKE '%$queryarray[0]%' OR bodytext LIKE '%$queryarray[0]%' OR title LIKE '%$queryarray[0]%' OR keywords LIKE '%$queryarray[0]%' OR description LIKE '%$queryarray[0]%')";
59
        for ($i = 1; $i < $count; ++$i) {
60
            $sql .= " $andor ";
61
            $sql .= "(hometext LIKE '%$queryarray[$i]%' OR bodytext LIKE '%$queryarray[$i]%' OR title LIKE '%$queryarray[$i]%' OR keywords LIKE '%$queryarray[$i]%' OR description LIKE '%$queryarray[$i]%')";
62
        }
63
        $sql .= ') ';
64
        // keywords highlighting
65
        if ($highlight) {
66
            $searchparam = '&keywords=' . urlencode(trim(implode(' ', $queryarray)));
67
        }
68
    }
69
70
    $sql    .= 'ORDER BY created DESC';
71
    $result = $xoopsDB->query($sql, $limit, $offset);
72
    $ret    = [];
73
    $i      = 0;
74
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
75
        $display = true;
76
        if ($modid && $grouppermHandler) {
77
            if ($restricted && !$grouppermHandler->checkRight('news_view', $myrow['topicid'], $groups, $modid)) {
78
                $display = false;
79
            }
80
        }
81
82
        if ($display) {
83
            $ret[$i]['image'] = 'assets/images/news.png';
84
            $ret[$i]['link']  = 'article.php?storyid=' . $myrow['storyid'] . $searchparam;
85
            $ret[$i]['title'] = $myrow['title'];
86
            $ret[$i]['time']  = $myrow['created'];
87
            $ret[$i]['uid']   = $myrow['uid'];
88
            ++$i;
89
        }
90
    }
91
92
    require_once XOOPS_ROOT_PATH . '/modules/news/config.php';
93
    $searchincomments = $cfg['config_search_comments'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cfg seems to be never defined.
Loading history...
94
95
    if ($searchincomments && (isset($limit) && $i <= $limit)) {
96
        require XOOPS_ROOT_PATH . '/include/comment_constants.php';
97
        $ind = $i;
98
        $sql = 'SELECT com_id, com_modid, com_itemid, com_created, com_uid, com_title, com_text, com_status FROM ' . $xoopsDB->prefix('xoopscomments') . " WHERE (com_id>0) AND (com_modid=$modid) AND (com_status=" . XOOPS_COMMENT_ACTIVE . ') ';
99
        if (0 != $userid) {
100
            $sql .= ' AND com_uid=' . $userid . ' ';
101
        }
102
103
        if (is_array($queryarray) && $count = count($queryarray)) {
104
            $sql .= " AND ((com_title LIKE '%$queryarray[0]%' OR com_text LIKE '%$queryarray[0]%')";
105
            for ($i = 1; $i < $count; ++$i) {
106
                $sql .= " $andor ";
107
                $sql .= "(com_title LIKE '%$queryarray[$i]%' OR com_text LIKE '%$queryarray[$i]%')";
108
            }
109
            $sql .= ') ';
110
        }
111
        $i      = $ind;
112
        $sql    .= 'ORDER BY com_created DESC';
113
        $result = $xoopsDB->query($sql, $limit, $offset);
114
        while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
115
            $display = true;
116
            if ($modid && $grouppermHandler) {
117
                if ($restricted && !$grouppermHandler->checkRight('news_view', $myrow['com_itemid'], $groups, $modid)) {
118
                    $display = false;
119
                }
120
            }
121
            if ($i + 1 > $limit) {
122
                $display = false;
123
            }
124
125
            if ($display) {
126
                $ret[$i]['image'] = 'assets/images/news.png';
127
                $ret[$i]['link']  = 'article.php?storyid=' . $myrow['com_itemid'] . $searchparam;
128
                $ret[$i]['title'] = $myrow['com_title'];
129
                $ret[$i]['time']  = $myrow['com_created'];
130
                $ret[$i]['uid']   = $myrow['com_uid'];
131
                ++$i;
132
            }
133
        }
134
    }
135
136
    return $ret;
137
}
138