Completed
Pull Request — master (#489)
by Richard
10:59
created

search.php ➔ publisher_search_show()   F

Complexity

Conditions 39
Paths > 20000

Size

Total Lines 99
Code Lines 80

Duplication

Lines 11
Ratio 11.11 %
Metric Value
cc 39
eloc 80
nc 2415919105
nop 1
dl 11
loc 99
rs 2.0063

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
23
include_once dirname(__DIR__) . '/include/common.php';
24
25
function publisher_search_show($options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

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

Loading history...
Coding Style introduced by
publisher_search_show uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
publisher_search_show uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
26
{
27
    $block = array();
28
    $xoops = Xoops::getInstance();
0 ignored issues
show
Unused Code introduced by
$xoops 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...
29
    $publisher = Publisher::getInstance();
30
    $categories = $publisher->getCategoryHandler()->getCategoriesForSearch();
31
    if (count($categories) == 0) return $block;
32
33
    $andor = isset($_POST["andor"]) ? $_POST["andor"] : (isset($_GET["andor"]) ? $_GET["andor"] : "");
34
35
    $category = isset($_POST["category"]) ? $_POST["category"] : (isset($_GET["category"]) ? $_GET["category"] : null);
36
    $username = isset($_POST["uname"]) ? $_POST["uname"] : (isset($_GET["uname"]) ? $_GET["uname"] : null);
37
    $searchin = isset($_POST["searchin"]) ? $_POST["searchin"] : (isset($_GET["searchin"]) ? explode("|", $_GET["searchin"]) : array());
38
    $sortby = isset($_POST["sortby"]) ? $_POST["sortby"] : (isset($_GET["sortby"]) ? $_GET["sortby"] : null);
39
    $term = isset($_POST["term"]) ? $_POST["term"] : (isset($_GET["term"]) ? $_GET["term"] : "");
40
41 View Code Duplication
    if (empty($category) || (is_array($category) && in_array("all", $category))) {
42
        $category = array();
43
    } else {
44
        $category = (!is_array($category)) ? explode(",", $category) : $category;
45
        $category = array_map("intval", $category);
46
    }
47
48
    $andor = (in_array(strtoupper($andor), array("OR", "AND", "EXACT"))) ? strtoupper($andor) : "OR";
49
    $sortby = (in_array(strtolower($sortby), array("itemid", "datesub", "title", "categoryid"))) ? strtolower($sortby) : "itemid";
50
51
    /* type */
52
    $type_select = "<select name=\"andor\">";
53
    $type_select .= "<option value=\"OR\"";
54
    if ("OR" === $andor) $type_select .= " selected=\"selected\"";
55
    $type_select .= ">" . XoopsLocale::ANY_OR . "</option>";
56
    $type_select .= "<option value=\"AND\"";
57
    if ("AND" === $andor) $type_select .= " selected=\"selected\"";
58
    $type_select .= ">" . XoopsLocale::ALL . "</option>";
59
    $type_select .= "<option value=\"EXACT\"";
60
    if ("exact" === $andor) $type_select .= " selected=\"selected\"";
61
    $type_select .= ">" . XoopsLocale::EXACT_MATCH . "</option>";
62
    $type_select .= "</select>";
63
64
    /* category */
65
66
    $select_category = "<select name=\"category[]\" size=\"5\" multiple=\"multiple\" width=\"150\" style=\"width:150px;\">";
67
    $select_category .= "<option value=\"all\"";
68
    if (empty($category) || count($category) == 0) $select_category .= "selected=\"selected\"";
69
    $select_category .= ">" . XoopsLocale::ALL . "</option>";
70 View Code Duplication
    foreach ($categories as $id => $cat) {
71
        $select_category .= "<option value=\"" . $id . "\"";
72
        if (in_array($id, $category)) $select_category .= "selected=\"selected\"";
73
        $select_category .= ">" . $cat . "</option>";
74
    }
75
    $select_category .= "</select>";
76
77
    /* scope */
78
    $searchin_select = "";
79
    $searchin_select .= "<input type=\"checkbox\" name=\"searchin[]\" value=\"title\"";
80
    if (in_array("title", $searchin)) $searchin_select .= " checked";
81
    $searchin_select .= " />" . _CO_PUBLISHER_TITLE . "&nbsp;&nbsp;";
82
    $searchin_select .= "<input type=\"checkbox\" name=\"searchin[]\" value=\"subtitle\"";
83
    if (in_array("subtitle", $searchin)) $searchin_select .= " checked";
84
    $searchin_select .= " />" . _CO_PUBLISHER_SUBTITLE . "&nbsp;&nbsp;";
85
    $searchin_select .= "<input type=\"checkbox\" name=\"searchin[]\" value=\"summary\"";
86
    if (in_array("summary", $searchin)) $searchin_select .= " checked";
87
    $searchin_select .= " />" . _CO_PUBLISHER_SUMMARY . "&nbsp;&nbsp;";
88
    $searchin_select .= "<input type=\"checkbox\" name=\"searchin[]\" value=\"text\"";
89
    if (in_array("body", $searchin)) $searchin_select .= " checked";
90
    $searchin_select .= " />" . _CO_PUBLISHER_BODY . "&nbsp;&nbsp;";
91
    $searchin_select .= "<input type=\"checkbox\" name=\"searchin[]\" value=\"keywords\"";
92
    if (in_array("meta_keywords", $searchin)) $searchin_select .= " checked";
93
    $searchin_select .= " />" . _CO_PUBLISHER_ITEM_META_KEYWORDS . "&nbsp;&nbsp;";
94
    $searchin_select .= "<input type=\"checkbox\" name=\"searchin[]\" value=\"all\"";
95
    if (in_array("all", $searchin) || empty($searchin)) $searchin_select .= " checked";
96
    $searchin_select .= " />" . XoopsLocale::ALL . "&nbsp;&nbsp;";
97
98
    /* sortby */
99
    $sortby_select = "<select name=\"sortby\">";
100
    $sortby_select .= "<option value=\"itemid\"";
101
    if ("itemid" === $sortby || empty($sortby)) $sortby_select .= " selected=\"selected\"";
102
    $sortby_select .= ">" . XoopsLocale::NONE . "</option>";
103
    $sortby_select .= "<option value=\"datesub\"";
104
    if ("datesub" === $sortby) $sortby_select .= " selected=\"selected\"";
105
    $sortby_select .= ">" . _CO_PUBLISHER_DATESUB . "</option>";
106
    $sortby_select .= "<option value=\"title\"";
107
    if ("title" === $sortby) $sortby_select .= " selected=\"selected\"";
108
    $sortby_select .= ">" . _CO_PUBLISHER_TITLE . "</option>";
109
    $sortby_select .= "<option value=\"categoryid\"";
110
    if ("categoryid" === $sortby) $sortby_select .= " selected=\"selected\"";
111
    $sortby_select .= ">" . _CO_PUBLISHER_CATEGORY . "</option>";
112
    $sortby_select .= "</select>";
113
114
    $block["type_select"] = $type_select;
115
    $block["searchin_select"] = $searchin_select;
116
    $block["category_select"] = $select_category;
117
    $block["sortby_select"] = $sortby_select;
118
    $block["search_term"] = $term;
119
    $block["search_user"] = $username;
120
    $block["publisher_url"] = PUBLISHER_URL;
121
122
    return $block;
123
}
124