Completed
Push — master ( fd0c8a...c024a6 )
by
unknown
03:33 queued 01:53
created

search.inc.php ➔ smallworld_search()   C

Complexity

Conditions 13
Paths 192

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 192
nop 6
dl 0
loc 71
rs 5.3127
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
 * SmallWorld
14
 *
15
 * @copyright    The XOOPS Project (https://xoops.org)
16
 * @copyright    2011 Culex
17
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
18
 * @package      SmallWorld
19
 * @since        1.0
20
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
21
 */
22
use Xoopsmodules\smallworld;
23
require_once XOOPS_ROOT_PATH . '/modules/smallworld/class/class_collector.php';
24
require_once XOOPS_ROOT_PATH . '/modules/smallworld/include/functions.php';
25
26
/**
27
 * @param        $queryarray
28
 * @param        $andor
29
 * @param        $limit
30
 * @param        $offset
31
 * @param        $userid
32
 * @param string $sortby
33
 * @return array
34
 */
35
function smallworld_search($queryarray, $andor, $limit, $offset, $userid, $sortby = 'created DESC')
36
{
37
    global $xoopsDB, $xoopsConfig, $myts, $xoopsUser;
38
39
    if (file_exists(XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $xoopsConfig['language'] . '/main.php')) {
40
        require_once XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $xoopsConfig['language'] . '/main.php';
41
    } else {
42
        require_once XOOPS_ROOT_PATH . '/modules/smallworld/language/english/main.php';
43
    }
44
45
    $moduleHandler = xoops_getHandler('module');
46
    $module        = $moduleHandler->getByDirname('smallworld');
47
    $modid         = $module->getVar('mid');
0 ignored issues
show
Unused Code introduced by
$modid 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...
48
    $searchparam   = '';
0 ignored issues
show
Unused Code introduced by
$searchparam 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...
49
    $highlight     = false;
50
51
    $gpermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Unused Code introduced by
$gpermHandler 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...
52
    if (is_object($xoopsUser)) {
53
        $groups    =& $xoopsUser->getGroups();
54
        $id        = $xoopsUser->getVar('uid');
55
        $Wall      = new smallworld\WallUpdates();
56
        $followers = Smallworld_array_flatten($Wall->getFollowers($id), 0);
57
    } else {
58
        $id        = 0;
59
        $groups    = XOOPS_GROUP_ANONYMOUS;
60
        $followers = [];
61
    }
62
63
    if ($id > 0 && '' != $id) {
64
        $sql = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $xoopsDB->prefix('smallworld_messages') . ' M, ' . $xoopsDB->prefix('smallworld_user') . ' U WHERE M.uid_fk=U.userid';
65
    } else {
66
        $sql = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $xoopsDB->prefix('smallworld_messages') . ' M, ' . $xoopsDB->prefix('smallworld_user') . ' U WHERE M.uid_fk=U.userid';
67
    }
68
69
    if (0 != $userid) {
70
        $sql .= ' AND M.uid_fk = ' . $userid . ' ';
71
    }
72
    if (is_array($queryarray) && $count = count($queryarray)) {
73
        $sql .= " AND (M.message LIKE '%$queryarray[0]%' OR M.message LIKE '%$queryarray[0]%' OR U.username LIKE '%$queryarray[0]%'";
74
        $sql .= ') ';
75
        // keywords highlighting
76
        if ($highlight) {
77
            $searchparam = '&keywords=' . urlencode(trim(implode(' ', $queryarray)));
0 ignored issues
show
Unused Code introduced by
$searchparam 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...
78
        }
79
    }
80
    $sql    .= 'ORDER BY created DESC';
81
    $result = $xoopsDB->query($sql, $limit, $offset);
82
    $ret    = [];
83
    $i      = 0;
84
    while ($myrow = $xoopsDB->fetchArray($result)) {
85
        if (in_array($myrow['uid_fk'], $followers) || $myrow['uid_fk'] == $id) {
86
            $ret[$i]['image'] = 'images/smallworld_icn.png';
87
            $ret[$i]['link']  = 'permalink.php?ownerid=' . $myrow['uid_fk'] . '&updid=' . $myrow['msg_id'];
88
            if (preg_match('/UPLIMAGE/', $myrow['message'])) {
89
                $ownmsg           = str_replace('UPLIMAGE ', '', $myrow['message']);
90
                $ret[$i]['title'] = $ownmsg;
91
                $ret[$i]['title'] = Smallworld_getName($myrow['uid_fk']) . ' -> ' . _SMALLWORLD_GALLERY;
92
                $ret[$i]['title'] = str_replace(['&lt;', '&gt;'], ['<', '>'], $ret[$i]['title']);
93
            } else {
94
                $ret[$i]['title'] = smallworld_shortenText($myrow['message'], 60);
95
            }
96
            $ret[$i]['time'] = $myrow['created'];
97
            $ret[$i]['uid']  = $myrow['uid_fk'];
98
        } else {
99
            --$i;
100
        }
101
        ++$i;
102
    }
103
104
    return $ret;
105
}
106