Conditions | 36 |
Paths | > 20000 |
Total Lines | 141 |
Code Lines | 99 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
31 | function publisher_search_show($options) |
||
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'); |
||
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 . ' '; |
||
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 . ' '; |
||
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 . ' '; |
||
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 . ' '; |
||
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 . ' '; |
||
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 . ' '; |
||
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 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: