publisher_search_show()   F
last analyzed

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 130
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 95
nc 37748737
nop 1
dl 0
loc 130
rs 0
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       The XUUPS Project http://sourceforge.net/projects/xuups/
14
 * @license         GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package         Publisher
16
 * @subpackage      Blocks
17
 * @since           1.0
18
 * @author          trabis <[email protected]>
19
 * @author          phppp
20
 * @version         $Id$
21
 */
22
use XoopsModules\Publisher\Helper;
23
24
require_once dirname(__DIR__) . '/include/common.php';
25
26
/**
27
 * @param $options
28
 * @return array
29
 */
30
function publisher_search_show($options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

30
function publisher_search_show(/** @scrutinizer ignore-unused */ $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
{
32
    $block = [];
33
    $xoops = Xoops::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $xoops is dead and can be removed.
Loading history...
34
    $helper = Helper::getInstance();
35
    $categories = $helper->getCategoryHandler()->getCategoriesForSearch();
36
    if (0 == count($categories)) {
37
        return $block;
38
    }
39
40
    $andor = $_POST['andor'] ?? (isset($_GET['andor']) ? $_GET['andor'] : '');
41
42
    $category = $_POST['category'] ?? (isset($_GET['category']) ? $_GET['category'] : null);
43
    $username = $_POST['uname'] ?? (isset($_GET['uname']) ? $_GET['uname'] : null);
44
    $searchin = $_POST['searchin'] ?? (isset($_GET['searchin']) ? explode('|', $_GET['searchin']) : []);
45
    $sortby = $_POST['sortby'] ?? (isset($_GET['sortby']) ? $_GET['sortby'] : null);
46
    $term = $_POST['term'] ?? (isset($_GET['term']) ? $_GET['term'] : '');
47
48
    if (empty($category) || (is_array($category) && in_array('all', $category))) {
49
        $category = [];
50
    } else {
51
        $category = (!is_array($category)) ? explode(',', $category) : $category;
52
        $category = array_map('\intval', $category);
53
    }
54
55
    $andor = in_array(mb_strtoupper($andor), ['OR', 'AND', 'EXACT']) ? mb_strtoupper($andor) : 'OR';
56
    $sortby = in_array(mb_strtolower($sortby), ['itemid', 'datesub', 'title', 'categoryid']) ? mb_strtolower($sortby) : 'itemid';
0 ignored issues
show
Bug introduced by
It seems like $sortby can also be of type null; however, parameter $string of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

56
    $sortby = in_array(mb_strtolower(/** @scrutinizer ignore-type */ $sortby), ['itemid', 'datesub', 'title', 'categoryid']) ? mb_strtolower($sortby) : 'itemid';
Loading history...
57
58
    /* type */
59
    $type_select = '<select name="andor">';
60
    $type_select .= '<option value="OR"';
61
    if ('OR' === $andor) {
62
        $type_select .= ' selected="selected"';
63
    }
64
    $type_select .= '>' . XoopsLocale::ANY_OR . '</option>';
65
    $type_select .= '<option value="AND"';
66
    if ('AND' === $andor) {
67
        $type_select .= ' selected="selected"';
68
    }
69
    $type_select .= '>' . XoopsLocale::ALL . '</option>';
70
    $type_select .= '<option value="EXACT"';
71
    if ('exact' === $andor) {
72
        $type_select .= ' selected="selected"';
73
    }
74
    $type_select .= '>' . XoopsLocale::EXACT_MATCH . '</option>';
75
    $type_select .= '</select>';
76
77
    /* category */
78
79
    $select_category = '<select name="category[]" size="5" multiple="multiple" width="150" style="width:150px;">';
80
    $select_category .= '<option value="all"';
81
    if (empty($category) || 0 == count($category)) {
82
        $select_category .= 'selected="selected"';
83
    }
84
    $select_category .= '>' . XoopsLocale::ALL . '</option>';
85
    foreach ($categories as $id => $cat) {
86
        $select_category .= '<option value="' . $id . '"';
87
        if (in_array($id, $category)) {
88
            $select_category .= 'selected="selected"';
89
        }
90
        $select_category .= '>' . $cat . '</option>';
91
    }
92
    $select_category .= '</select>';
93
94
    /* scope */
95
    $searchin_select = '';
96
    $searchin_select .= '<input type="checkbox" name="searchin[]" value="title"';
97
    if (in_array('title', $searchin)) {
98
        $searchin_select .= ' checked';
99
    }
100
    $searchin_select .= '>' . _CO_PUBLISHER_TITLE . '&nbsp;&nbsp;';
101
    $searchin_select .= '<input type="checkbox" name="searchin[]" value="subtitle"';
102
    if (in_array('subtitle', $searchin)) {
103
        $searchin_select .= ' checked';
104
    }
105
    $searchin_select .= '>' . _CO_PUBLISHER_SUBTITLE . '&nbsp;&nbsp;';
106
    $searchin_select .= '<input type="checkbox" name="searchin[]" value="summary"';
107
    if (in_array('summary', $searchin)) {
108
        $searchin_select .= ' checked';
109
    }
110
    $searchin_select .= '>' . _CO_PUBLISHER_SUMMARY . '&nbsp;&nbsp;';
111
    $searchin_select .= '<input type="checkbox" name="searchin[]" value="text"';
112
    if (in_array('body', $searchin)) {
113
        $searchin_select .= ' checked';
114
    }
115
    $searchin_select .= '>' . _CO_PUBLISHER_BODY . '&nbsp;&nbsp;';
116
    $searchin_select .= '<input type="checkbox" name="searchin[]" value="keywords"';
117
    if (in_array('meta_keywords', $searchin)) {
118
        $searchin_select .= ' checked';
119
    }
120
    $searchin_select .= '>' . _CO_PUBLISHER_ITEM_META_KEYWORDS . '&nbsp;&nbsp;';
121
    $searchin_select .= '<input type="checkbox" name="searchin[]" value="all"';
122
    if (in_array('all', $searchin) || empty($searchin)) {
123
        $searchin_select .= ' checked';
124
    }
125
    $searchin_select .= '>' . XoopsLocale::ALL . '&nbsp;&nbsp;';
126
127
    /* sortby */
128
    $sortby_select = '<select name="sortby">';
129
    $sortby_select .= '<option value="itemid"';
130
    if ('itemid' === $sortby || empty($sortby)) {
131
        $sortby_select .= ' selected="selected"';
132
    }
133
    $sortby_select .= '>' . XoopsLocale::NONE . '</option>';
134
    $sortby_select .= '<option value="datesub"';
135
    if ('datesub' === $sortby) {
136
        $sortby_select .= ' selected="selected"';
137
    }
138
    $sortby_select .= '>' . _CO_PUBLISHER_DATESUB . '</option>';
139
    $sortby_select .= '<option value="title"';
140
    if ('title' === $sortby) {
141
        $sortby_select .= ' selected="selected"';
142
    }
143
    $sortby_select .= '>' . _CO_PUBLISHER_TITLE . '</option>';
144
    $sortby_select .= '<option value="categoryid"';
145
    if ('categoryid' === $sortby) {
146
        $sortby_select .= ' selected="selected"';
147
    }
148
    $sortby_select .= '>' . _CO_PUBLISHER_CATEGORY . '</option>';
149
    $sortby_select .= '</select>';
150
151
    $block['type_select'] = $type_select;
152
    $block['searchin_select'] = $searchin_select;
153
    $block['category_select'] = $select_category;
154
    $block['sortby_select'] = $sortby_select;
155
    $block['search_term'] = $term;
156
    $block['search_user'] = $username;
157
    $block['publisher_url'] = PUBLISHER_URL;
158
159
    return $block;
160
}
161