search.inc.php ➔ smallworld_search()   D
last analyzed

Complexity

Conditions 14
Paths 192

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 192
nop 6
dl 0
loc 75
rs 4.9454
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
23
use XoopsModules\Smallworld;
24
25
require_once XOOPS_ROOT_PATH . '/modules/smallworld/include/functions.php';
26
27
/**
28
 * Global search
29
 *
30
 * @param array  $queryarray
31
 * @param string $andor
32
 * @param int    $limit
33
 * @param int    $offset
34
 * @param int    $userid
35
 * @param string $sortby
36
 * @return array
37
 */
38
function smallworld_search($queryarray, $andor, $limit, $offset, $userid, $sortby = 'created DESC')
39
{
40
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
$moduleDirName 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...
41
42
    if (file_exists(XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $GLOBALS['xoopsConfig']['language'] . '/main.php')) {
43
        require_once XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $GLOBALS['xoopsConfig']['language'] . '/main.php';
44
    } else {
45
        require_once XOOPS_ROOT_PATH . '/modules/smallworld/language/english/main.php';
46
    }
47
48
    $moduleHandler = xoops_getHandler('module');
49
    $module        = $moduleHandler->getByDirname('smallworld');
50
    $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...
51
    $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...
52
    $highlight     = false;
53
54
    $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...
55
    if ($GLOBALS['xoopsUser'] && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
56
        $groups    = $GLOBALS['xoopsUser']->getGroups();
0 ignored issues
show
Unused Code introduced by
$groups 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...
57
        $id        = $GLOBALS['xoopsUser']->getVar('uid');
58
        $wall      = new Smallworld\WallUpdates();
59
        $followers = smallworld_array_flatten($wall->getFollowers($id), 0);
60
    } else {
61
        $id        = 0;
62
        $groups    = XOOPS_GROUP_ANONYMOUS;
0 ignored issues
show
Unused Code introduced by
$groups 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...
63
        $followers = [];
64
    }
65
66
    //@todo figure out why this if statement is here both conditions yield the same result
67
    if ($id > 0 && '' != $id) {
68
        $sql = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' U WHERE M.uid_fk=U.userid';
69
    } else {
70
        $sql = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' U WHERE M.uid_fk=U.userid';
71
    }
72
73
    if (0 != (int)$userid) {
74
        $sql .= ' AND M.uid_fk = ' . $userid . ' ';
75
    }
76
    //@todo this needs to be refactored to address when $queryarray has more than 1 element
77
    if (is_array($queryarray) && $count = count($queryarray)) {
78
        $sql .= " AND (M.message LIKE '%$queryarray[0]%' OR M.message LIKE '%$queryarray[0]%' OR U.username LIKE '%$queryarray[0]%'";
79
        $sql .= ') ';
80
        // keywords highlighting
81
        if ($highlight) {
82
            $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...
83
        }
84
    }
85
    $sql    .= 'ORDER BY created DESC';
86
    $result = $GLOBALS['xoopsDB']->query($sql, $limit, $offset);
87
    $ret    = [];
88
    $i      = 0;
89
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
90
        if (in_array($myrow['uid_fk'], $followers) || $myrow['uid_fk'] == $id) {
91
            $ret[$i]['image'] = 'assets/images/smallworld_icn.png';
92
            $ret[$i]['link']  = 'permalink.php?ownerid=' . $myrow['uid_fk'] . '&updid=' . $myrow['msg_id'];
93
            if (preg_match('/UPLIMAGE/', $myrow['message'])) {
94
                // @todo - figure this out, doesn't look right - title value is overwrittern so 'message' never gets displayed
95
                $ownmsg           = str_replace('UPLIMAGE ', '', $myrow['message']);
96
                $ret[$i]['title'] = $ownmsg;
97
                $ret[$i]['title'] = Smallworld\Helper::getInstance()->getHandler('SwUser')->getName($myrow['uid_fk']) . ' -> ' . _SMALLWORLD_GALLERY;
98
                //$ret[$i]['title'] = smallworld_getName($myrow['uid_fk']) . ' -> ' . _SMALLWORLD_GALLERY;
99
                $ret[$i]['title'] = str_replace(['&lt;', '&gt;'], ['<', '>'], $ret[$i]['title']);
100
            } else {
101
                $ret[$i]['title'] = smallworld_shortenText($myrow['message'], 60);
102
            }
103
            $ret[$i]['time'] = $myrow['created'];
104
            $ret[$i]['uid']  = $myrow['uid_fk'];
105
        } else {
106
            --$i;
107
        }
108
        ++$i;
109
    }
110
111
    return $ret;
112
}
113