Completed
Branch master (caa4fc)
by Michael
06:53 queued 04:04
created

search.inc.php ➔ news_search()   F

Complexity

Conditions 27
Paths 420

Size

Total Lines 113
Code Lines 79

Duplication

Lines 10
Ratio 8.85 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 27
eloc 79
c 2
b 0
f 0
nc 420
nop 5
dl 10
loc 113
rs 3.4165

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
//                XOOPS - PHP Content Management System                      //
5
//                  Copyright (c) 2000-2016 XOOPS.org                        //
6
//                       <http://xoops.org/>                             //
7
//  ------------------------------------------------------------------------ //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
28
29
/**
30
 * @param $queryarray
31
 * @param $andor
32
 * @param $limit
33
 * @param $offset
34
 * @param $userid
35
 *
36
 * @return array
37
 */
38
function news_search($queryarray, $andor, $limit, $offset, $userid)
39
{
40
    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...
41
    include_once XOOPS_ROOT_PATH . '/modules/news/include/functions.php';
42
    $restricted = news_getmoduleoption('restrictindex');
43
    $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...
44
    $highlight  = news_getmoduleoption('keywordshighlight'); // keywords highlighting
45
46
    $moduleHandler = xoops_getHandler('module');
47
    $module        = $moduleHandler->getByDirname('news');
48
    $modid         = $module->getVar('mid');
49
    $searchparam   = '';
50
51
    $gperm_handler = xoops_getHandler('groupperm');
52
    if (is_object($xoopsUser)) {
53
        $groups = $xoopsUser->getGroups();
54
    } else {
55
        $groups = XOOPS_GROUP_ANONYMOUS;
56
    }
57
58
    $sql =
59
        'SELECT storyid, topicid, uid, title, created FROM ' . $xoopsDB->prefix('news_stories') . ' WHERE (published>0 AND published<=' . time() . ') AND (expired = 0 OR expired > ' . time() . ') ';
60
61
    if ($userid != 0) {
62
        $sql .= ' AND uid=' . $userid . ' ';
63
    }
64
    // because count() returns 1 even if a supplied variable
65
    // is not an array, we must check if $querryarray is really an array
66
    if (is_array($queryarray) && $count = count($queryarray)) {
67
        $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]%')";
68
        for ($i = 1; $i < $count; ++$i) {
69
            $sql .= " $andor ";
70
            $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]%')";
71
        }
72
        $sql .= ') ';
73
        // keywords highlighting
74
        if ($highlight) {
75
            $searchparam = '&keywords=' . urlencode(trim(implode(' ', $queryarray)));
76
        }
77
    }
78
79
    $sql .= 'ORDER BY created DESC';
80
    $result = $xoopsDB->query($sql, $limit, $offset);
81
    $ret    = array();
82
    $i      = 0;
83
    while ($myrow = $xoopsDB->fetchArray($result)) {
84
        $display = true;
85 View Code Duplication
        if ($modid && $gperm_handler) {
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...
86
            if ($restricted && !$gperm_handler->checkRight('news_view', $myrow['topicid'], $groups, $modid)) {
87
                $display = false;
88
            }
89
        }
90
91
        if ($display) {
92
            $ret[$i]['image'] = 'assets/images/news.png';
93
            $ret[$i]['link']  = 'article.php?storyid=' . $myrow['storyid'] . '' . $searchparam;
94
            $ret[$i]['title'] = $myrow['title'];
95
            $ret[$i]['time']  = $myrow['created'];
96
            $ret[$i]['uid']   = $myrow['uid'];
97
            ++$i;
98
        }
99
    }
100
101
    include_once XOOPS_ROOT_PATH . '/modules/news/config.php';
102
    $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...
103
104
    if ($searchincomments && (isset($limit) && $i <= $limit)) {
105
        include_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
106
        $ind = $i;
107
        $sql = 'SELECT com_id, com_modid, com_itemid, com_created, com_uid, com_title, com_text, com_status FROM '
108
               . $xoopsDB->prefix('xoopscomments')
109
               . " WHERE (com_id>0) AND (com_modid=$modid) AND (com_status="
110
               . XOOPS_COMMENT_ACTIVE
111
               . ') ';
112
        if ($userid != 0) {
113
            $sql .= ' AND com_uid=' . $userid . ' ';
114
        }
115
116
        if (is_array($queryarray) && $count = count($queryarray)) {
117
            $sql .= " AND ((com_title LIKE '%$queryarray[0]%' OR com_text LIKE '%$queryarray[0]%')";
118
            for ($i = 1; $i < $count; ++$i) {
119
                $sql .= " $andor ";
120
                $sql .= "(com_title LIKE '%$queryarray[$i]%' OR com_text LIKE '%$queryarray[$i]%')";
121
            }
122
            $sql .= ') ';
123
        }
124
        $i = $ind;
125
        $sql .= 'ORDER BY com_created DESC';
126
        $result = $xoopsDB->query($sql, $limit, $offset);
127
        while ($myrow = $xoopsDB->fetchArray($result)) {
128
            $display = true;
129 View Code Duplication
            if ($modid && $gperm_handler) {
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...
130
                if ($restricted && !$gperm_handler->checkRight('news_view', $myrow['com_itemid'], $groups, $modid)) {
131
                    $display = false;
132
                }
133
            }
134
            if ($i + 1 > $limit) {
135
                $display = false;
136
            }
137
138
            if ($display) {
139
                $ret[$i]['image'] = 'assets/images/news.png';
140
                $ret[$i]['link']  = 'article.php?storyid=' . $myrow['com_itemid'] . '' . $searchparam;
141
                $ret[$i]['title'] = $myrow['com_title'];
142
                $ret[$i]['time']  = $myrow['com_created'];
143
                $ret[$i]['uid']   = $myrow['com_uid'];
144
                ++$i;
145
            }
146
        }
147
    }
148
149
    return $ret;
150
}
151