Conditions | 24 |
Paths | > 20000 |
Total Lines | 130 |
Code Lines | 81 |
Lines | 6 |
Ratio | 4.62 % |
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 |
||
189 | protected function &getSearchResults(\Base $f3, \FFCMS\Mappers\Mapper $m): array |
||
190 | { |
||
191 | // set up paging limits |
||
192 | $minPerPage = 10; |
||
193 | $maxPerPage = 100; |
||
194 | $perPage = (int) $f3->get('REQUEST.per_page'); |
||
195 | if ($perPage < $minPerPage) { |
||
196 | $perPage = $minPerPage; |
||
197 | } |
||
198 | if ($perPage > $maxPerPage) { |
||
199 | $perPage = $maxPerPage; |
||
200 | } |
||
201 | |||
202 | $page = $f3->get('REQUEST.page'); |
||
203 | if ($page < 1) { |
||
204 | $page = 1; |
||
205 | } |
||
206 | |||
207 | // fetch data (paging is 0 based) |
||
208 | $allFields = $m->fields(); |
||
209 | |||
210 | // validate order field |
||
211 | $order = self::checkOrderField($f3->get('REQUEST.order'), $allFields); |
||
212 | $validFields = self::checkFieldsExist([$f3->get('REQUEST.fields'), $f3->get('REQUEST.search_fields')], $allFields); |
||
213 | |||
214 | // validated fields to return |
||
215 | $fields = empty($validFields['fields']) ? join(',', $allFields) : $validFields['fields']; |
||
216 | |||
217 | // validated fields to search in, use all if empty |
||
218 | $searchFields = empty($validFields['search_fields']) ? join(',', $allFields) : $validFields['search_fields']; |
||
219 | |||
220 | // get search type |
||
221 | $search = $f3->get('REQUEST.search'); |
||
222 | if (!empty($search)) { |
||
223 | $search = trim(strtolower($search)); |
||
224 | } |
||
225 | $search_type = $f3->get('REQUEST.search_type'); |
||
226 | if (empty($search_type)) { |
||
227 | $search_type = 'exact'; |
||
228 | } elseif ($search_type !== 'exact') { |
||
229 | $search_type = 'fuzzy'; |
||
230 | } |
||
231 | |||
232 | // construct search query |
||
233 | $db = \Registry::get('db'); |
||
234 | $sqlClauses = []; |
||
235 | $searchFieldsArray = preg_split("/[,]/", $searchFields); |
||
236 | View Code Duplication | foreach ($searchFieldsArray as $field) { |
|
1 ignored issue
–
show
|
|||
237 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') = ' . $db->quote($search); |
||
238 | if ($search_type == 'fuzzy') { |
||
239 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') LIKE ' . $db->quote('%' . $search . '%'); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | // get total results |
||
244 | $query = 'SELECT COUNT(*) AS results FROM ' . $db->quotekey($m->table()) . ' WHERE ' . join(' OR ', $sqlClauses); |
||
245 | $data = []; |
||
246 | $rows = $db->exec($query); |
||
247 | $rows = (int) $rows[0]['results']; |
||
248 | if ($rows < 1) { |
||
249 | return $data; |
||
250 | } |
||
251 | |||
252 | // if fewer results than per page, set per_page |
||
253 | if ($page == 1 && $perPage > $rows) { |
||
254 | $perPage = $rows; |
||
255 | } |
||
256 | |||
257 | $pagination = []; |
||
258 | $pagination['count'] = (int) ceil($rows / $perPage); |
||
259 | |||
260 | // too high page number? |
||
261 | if ($page > $pagination['count']) { |
||
262 | $page = $pagination['count']; |
||
263 | } |
||
264 | |||
265 | // set up page URLs |
||
266 | $url = $f3->get('PATH'); |
||
267 | $urlParams = [ |
||
268 | 'per_page' => $perPage, |
||
269 | 'search' => $search, |
||
270 | 'search_type' => $search_type, |
||
271 | ]; |
||
272 | if (!empty($order)) { |
||
273 | $urlParams['order'] = $order; |
||
274 | } |
||
275 | if (!empty($fields)) { |
||
276 | $urlParams['fields'] = $fields; |
||
277 | } |
||
278 | ksort($urlParams); |
||
279 | |||
280 | // previous page url |
||
281 | $prevPage = (1 > $page - 1 ) ? null : $page - 1; |
||
282 | $nextPage = (1 + $page > $pagination['count']) ? null : $page + 1; |
||
283 | |||
284 | $resultsFrom = 1 + ($page * $perPage) - $perPage; |
||
285 | $resultsTo = $resultsFrom + $perPage - 1; |
||
286 | if ($resultsTo > $rows) { |
||
287 | $resultsTo = $rows; |
||
288 | } |
||
289 | |||
290 | // return data |
||
291 | $data['pagination'] = [ |
||
292 | 'url_base' => $this->url($url, $urlParams), |
||
293 | 'url_current' => $this->url($url, $urlParams + ['page' => $page]), |
||
294 | 'url_first' => $this->url($url, $urlParams + ['page' => 1]), |
||
295 | 'url_last' => $this->url($url, $urlParams + ['page' => $pagination['count']]), |
||
296 | 'url_next' => (null == $nextPage) ? null : $this->url($url, $urlParams + ['page' => $nextPage]), |
||
297 | 'url_previous' => (null == $prevPage) ? null : $this->url($url, $urlParams + ['page' => $prevPage]), |
||
298 | 'results' => $rows, |
||
299 | 'results_from' => $resultsFrom, |
||
300 | 'results_to' => $resultsTo, |
||
301 | 'per_page' => $perPage, |
||
302 | 'pages' => $pagination['count'], |
||
303 | 'page' => $page, |
||
304 | 'object' => $m->table(), |
||
305 | 'fields' => preg_split("/[,]/", $fields), |
||
306 | 'view' => $f3->get('REQUEST.view') |
||
307 | ]; |
||
308 | |||
309 | // retrieve results |
||
310 | $query = 'SELECT * FROM ' . $db->quotekey($m->table()) . ' WHERE ' . join(' OR ', $sqlClauses); |
||
311 | $query .= sprintf(' LIMIT %d,%d', (1 == $page) ? 0 : ($page - 1) * $perPage, $perPage); |
||
312 | $results = $db->exec($query); |
||
313 | foreach ($results as $row) { |
||
314 | $data['objects'][] = $m->castFields($fields, $row); |
||
315 | } |
||
316 | |||
317 | return $data; |
||
318 | } |
||
319 | |||
321 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.