suico_search()   C
last analyzed

Complexity

Conditions 11
Paths 48

Size

Total Lines 94
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 70
nc 48
nop 5
dl 0
loc 94
rs 6.5077
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 declare(strict_types=1);
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
 * @category        Module
13
 * @copyright       {@link https://xoops.org/ XOOPS Project}
14
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
16
 */
17
/**
18
 * Protection against inclusion outside the site
19
 */
20
if (!defined('XOOPS_ROOT_PATH')) {
21
    exit('XOOPS root path not defined');
22
}
23
/**
24
 * Return search results and show images on userinfo page
25
 *
26
 * @param array  $queryarray the terms to look
27
 * @param string $andor      the conector between the terms to be looked
28
 * @param int    $limit      The number of maximum results
29
 * @param int    $offset     from wich register start
30
 * @param int    $userid     from which user to look
31
 * @return array with all results
32
 */
33
function suico_search(
34
    $queryarray,
35
    $andor,
36
    $limit,
37
    $offset,
38
    $userid
39
) {
40
    global $xoopsDB, $module;
41
    //getting the url to the uploads directory
42
    $moduleHandler = xoops_getHandler('module');
43
    $modulo        = $moduleHandler->getByDirname('suico');
0 ignored issues
show
Bug introduced by
The method getByDirname() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

43
    /** @scrutinizer ignore-call */ 
44
    $modulo        = $moduleHandler->getByDirname('suico');
Loading history...
44
    /** @var \XoopsConfigHandler $configHandler */
45
    $configHandler     = xoops_getHandler('config');
46
    $moduleConfig      = $configHandler->getConfigsByCat(0, $modulo->getVar('mid'));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleConfig is dead and can be removed.
Loading history...
47
    $path_uploadimages = XOOPS_UPLOAD_URL;
48
    $ret               = [];
49
    $sql               = 'SELECT image_id, title, caption,  date_created,  uid_owner, filename FROM ' . $xoopsDB->prefix(
50
            'suico_images'
51
        ) . ' WHERE ';
52
    if (0 !== $userid) {
53
        $sql .= '(uid_owner =' . (int)$userid . ')';
54
    }
55
    // because count() returns 1 even if a supplied variable
56
    // is not an array, we must check if $querryarray is really an array
57
    $count = count($queryarray);
58
    if ($count > 0 && is_array($queryarray)) {
59
        $sql .= " ((title LIKE '%" . $queryarray[0] . "%')";
60
        for ($i = 1; $i < $count; ++$i) {
61
            $sql .= " {$andor} ";
62
            $sql .= "(title LIKE '%" . $queryarray[$i] . "%')";
63
        }
64
        $sql .= ') ';
65
    }
66
    $sql .= 'ORDER BY image_id DESC';
67
    //echo $sql;
68
    //printr($xoopsModules);
69
    $result        = $xoopsDB->query($sql, $limit, $offset);
70
    $i             = 0;
71
    $stringofimage = 'images/search.png">';
72
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
73
        if (0 !== $userid) {
74
            if ($limit > 5) {
75
                $ret[$i]['image'] = "assets/images/search.png'><a href='"
76
                                    . XOOPS_URL
77
                                    . '/modules/suico/album.php?uid='
78
                                    . $myrow['uid_owner']
79
                                    . "'><img src='"
80
                                    . $path_uploadimages
81
                                    . '/suico/images/thumb_'
82
                                    . $myrow['filename']
83
                                    . "'></a><br>"
84
                                    . '<img src='
85
                                    . XOOPS_URL
86
                                    . '/modules/suico/images/search.png';
87
                $ret[$i]['link']  = 'album.php?uid=' . $myrow['uid_owner'];
88
                $ret[$i]['title'] = $myrow['title'];
89
                //$ret[$i]['time'] = $myrow['date_created'];
90
                $ret[$i]['uid'] = $myrow['uid_owner'];
91
            } else {
92
                $stringofimage .= '<a href="' . XOOPS_URL . '/modules/suico/album.php?uid=' . $myrow['uid_owner'] . '" title="' . $myrow['title'] . '"><img src="' . $path_uploadimages . '/suico/images/thumb_' . $myrow['filename'] . '"></a>&nbsp;';
93
            }
94
        } else {
95
            $ret[$i]['image'] = "assets/images/search.png'><a href='"
96
                                . XOOPS_URL
97
                                . '/modules/suico/album.php?uid='
98
                                . $myrow['uid_owner']
99
                                . "'><img src='"
100
                                . $path_uploadimages
101
                                . '/suico/images/thumb_'
102
                                . $myrow['filename']
103
                                . "'></a><br>"
104
                                . "<img src='"
105
                                . XOOPS_URL
106
                                . '/modules/suico/images/search.png';
107
            $ret[$i]['link']  = 'album.php?uid=' . $myrow['uid_owner'];
108
            $ret[$i]['title'] = $myrow['title'];
109
            //$ret[$i]['time'] = $myrow['date_created'];
110
            $ret[$i]['uid'] = $myrow['uid_owner'];
111
        }
112
        $i++;
113
    }
114
    if (0 !== $userid && $i > 0) {
115
        if ($limit < 6) {
116
            $ret             = [];
117
            $ret[0]['title'] = 'See its album';
118
            $ret[0]['time']  = time();
119
            $ret[0]['uid']   = $userid;
120
            $ret[0]['link']  = 'album.php?uid=' . $userid;
121
            $stringofimage   .= '<img src="' . XOOPS_URL . '/modules/suico/assets/images/search.png';
122
            $ret[0]['image'] = $stringofimage;
123
        }
124
    }
125
126
    return $ret;
127
}
128