Completed
Pull Request — master (#16)
by Michael
01:51
created

search.inc.php ➔ news_search()   F

Complexity

Conditions 27
Paths 420

Size

Total Lines 109
Code Lines 74

Duplication

Lines 10
Ratio 9.17 %

Importance

Changes 0
Metric Value
cc 27
eloc 74
nc 420
nop 5
dl 10
loc 109
rs 3.4165
c 0
b 0
f 0

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
 * 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 http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team
18
 */
19
20
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
22
/**
23
 * @param $queryarray
24
 * @param $andor
25
 * @param $limit
26
 * @param $offset
27
 * @param $userid
28
 *
29
 * @return array
30
 */
31
function news_search($queryarray, $andor, $limit, $offset, $userid)
32
{
33
    global $xoopsDB, $xoopsUser;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
34
    require_once XOOPS_ROOT_PATH . '/modules/news/class/utility.php';
35
    $restricted = NewsUtility::getModuleOption('restrictindex');
36
    $highlight  = false;
0 ignored issues
show
Unused Code introduced by
$highlight is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
    $highlight  = NewsUtility::getModuleOption('keywordshighlight'); // keywords highlighting
38
39
    /** @var XoopsModuleHandler $moduleHandler */
40
    $moduleHandler = xoops_getHandler('module');
41
    $module        = $moduleHandler->getByDirname('news');
42
    $modid         = $module->getVar('mid');
43
    $searchparam   = '';
44
45
    $gpermHandler = xoops_getHandler('groupperm');
46
    if (is_object($xoopsUser)) {
47
        $groups = $xoopsUser->getGroups();
48
    } else {
49
        $groups = XOOPS_GROUP_ANONYMOUS;
50
    }
51
52
    $sql = 'SELECT storyid, topicid, uid, title, created FROM ' . $xoopsDB->prefix('news_stories') . ' WHERE (published>0 AND published<=' . time() . ') AND (expired = 0 OR expired > ' . time() . ') ';
53
54
    if (0 != $userid) {
55
        $sql .= ' AND uid=' . $userid . ' ';
56
    }
57
    // because count() returns 1 even if a supplied variable
58
    // is not an array, we must check if $querryarray is really an array
59
    if (is_array($queryarray) && $count = count($queryarray)) {
60
        $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]%')";
61
        for ($i = 1; $i < $count; ++$i) {
62
            $sql .= " $andor ";
63
            $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]%')";
64
        }
65
        $sql .= ') ';
66
        // keywords highlighting
67
        if ($highlight) {
68
            $searchparam = '&keywords=' . urlencode(trim(implode(' ', $queryarray)));
69
        }
70
    }
71
72
    $sql    .= 'ORDER BY created DESC';
73
    $result = $xoopsDB->query($sql, $limit, $offset);
74
    $ret    = [];
75
    $i      = 0;
76
    while ($myrow = $xoopsDB->fetchArray($result)) {
77
        $display = true;
78 View Code Duplication
        if ($modid && $gpermHandler) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            if ($restricted && !$gpermHandler->checkRight('news_view', $myrow['topicid'], $groups, $modid)) {
80
                $display = false;
81
            }
82
        }
83
84
        if ($display) {
85
            $ret[$i]['image'] = 'assets/images/news.png';
86
            $ret[$i]['link']  = 'article.php?storyid=' . $myrow['storyid'] . '' . $searchparam;
87
            $ret[$i]['title'] = $myrow['title'];
88
            $ret[$i]['time']  = $myrow['created'];
89
            $ret[$i]['uid']   = $myrow['uid'];
90
            ++$i;
91
        }
92
    }
93
94
    require_once XOOPS_ROOT_PATH . '/modules/news/config.php';
95
    $searchincomments = $cfg['config_search_comments'];
0 ignored issues
show
Bug introduced by
The variable $cfg does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
96
97
    if ($searchincomments && (isset($limit) && $i <= $limit)) {
98
        require_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
99
        $ind = $i;
100
        $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 . ') ';
101
        if (0 != $userid) {
102
            $sql .= ' AND com_uid=' . $userid . ' ';
103
        }
104
105
        if (is_array($queryarray) && $count = count($queryarray)) {
106
            $sql .= " AND ((com_title LIKE '%$queryarray[0]%' OR com_text LIKE '%$queryarray[0]%')";
107
            for ($i = 1; $i < $count; ++$i) {
108
                $sql .= " $andor ";
109
                $sql .= "(com_title LIKE '%$queryarray[$i]%' OR com_text LIKE '%$queryarray[$i]%')";
110
            }
111
            $sql .= ') ';
112
        }
113
        $i      = $ind;
114
        $sql    .= 'ORDER BY com_created DESC';
115
        $result = $xoopsDB->query($sql, $limit, $offset);
116
        while ($myrow = $xoopsDB->fetchArray($result)) {
117
            $display = true;
118 View Code Duplication
            if ($modid && $gpermHandler) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
                if ($restricted && !$gpermHandler->checkRight('news_view', $myrow['com_itemid'], $groups, $modid)) {
120
                    $display = false;
121
                }
122
            }
123
            if ($i + 1 > $limit) {
124
                $display = false;
125
            }
126
127
            if ($display) {
128
                $ret[$i]['image'] = 'assets/images/news.png';
129
                $ret[$i]['link']  = 'article.php?storyid=' . $myrow['com_itemid'] . '' . $searchparam;
130
                $ret[$i]['title'] = $myrow['com_title'];
131
                $ret[$i]['time']  = $myrow['com_created'];
132
                $ret[$i]['uid']   = $myrow['com_uid'];
133
                ++$i;
134
            }
135
        }
136
    }
137
138
    return $ret;
139
}
140