| Conditions | 46 |
| Paths | > 20000 |
| Total Lines | 189 |
| Code Lines | 122 |
| 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 |
||
| 477 | public function search(\Base $f3, array $params) |
||
| 478 | { |
||
| 479 | $isAdmin = $f3->get('isAdmin'); |
||
| 480 | if (!$isAdmin && array_key_exists('id', $params)) { |
||
| 481 | $this->failure('authentication_error', "User does not have permission.", 401); |
||
| 482 | return $this->setOAuthError('access_denied'); |
||
| 483 | } elseif ($isAdmin && array_key_exists('id', $params)) { |
||
| 484 | $users_uuid = $params['id']; |
||
| 485 | } elseif (!$isAdmin) { |
||
| 486 | $users_uuid = $f3->get('uuid'); |
||
| 487 | } |
||
| 488 | |||
| 489 | // return raw data for object? |
||
| 490 | $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view'); |
||
| 491 | |||
| 492 | // set up paging limits |
||
| 493 | $minPerPage = $f3->get('api.paging_min'); |
||
| 494 | $maxPerPage = $f3->get('api.paging_max'); |
||
| 495 | $perPage = (int) $f3->get('REQUEST.per_page'); |
||
| 496 | if ($perPage < $minPerPage) { |
||
| 497 | $perPage = $minPerPage; |
||
| 498 | } |
||
| 499 | if ($perPage > $maxPerPage) { |
||
| 500 | $perPage = $maxPerPage; |
||
| 501 | } |
||
| 502 | |||
| 503 | $page = $f3->get('REQUEST.page'); |
||
| 504 | if ($page < 1) { |
||
| 505 | $page = 1; |
||
| 506 | } |
||
| 507 | |||
| 508 | // fetch data (paging is 0 based) |
||
| 509 | $m = $this->getMapper(); |
||
| 510 | $allFields = $m->fields(); |
||
| 511 | |||
| 512 | // validate order field |
||
| 513 | $order = $f3->get('REQUEST.order'); |
||
| 514 | if (!empty($order)) { |
||
| 515 | $orderClauses = empty($order) ? [] : preg_split("/[,]/", $order); |
||
| 516 | foreach ($orderClauses as $k => $field) { |
||
| 517 | // split into field, asc/desc |
||
| 518 | $field = preg_split("/[\s]+/", trim($field)); |
||
| 519 | if (!in_array($field[0], $allFields)) { |
||
| 520 | // invalid field |
||
| 521 | unset($orderClauses[$k]); |
||
| 522 | continue; |
||
| 523 | } elseif (count($field) == 1) { |
||
| 524 | $field[1] = 'asc'; |
||
| 525 | } elseif (count($field) == 2) { |
||
| 526 | if (!in_array($field[1], ['asc', 'desc'])) { |
||
| 527 | $field[1] = 'asc'; |
||
| 528 | } |
||
| 529 | } |
||
| 530 | $orderClauses[$k] = $field[0] . ' ' . $field[1]; |
||
| 531 | } |
||
| 532 | $order = join(',', $orderClauses); |
||
| 533 | } |
||
| 534 | |||
| 535 | // fields to return and fields to search - validate |
||
| 536 | $validFields = []; |
||
| 537 | foreach (['fields', 'search_fields'] as $fieldsList) { |
||
| 538 | $fields = $f3->get('REQUEST.' . $fieldsList); |
||
| 539 | if (empty($fields)) { |
||
| 540 | continue; |
||
| 541 | } |
||
| 542 | $fields = empty($fields) ? [] : preg_split("/[,]/", $fields); |
||
| 543 | foreach ($fields as $k => $field) { |
||
| 544 | if (!in_array($field, $allFields)) { |
||
| 545 | unset($fields[$k]); |
||
| 546 | } |
||
| 547 | } |
||
| 548 | $validFields[$fieldsList] = join(',', $fields); |
||
| 549 | } |
||
| 550 | |||
| 551 | // validated fields to return |
||
| 552 | $fields = empty($validFields['fields']) ? join(',', $allFields) : $validFields['fields']; |
||
| 553 | |||
| 554 | // validated fields to search in, use all if empty |
||
| 555 | $searchFields = empty($fields) ? join(',', $allFields) : $validFields['searchFields']; |
||
| 556 | |||
| 557 | // get search type |
||
| 558 | $search = $f3->get('REQUEST.search'); |
||
| 559 | if (!empty($search)) { |
||
| 560 | $search = trim(strtolower($search)); |
||
| 561 | } |
||
| 562 | $search_type = $f3->get('REQUEST.search_type'); |
||
| 563 | if (empty($search_type)) { |
||
| 564 | $search_type = 'exact'; |
||
| 565 | } elseif ($search_type !== 'exact') { |
||
| 566 | $search_type = 'fuzzy'; |
||
| 567 | } |
||
| 568 | |||
| 569 | // construct search query |
||
| 570 | $db = \Registry::get('db'); |
||
| 571 | $sqlClauses = []; |
||
| 572 | $searchFieldsArray = preg_split("/[,]/", $searchFields); |
||
| 573 | foreach ($searchFieldsArray as $field) { |
||
| 574 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') = ' . $db->quote($search); |
||
| 575 | if ($search_type == 'fuzzy') { |
||
| 576 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') LIKE ' . $db->quote('%' . $search . '%'); |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | // get total results |
||
| 581 | $query = 'SELECT COUNT(*) AS results FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
| 582 | if (empty($users_uuid)) { |
||
| 583 | $query .= join(' OR ', $sqlClauses); |
||
| 584 | } else { |
||
| 585 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
| 586 | } |
||
| 587 | $rows = $db->exec($query); |
||
| 588 | $rows = (int) $rows[0]['results']; |
||
| 589 | if ($rows < 1) { |
||
| 590 | $this->failure('sever_error', "No data available for request.", 404); |
||
| 591 | $this->setOAuthError('server_error'); |
||
| 592 | return; |
||
| 593 | } |
||
| 594 | |||
| 595 | // if fewer results than per page, set per_page |
||
| 596 | if ($page == 1 && $perPage > $rows) { |
||
| 597 | $perPage = $rows; |
||
| 598 | } |
||
| 599 | |||
| 600 | $pagination['count'] = ceil($rows / $perPage); |
||
| 601 | |||
| 602 | // too high page number? |
||
| 603 | if ($page > $pagination['count']) { |
||
| 604 | $page = $pagination['count']; |
||
| 605 | } |
||
| 606 | |||
| 607 | // set up page URLs |
||
| 608 | $url = $f3->get('PATH'); |
||
| 609 | $urlParams = [ |
||
| 610 | 'per_page' => $perPage, |
||
| 611 | 'search' => $search, |
||
| 612 | 'search_type' => $search_type, |
||
| 613 | ]; |
||
| 614 | if (!empty($order)) { |
||
| 615 | $urlParams['order'] = $order; |
||
| 616 | } |
||
| 617 | if (!empty($adminView)) { |
||
| 618 | $urlParams['view'] = 'admin'; |
||
| 619 | } |
||
| 620 | if (!empty($fields)) { |
||
| 621 | $urlParams['fields'] = $fields; |
||
| 622 | } |
||
| 623 | ksort($urlParams); |
||
| 624 | |||
| 625 | // previous page url |
||
| 626 | $prevPage = (1 > $page - 1 ) ? null : $page - 1; |
||
| 627 | $nextPage = (1 + $page> $pagination['count']) ? null : $page + 1; |
||
| 628 | |||
| 629 | $resultsFrom = 1 + ($page * $perPage) - $perPage; |
||
| 630 | $resultsTo = $resultsFrom + $perPage - 1; |
||
| 631 | if ($resultsTo > $rows) { |
||
| 632 | $resultsTo = $rows; |
||
| 633 | } |
||
| 634 | |||
| 635 | // return data |
||
| 636 | $this->data['pagination'] = [ |
||
| 637 | 'url_base' => $this->url($url, $urlParams), |
||
| 638 | 'url_current' => $this->url($url, $urlParams + ['page' => $page]), |
||
| 639 | 'url_first' => $this->url($url, $urlParams + ['page' => 1]), |
||
| 640 | 'url_last' => $this->url($url, $urlParams + ['page' => $pagination['count']]), |
||
| 641 | 'url_next' => (null == $nextPage) ? null : $this->url($url, $urlParams + ['page' => $nextPage]), |
||
| 642 | 'url_previous' => (null == $prevPage) ? null : $this->url($url, $urlParams + ['page' => $prevPage]), |
||
| 643 | 'results' => $rows, |
||
| 644 | 'results_from' => $resultsFrom, |
||
| 645 | 'results_to' => $resultsTo, |
||
| 646 | 'per_page' => $perPage, |
||
| 647 | 'pages' => $pagination['count'], |
||
| 648 | 'page' => $page, |
||
| 649 | 'object' => $m->table(), |
||
| 650 | 'fields' => preg_split("/[,]/", $fields) |
||
| 651 | ]; |
||
| 652 | |||
| 653 | // retrieve results |
||
| 654 | $query = 'SELECT * FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
| 655 | if (empty($users_uuid)) { |
||
| 656 | $query .= join(' OR ', $sqlClauses); |
||
| 657 | } else { |
||
| 658 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
| 659 | } |
||
| 660 | $query .= sprintf(' LIMIT %d,%d', (1 == $page) ? 0 : ($page - 1) * $perPage, $perPage); |
||
| 661 | $results = $db->exec($query); |
||
| 662 | foreach ($results as $row) { |
||
| 663 | $this->data['objects'][] = $adminView ? $m->castFields($fields, $row) : $m->exportArray($fields, $row); |
||
| 664 | } |
||
| 665 | } |
||
| 666 | |||
| 668 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.