newbb_search()   F
last analyzed

Complexity

Conditions 25
Paths 18432

Size

Total Lines 116
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 73
nc 18432
nop 9
dl 0
loc 116
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * NewBB 5.0x,  the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (https://xoops.org)
6
 * @license        GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
7
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>, irmtfan <[email protected]>
8
 * @since          4.3
9
 * @package        module::newbb
10
 */
11
12
use XoopsModules\Newbb;
13
14
// completely rewrite by irmtfan - remove hardcode database access, solve order issues, add post_text & topic_id, add highlight and reduce queries
15
16
require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
17
18
/**
19
 * @param                $queryarray
20
 * @param                $andor
21
 * @param                $limit
22
 * @param                $offset
23
 * @param                $userid
24
 * @param int            $forums
25
 * @param int|string     $sortby
26
 * @param string         $searchin
27
 * @param \CriteriaCompo $criteriaExtra
28
 * @return array
29
 */
30
function newbb_search(
31
    $queryarray,
32
    $andor,
33
    $limit,
34
    $offset,
35
    $userid,
36
    $forums = 0,
37
    $sortby = 0,
38
    $searchin = 'both',
39
    \CriteriaCompo $criteriaExtra = null
40
) {
41
    global $myts, $xoopsDB;
42
    // irmtfan - in XOOPSCORE/search.php $GLOBALS['xoopsModuleConfig'] is not set
43
    if (!isset($GLOBALS['xoopsModuleConfig'])) {
44
        $GLOBALS['xoopsModuleConfig'] = newbbLoadConfig();
45
    }
46
    // irmtfan - in XOOPSCORE/search.php $xoopsModule is not set
47
    if (!is_object($GLOBALS['xoopsModule']) && is_object($GLOBALS['module'])
48
        && 'newbb' === $GLOBALS['module']->getVar('dirname')) {
49
        $GLOBALS['xoopsModule'] = $GLOBALS['module'];
50
    }
51
    /** @var Newbb\ForumHandler $forumHandler */
52
    $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
53
    $validForums  = $forumHandler->getIdsByValues($forums); // can we use view permission? $forumHandler->getIdsByValues($forums, "view")
54
55
    $criteriaPost = new \CriteriaCompo();
56
    $criteriaPost->add(new \Criteria('p.approved', 1), 'AND'); // only active posts
57
58
    $forum_list = []; // get forum lists just for forum names
59
    if (count($validForums) > 0) {
60
        $criteriaPermissions = new \CriteriaCompo();
61
        $criteriaPermissions->add(new \Criteria('p.forum_id', '(' . implode(',', $validForums) . ')', 'IN'), 'AND');
62
        $forum_list = $forumHandler->getAll(new \Criteria('forum_id', '(' . implode(', ', $validForums) . ')', 'IN'), 'forum_name', false);
0 ignored issues
show
Bug introduced by
'forum_name' of type string is incompatible with the type array expected by parameter $fields of XoopsPersistableObjectHandler::getAll(). ( Ignorable by Annotation )

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

62
        $forum_list = $forumHandler->getAll(new \Criteria('forum_id', '(' . implode(', ', $validForums) . ')', 'IN'), /** @scrutinizer ignore-type */ 'forum_name', false);
Loading history...
63
    }
64
65
    if (is_numeric($userid) && 0 !== $userid) {
66
        $criteriaUser = new \CriteriaCompo();
67
        $criteriaUser->add(new \Criteria('p.uid', $userid), 'OR');
68
    } elseif ($userid && is_array($userid)) {
69
        $userid       = array_map('\intval', $userid);
70
        $criteriaUser = new \CriteriaCompo();
71
        $criteriaUser->add(new \Criteria('p.uid', '(' . implode(',', $userid) . ')', 'IN'), 'OR');
72
    }
73
74
    $count = 0;
75
    if (is_array($queryarray)) {
76
        $count = count($queryarray);
77
    }
78
    $highlightKey = '';
79
    if ($count > 0) {
80
        $criteriaKeywords = new \CriteriaCompo();
81
        foreach ($queryarray as $queryTerm) {
82
            $termCriteria  = new \CriteriaCompo();
83
            $queryTermLike = '%' . $xoopsDB->escape($queryTerm) . '%';
84
            if ('title' === $searchin || 'both' === $searchin) {
85
                $termCriteria->add(new \Criteria('p.subject', $queryTermLike, 'LIKE'), 'OR');
86
            }
87
            if ('text' === $searchin || 'both' === $searchin) {
88
                $termCriteria->add(new \Criteria('t.post_text', $queryTermLike, 'LIKE'), 'OR');
89
            }
90
            $criteriaKeywords->add($termCriteria, $andor);
91
        }
92
        // add highlight keywords to post links
93
        $highlightKey = '&amp;keywords=' . implode(' ', $queryarray);
94
        $highlightKey = str_replace(' ', '+', $highlightKey);
95
    }
96
    $criteria = new \CriteriaCompo();
97
    $criteria->add($criteriaPost, 'AND');
98
    if (null !== $criteriaPermissions) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $criteriaPermissions does not seem to be defined for all execution paths leading up to this point.
Loading history...
99
        $criteria->add($criteriaPermissions, 'AND');
100
    }
101
    if (isset($criteriaUser)) {
102
        $criteria->add($criteriaUser, 'AND');
103
    }
104
    if (isset($criteriaKeywords)) {
105
        $criteria->add($criteriaKeywords, 'AND');
106
    }
107
    if (isset($criteriaExtra)) {
108
        $criteria->add($criteriaExtra, 'AND');
109
    }
110
    //$criteria->setLimit($limit); // no need for this
111
    //$criteria->setStart($offset); // no need for this
112
113
    if (empty($sortby)) {
114
        $sortby = 'p.post_time';
115
    }
116
    $criteria->setSort($sortby);
117
    $order = 'ASC';
118
    if ('p.post_time' === $sortby) {
119
        $order = 'DESC';
120
    }
121
    $criteria->setOrder($order);
122
123
    /** @var Newbb\PostHandler $postHandler */
124
    $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post');
125
    $posts       = $postHandler->getPostsByLimit($criteria, $limit, $offset);
126
127
    $ret = [];
128
    $i   = 0;
129
    foreach (array_keys($posts) as $id) {
130
        /** @var Newbb\Post $post */
131
        $post                  = $posts[$id];
132
        $post_data             = $post->getPostBody();
133
        $ret[$i]['topic_id']   = $post->getVar('topic_id');
134
        $ret[$i]['link']       = XOOPS_URL . '/modules/newbb/viewtopic.php?post_id=' . $post->getVar('post_id') . $highlightKey; // add highlight key
135
        $ret[$i]['title']      = $post_data['subject'];
136
        $ret[$i]['time']       = $post_data['date'];
137
        $ret[$i]['forum_name'] = $myts->htmlSpecialChars($forum_list[$post->getVar('forum_id')]['forum_name']);
138
        $ret[$i]['forum_link'] = XOOPS_URL . '/modules/newbb/viewforum.php?forum=' . $post->getVar('forum_id');
139
        $ret[$i]['post_text']  = $post_data['text'];
140
        $ret[$i]['uid']        = $post->getVar('uid');
141
        $ret[$i]['poster']     = $post->getVar('uid') ? '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $ret[$i]['uid'] . '">' . $post_data['author'] . '</a>' : $post_data['author'];
142
        ++$i;
143
    }
144
145
    return $ret;
146
}
147