publisher_search_show()   F
last analyzed

Complexity

Conditions 36
Paths > 20000

Size

Total Lines 141
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 36
eloc 99
nc 1769473
nop 1
dl 0
loc 141
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 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
/**
13
 * @copyright       XOOPS Project (https://xoops.org)
14
 * @license         https://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @since           1.0
16
 * @author          trabis <[email protected]>
17
 * @author          phppp
18
 */
19
20
use Xmf\Request;
1 ignored issue
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
use XoopsModules\Publisher\CategoryHandler;
22
use XoopsModules\Publisher\Helper;
23
24
require_once \dirname(__DIR__) . '/include/common.php';
25
26
/**
27
 * @param $options
28
 *
29
 * @return array
30
 */
31
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

31
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...
32
{
33
    $block  = [];
34
    $helper = Helper::getInstance();
35
    /** @var CategoryHandler $categoryHandler */
36
    $categoryHandler = $helper->getHandler('Category');
37
    $categories      = $categoryHandler->getCategoriesForSearch();
38
    if (0 === count($categories)) {
39
        return $block;
40
    }
41
42
    xoops_loadLanguage('search');
43
44
    $andor    = Request::getString('andor', Request::getString('andor', '', 'GET'), 'POST');
45
    $username = Request::getString('uname', Request::getString('uname', null, 'GET'), 'POST');
46
    //  $searchin = isset($_POST["searchin"]) ? $_POST["searchin"] : (isset($_GET["searchin"]) ? explode("|", $_GET["searchin"]) : array());
47
    //  $searchin = Request::getArray('searchin', (explode("|", Request::getString('searchin', array(), 'GET'))), 'POST');
48
49
    $searchin = Request::getArray('searchin', '', 'POST');
50
    if (!isset($searchin)) {
51
        $searchin = Request::getString('searchin', [], 'GET');
0 ignored issues
show
Bug introduced by
array() of type array is incompatible with the type string expected by parameter $default of Xmf\Request::getString(). ( Ignorable by Annotation )

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

51
        $searchin = Request::getString('searchin', /** @scrutinizer ignore-type */ [], 'GET');
Loading history...
52
        $searchin = isset($searchin) ? explode('|', $searchin) : [];
53
    }
54
55
    $sortby = Request::getString('sortby', Request::getString('sortby', null, 'GET'), 'POST');
56
    $term   = Request::getString('term', Request::getString('term', '', 'GET'));
57
58
    //mb TODO simplify next lines with category
59
    $category = Request::getArray('category', [], 'POST') ?: Request::getArray('category', null, 'GET');
60
    if (empty($category) || (is_array($category) && in_array('all', $category, true))) {
61
        $category = [];
62
    } else {
63
        $category = (!is_array($category)) ? explode(',', $category) : $category;
64
        $category = array_map('\intval', $category);
65
    }
66
67
    $andor  = in_array(mb_strtoupper($andor), ['OR', 'AND', 'EXACT'], true) ? \mb_strtoupper($andor) : 'OR';
68
    $sortby = in_array(mb_strtolower($sortby), ['itemid', 'datesub', 'title', 'categoryid'], true) ? \mb_strtolower($sortby) : 'itemid';
69
70
    /* type */
71
    $typeSelect = '<select name="andor">';
72
    $typeSelect .= '<option value="OR"';
73
    if ('OR' === $andor) {
74
        $typeSelect .= ' selected="selected"';
75
    }
76
    $typeSelect .= '>' . _SR_ANY . '</option>';
77
    $typeSelect .= '<option value="AND"';
78
    if ('AND' === $andor) {
79
        $typeSelect .= ' selected="selected"';
80
    }
81
    $typeSelect .= '>' . _SR_ALL . '</option>';
82
    $typeSelect .= '<option value="EXACT"';
83
    if ('exact' === $andor) {
84
        $typeSelect .= ' selected="selected"';
85
    }
86
    $typeSelect .= '>' . _SR_EXACT . '</option>';
87
    $typeSelect .= '</select>';
88
89
    /* category */
90
91
    $categorySelect = '<select name="category[]" size="5" multiple="multiple" width="150" style="width:150px;">';
92
    $categorySelect .= '<option value="all"';
93
    if (empty($category) || 0 === count($category)) {
94
        $categorySelect .= 'selected="selected"';
95
    }
96
    $categorySelect .= '>' . _ALL . '</option>';
97
    foreach ($categories as $id => $cat) {
98
        $categorySelect .= '<option value="' . $id . '"';
99
        if (in_array($id, $category, true)) {
100
            $categorySelect .= 'selected="selected"';
101
        }
102
        $categorySelect .= '>' . $cat . '</option>';
103
    }
104
    unset($id);
105
    $categorySelect .= '</select>';
106
107
    /* scope */
108
    $searchSelect = '<input type="checkbox" name="searchin[]" value="title"';
109
    if (is_array($searchin) && in_array('title', $searchin, true)) {
110
        $searchSelect .= ' checked';
111
    }
112
    $searchSelect .= '>' . _CO_PUBLISHER_TITLE . '&nbsp;&nbsp;';
113
    $searchSelect .= '<input type="checkbox" name="searchin[]" value="subtitle"';
114
    if (is_array($searchin) && in_array('subtitle', $searchin, true)) {
115
        $searchSelect .= ' checked';
116
    }
117
    $searchSelect .= '>' . _CO_PUBLISHER_SUBTITLE . '&nbsp;&nbsp;';
118
    $searchSelect .= '<input type="checkbox" name="searchin[]" value="summary"';
119
    if (is_array($searchin) && in_array('summary', $searchin, true)) {
120
        $searchSelect .= ' checked';
121
    }
122
    $searchSelect .= '>' . _CO_PUBLISHER_SUMMARY . '&nbsp;&nbsp;';
123
    $searchSelect .= '<input type="checkbox" name="searchin[]" value="text"';
124
    if (is_array($searchin) && in_array('body', $searchin, true)) {
125
        $searchSelect .= ' checked';
126
    }
127
    $searchSelect .= '>' . _CO_PUBLISHER_BODY . '&nbsp;&nbsp;';
128
    $searchSelect .= '<input type="checkbox" name="searchin[]" value="keywords"';
129
    if (is_array($searchin) && in_array('meta_keywords', $searchin, true)) {
130
        $searchSelect .= ' checked';
131
    }
132
    $searchSelect .= '>' . _CO_PUBLISHER_ITEM_META_KEYWORDS . '&nbsp;&nbsp;';
133
    $searchSelect .= '<input type="checkbox" name="searchin[]" value="all"';
134
    if (empty($searchin) || (is_array($searchin) && in_array('all', $searchin, true))) {
135
        $searchSelect .= ' checked';
136
    }
137
    $searchSelect .= '>' . _ALL . '&nbsp;&nbsp;';
138
139
    /* sortby */
140
    $sortbySelect = '<select name="sortby">';
141
    $sortbySelect .= '<option value="itemid"';
142
    if ('itemid' === $sortby || empty($sortby)) {
143
        $sortbySelect .= ' selected="selected"';
144
    }
145
    $sortbySelect .= '>' . _NONE . '</option>';
146
    $sortbySelect .= '<option value="datesub"';
147
    if ('datesub' === $sortby) {
148
        $sortbySelect .= ' selected="selected"';
149
    }
150
    $sortbySelect .= '>' . _CO_PUBLISHER_DATESUB . '</option>';
151
    $sortbySelect .= '<option value="title"';
152
    if ('title' === $sortby) {
153
        $sortbySelect .= ' selected="selected"';
154
    }
155
    $sortbySelect .= '>' . _CO_PUBLISHER_TITLE . '</option>';
156
    $sortbySelect .= '<option value="categoryid"';
157
    if ('categoryid' === $sortby) {
158
        $sortbySelect .= ' selected="selected"';
159
    }
160
    $sortbySelect .= '>' . _CO_PUBLISHER_CATEGORY . '</option>';
161
    $sortbySelect .= '</select>';
162
163
    $block['typeSelect']     = $typeSelect;
164
    $block['searchSelect']   = $searchSelect;
165
    $block['categorySelect'] = $categorySelect;
166
    $block['sortbySelect']   = $sortbySelect;
167
    $block['search_term']    = htmlspecialchars($term, ENT_QUOTES | ENT_HTML5);
168
    $block['search_user']    = $username;
169
    $block['publisher_url']  = PUBLISHER_URL;
170
171
    return $block;
172
}
173