Conditions | 46 |
Paths | > 20000 |
Total Lines | 190 |
Code Lines | 123 |
Lines | 57 |
Ratio | 30 % |
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 |
||
462 | public function search(\Base $f3, array $params) |
||
463 | { |
||
464 | $isAdmin = $f3->get('isAdmin'); |
||
465 | View Code Duplication | if (!$isAdmin && array_key_exists('id', $params)) { |
|
1 ignored issue
–
show
|
|||
466 | $this->failure('authentication_error', "User does not have permission.", 401); |
||
467 | return $this->setOAuthError('access_denied'); |
||
468 | } elseif ($isAdmin && array_key_exists('id', $params)) { |
||
469 | $users_uuid = $params['id']; |
||
470 | } elseif (!$isAdmin) { |
||
471 | $users_uuid = $f3->get('uuid'); |
||
472 | } |
||
473 | |||
474 | // return raw data for object? |
||
475 | $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view'); |
||
476 | |||
477 | // set up paging limits |
||
478 | $minPerPage = $f3->get('api.paging_min'); |
||
479 | $maxPerPage = $f3->get('api.paging_max'); |
||
480 | $perPage = (int) $f3->get('REQUEST.per_page'); |
||
481 | if ($perPage < $minPerPage) { |
||
482 | $perPage = $minPerPage; |
||
483 | } |
||
484 | if ($perPage > $maxPerPage) { |
||
485 | $perPage = $maxPerPage; |
||
486 | } |
||
487 | |||
488 | $page = $f3->get('REQUEST.page'); |
||
489 | if ($page < 1) { |
||
490 | $page = 1; |
||
491 | } |
||
492 | |||
493 | // fetch data (paging is 0 based) |
||
494 | $m = $this->getMapper(); |
||
495 | $allFields = $m->fields(); |
||
496 | |||
497 | // validate order field |
||
498 | $order = $f3->get('REQUEST.order'); |
||
499 | View Code Duplication | if (!empty($order)) { |
|
1 ignored issue
–
show
|
|||
500 | $orderClauses = empty($order) ? [] : preg_split("/[,]/", $order); |
||
501 | foreach ($orderClauses as $k => $field) { |
||
502 | // split into field, asc/desc |
||
503 | $field = preg_split("/[\s]+/", trim($field)); |
||
504 | if (!in_array($field[0], $allFields)) { |
||
505 | // invalid field |
||
506 | unset($orderClauses[$k]); |
||
507 | continue; |
||
508 | } elseif (count($field) == 1) { |
||
509 | $field[1] = 'asc'; |
||
510 | } elseif (count($field) == 2) { |
||
511 | if (!in_array($field[1], ['asc', 'desc'])) { |
||
512 | $field[1] = 'asc'; |
||
513 | } |
||
514 | } |
||
515 | $orderClauses[$k] = $field[0] . ' ' . $field[1]; |
||
516 | } |
||
517 | $order = join(',', $orderClauses); |
||
518 | } |
||
519 | |||
520 | // fields to return and fields to search - validate |
||
521 | $validFields = []; |
||
522 | View Code Duplication | foreach (['fields', 'search_fields'] as $fieldsList) { |
|
1 ignored issue
–
show
|
|||
523 | $fields = $f3->get('REQUEST.' . $fieldsList); |
||
524 | if (empty($fields)) { |
||
525 | continue; |
||
526 | } |
||
527 | $fields = empty($fields) ? [] : preg_split("/[,]/", $fields); |
||
528 | foreach ($fields as $k => $field) { |
||
529 | if (!in_array($field, $allFields)) { |
||
530 | unset($fields[$k]); |
||
531 | } |
||
532 | } |
||
533 | $validFields[$fieldsList] = join(',', $fields); |
||
534 | } |
||
535 | |||
536 | // validated fields to return |
||
537 | $fields = empty($validFields['fields']) ? join(',', $allFields) : $validFields['fields']; |
||
538 | |||
539 | // validated fields to search in, use all if empty |
||
540 | $searchFields = empty($validFields['search_fields']) ? join(',', $allFields) : $validFields['search_fields']; |
||
541 | |||
542 | // get search type |
||
543 | $search = $f3->get('REQUEST.search'); |
||
544 | if (!empty($search)) { |
||
545 | $search = trim(strtolower($search)); |
||
546 | } |
||
547 | $search_type = $f3->get('REQUEST.search_type'); |
||
548 | if (empty($search_type)) { |
||
549 | $search_type = 'exact'; |
||
550 | } elseif ($search_type !== 'exact') { |
||
551 | $search_type = 'fuzzy'; |
||
552 | } |
||
553 | |||
554 | // construct search query |
||
555 | $db = \Registry::get('db'); |
||
556 | $sqlClauses = []; |
||
557 | $searchFieldsArray = preg_split("/[,]/", $searchFields); |
||
558 | View Code Duplication | foreach ($searchFieldsArray as $field) { |
|
1 ignored issue
–
show
|
|||
559 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') = ' . $db->quote($search); |
||
560 | if ($search_type == 'fuzzy') { |
||
561 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') LIKE ' . $db->quote('%' . $search . '%'); |
||
562 | } |
||
563 | } |
||
564 | |||
565 | // get total results |
||
566 | $query = 'SELECT COUNT(*) AS results FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
567 | View Code Duplication | if (empty($users_uuid)) { |
|
1 ignored issue
–
show
|
|||
568 | $query .= join(' OR ', $sqlClauses); |
||
569 | } else { |
||
570 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
571 | } |
||
572 | $rows = $db->exec($query); |
||
573 | $rows = (int) $rows[0]['results']; |
||
574 | if ($rows < 1) { |
||
575 | $this->failure('sever_error', "No data available for request.", 404); |
||
576 | $this->setOAuthError('server_error'); |
||
577 | return; |
||
578 | } |
||
579 | |||
580 | // if fewer results than per page, set per_page |
||
581 | if ($page == 1 && $perPage > $rows) { |
||
582 | $perPage = $rows; |
||
583 | } |
||
584 | |||
585 | $pagination = []; |
||
586 | $pagination['count'] = ceil($rows / $perPage); |
||
587 | |||
588 | // too high page number? |
||
589 | if ($page > $pagination['count']) { |
||
590 | $page = $pagination['count']; |
||
591 | } |
||
592 | |||
593 | // set up page URLs |
||
594 | $url = $f3->get('PATH'); |
||
595 | $urlParams = [ |
||
596 | 'per_page' => $perPage, |
||
597 | 'search' => $search, |
||
598 | 'search_type' => $search_type, |
||
599 | ]; |
||
600 | if (!empty($order)) { |
||
601 | $urlParams['order'] = $order; |
||
602 | } |
||
603 | if (!empty($adminView)) { |
||
604 | $urlParams['view'] = 'admin'; |
||
605 | } |
||
606 | if (!empty($fields)) { |
||
607 | $urlParams['fields'] = $fields; |
||
608 | } |
||
609 | ksort($urlParams); |
||
610 | |||
611 | // previous page url |
||
612 | $prevPage = (1 > $page - 1 ) ? null : $page - 1; |
||
613 | $nextPage = (1 + $page> $pagination['count']) ? null : $page + 1; |
||
614 | |||
615 | $resultsFrom = 1 + ($page * $perPage) - $perPage; |
||
616 | $resultsTo = $resultsFrom + $perPage - 1; |
||
617 | if ($resultsTo > $rows) { |
||
618 | $resultsTo = $rows; |
||
619 | } |
||
620 | |||
621 | // return data |
||
622 | $this->data['pagination'] = [ |
||
623 | 'url_base' => $this->url($url, $urlParams), |
||
624 | 'url_current' => $this->url($url, $urlParams + ['page' => $page]), |
||
625 | 'url_first' => $this->url($url, $urlParams + ['page' => 1]), |
||
626 | 'url_last' => $this->url($url, $urlParams + ['page' => $pagination['count']]), |
||
627 | 'url_next' => (null == $nextPage) ? null : $this->url($url, $urlParams + ['page' => $nextPage]), |
||
628 | 'url_previous' => (null == $prevPage) ? null : $this->url($url, $urlParams + ['page' => $prevPage]), |
||
629 | 'results' => $rows, |
||
630 | 'results_from' => $resultsFrom, |
||
631 | 'results_to' => $resultsTo, |
||
632 | 'per_page' => $perPage, |
||
633 | 'pages' => $pagination['count'], |
||
634 | 'page' => $page, |
||
635 | 'object' => $m->table(), |
||
636 | 'fields' => preg_split("/[,]/", $fields) |
||
637 | ]; |
||
638 | |||
639 | // retrieve results |
||
640 | $query = 'SELECT * FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
641 | View Code Duplication | if (empty($users_uuid)) { |
|
1 ignored issue
–
show
|
|||
642 | $query .= join(' OR ', $sqlClauses); |
||
643 | } else { |
||
644 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
645 | } |
||
646 | $query .= sprintf(' LIMIT %d,%d', (1 == $page) ? 0 : ($page - 1) * $perPage, $perPage); |
||
647 | $results = $db->exec($query); |
||
648 | foreach ($results as $row) { |
||
649 | $this->data['objects'][] = $adminView ? $m->castFields($fields, $row) : $m->exportArray($fields, $row); |
||
650 | } |
||
651 | } |
||
652 | |||
654 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.