Conditions | 32 |
Paths | > 20000 |
Total Lines | 146 |
Code Lines | 91 |
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 |
||
220 | protected function &getSearchResults(\Base $f3, \FFCMS\Mappers\Mapper $m, string $users_uuid = null): array |
||
221 | { |
||
222 | // set up paging limits |
||
223 | $minPerPage = 10; |
||
224 | $maxPerPage = 100; |
||
225 | $perPage = (int) $f3->get('REQUEST.per_page'); |
||
226 | if ($perPage < $minPerPage) { |
||
227 | $perPage = $minPerPage; |
||
228 | } |
||
229 | if ($perPage > $maxPerPage) { |
||
230 | $perPage = $maxPerPage; |
||
231 | } |
||
232 | |||
233 | $page = $f3->get('REQUEST.page'); |
||
234 | if ($page < 1) { |
||
235 | $page = 1; |
||
236 | } |
||
237 | |||
238 | // fetch data (paging is 0 based) |
||
239 | $allFields = $m->fields(); |
||
240 | |||
241 | // validate order field |
||
242 | $order = self::checkOrderField($f3->get('REQUEST.order'), $allFields); |
||
243 | $validFields = self::checkFieldsExist([$f3->get('REQUEST.fields'), $f3->get('REQUEST.search_fields')], $allFields); |
||
244 | |||
245 | // validated fields to return |
||
246 | $fields = empty($validFields['fields']) ? join(',', $allFields) : $validFields['fields']; |
||
247 | |||
248 | // validated fields to search in, use all if empty |
||
249 | $searchFields = empty($validFields['search_fields']) ? join(',', $allFields) : $validFields['search_fields']; |
||
250 | |||
251 | // get search type |
||
252 | $search = $f3->get('REQUEST.search'); |
||
253 | if (!empty($search)) { |
||
254 | $search = trim(strtolower($search)); |
||
255 | } |
||
256 | $search_type = $f3->get('REQUEST.search_type'); |
||
257 | if (empty($search_type)) { |
||
258 | $search_type = 'exact'; |
||
259 | } elseif ($search_type !== 'exact') { |
||
260 | $search_type = 'fuzzy'; |
||
261 | } |
||
262 | |||
263 | // construct search query |
||
264 | $db = \Registry::get('db'); |
||
265 | $sqlClauses = []; |
||
266 | $searchFieldsArray = preg_split("/[,]/", $searchFields); |
||
267 | foreach ($searchFieldsArray as $field) { |
||
268 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') = ' . $db->quote($search); |
||
269 | if ($search_type == 'fuzzy') { |
||
270 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') LIKE ' . $db->quote('%' . $search . '%'); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | |||
275 | // get total results |
||
276 | $isAdmin = $f3->get('isAdmin'); |
||
277 | $query = 'SELECT COUNT(*) AS results FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
278 | if (in_array('users_uuid', $allFields) && !empty($users_uuid)) { |
||
279 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
280 | } elseif ($isAdmin && empty($users_uuid)) { |
||
281 | $query .= join(' OR ', $sqlClauses); |
||
282 | } |
||
283 | |||
284 | $data = []; |
||
285 | $rows = $db->exec($query); |
||
286 | $rows = (int) $rows[0]['results']; |
||
287 | if ($rows < 1) { |
||
288 | return $data; |
||
289 | } |
||
290 | |||
291 | // if fewer results than per page, set per_page |
||
292 | if ($page == 1 && $perPage > $rows) { |
||
293 | $perPage = $rows; |
||
294 | } |
||
295 | |||
296 | $pagination = []; |
||
297 | $pagination['count'] = (int) ceil($rows / $perPage); |
||
298 | |||
299 | // too high page number? |
||
300 | if ($page > $pagination['count']) { |
||
301 | $page = $pagination['count']; |
||
302 | } |
||
303 | |||
304 | // set up page URLs |
||
305 | $url = $f3->get('PATH'); |
||
306 | $urlParams = [ |
||
307 | 'per_page' => $perPage, |
||
308 | 'search' => $search, |
||
309 | 'search_type' => $search_type, |
||
310 | ]; |
||
311 | if (!empty($order)) { |
||
312 | $urlParams['order'] = $order; |
||
313 | } |
||
314 | if (!empty($fields)) { |
||
315 | $urlParams['fields'] = $fields; |
||
316 | } |
||
317 | ksort($urlParams); |
||
318 | |||
319 | // previous page url |
||
320 | $prevPage = (1 > $page - 1 ) ? null : $page - 1; |
||
321 | $nextPage = (1 + $page > $pagination['count']) ? null : $page + 1; |
||
322 | |||
323 | $resultsFrom = 1 + ($page * $perPage) - $perPage; |
||
324 | $resultsTo = $resultsFrom + $perPage - 1; |
||
325 | if ($resultsTo > $rows) { |
||
326 | $resultsTo = $rows; |
||
327 | } |
||
328 | |||
329 | // return data |
||
330 | $data['pagination'] = [ |
||
331 | 'url_base' => $this->url($url, $urlParams), |
||
332 | 'url_current' => $this->url($url, $urlParams + ['page' => $page]), |
||
333 | 'url_first' => $this->url($url, $urlParams + ['page' => 1]), |
||
334 | 'url_last' => $this->url($url, $urlParams + ['page' => $pagination['count']]), |
||
335 | 'url_next' => (null == $nextPage) ? null : $this->url($url, $urlParams + ['page' => $nextPage]), |
||
336 | 'url_previous' => (null == $prevPage) ? null : $this->url($url, $urlParams + ['page' => $prevPage]), |
||
337 | 'results' => $rows, |
||
338 | 'results_from' => $resultsFrom, |
||
339 | 'results_to' => $resultsTo, |
||
340 | 'per_page' => $perPage, |
||
341 | 'pages' => $pagination['count'], |
||
342 | 'page' => $page, |
||
343 | 'object' => $m->table(), |
||
344 | 'fields' => preg_split("/[,]/", $fields), |
||
345 | 'view' => $f3->get('REQUEST.view') |
||
346 | ]; |
||
347 | |||
348 | |||
349 | // retrieve results |
||
350 | $query = 'SELECT * FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
351 | if (empty($users_uuid)) { |
||
352 | $query .= join(' OR ', $sqlClauses); |
||
353 | } else { |
||
354 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
355 | } |
||
356 | $query .= sprintf(' LIMIT %d,%d', (1 == $page) ? 0 : ($page - 1) * $perPage, $perPage); |
||
357 | |||
358 | $adminView = $isAdmin || ($isAdmin && 'admin' == $f3->get('REQUEST.view')); |
||
359 | $results = $db->exec($query); |
||
360 | foreach ($results as $row) { |
||
361 | $data['objects'][] = $adminView ? $m->castFields($fields, $row) : $m->exportArray($fields, $row); |
||
362 | } |
||
363 | |||
364 | return $data; |
||
365 | } |
||
366 | |||
368 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.