| Conditions | 11 |
| Paths | 49 |
| Total Lines | 61 |
| Code Lines | 38 |
| 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 |
||
| 29 | public function paginate($target, $offset = 1, $limit = 10, array $options = array()) |
||
|
|
|||
| 30 | { |
||
| 31 | $limit = intval(abs($limit)); |
||
| 32 | if (!$limit) { |
||
| 33 | throw new \LogicException('Invalid item per page number, must be a positive number'); |
||
| 34 | } |
||
| 35 | |||
| 36 | $page = intval(round($offset / $limit), 10); |
||
| 37 | if ($page < 1) { |
||
| 38 | $page = 1; |
||
| 39 | } |
||
| 40 | $options = array_merge($this->defaultOptions, $options); |
||
| 41 | |||
| 42 | // normalize default sort field |
||
| 43 | if (isset($options['defaultSortFieldName']) && is_array($options['defaultSortFieldName'])) { |
||
| 44 | $options['defaultSortFieldName'] = implode('+', $options['defaultSortFieldName']); |
||
| 45 | } |
||
| 46 | |||
| 47 | // default sort field and direction are set based on options (if available) |
||
| 48 | if (!isset($_GET[$options['sortFieldParameterName']]) && isset($options['defaultSortFieldName'])) { |
||
| 49 | $_GET[$options['sortFieldParameterName']] = $options['defaultSortFieldName']; |
||
| 50 | |||
| 51 | if (!isset($_GET[$options['sortDirectionParameterName']])) { |
||
| 52 | $_GET[$options['sortDirectionParameterName']] = isset($options['defaultSortDirection']) ? $options['defaultSortDirection'] : 'asc'; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | // before pagination start |
||
| 57 | $beforeEvent = new Event\BeforeEvent($this->eventDispatcher); |
||
| 58 | $this->eventDispatcher->dispatch('knp_pager.before', $beforeEvent); |
||
| 59 | // items |
||
| 60 | $itemsEvent = new Event\ItemsEvent($offset, $limit); |
||
| 61 | $itemsEvent->options = &$options; |
||
| 62 | $itemsEvent->target = &$target; |
||
| 63 | $this->eventDispatcher->dispatch('knp_pager.items', $itemsEvent); |
||
| 64 | if (!$itemsEvent->isPropagationStopped()) { |
||
| 65 | throw new \RuntimeException('One of listeners must count and slice given target'); |
||
| 66 | } |
||
| 67 | // pagination initialization event |
||
| 68 | $paginationEvent = new Event\PaginationEvent(); |
||
| 69 | $paginationEvent->target = &$target; |
||
| 70 | $paginationEvent->options = &$options; |
||
| 71 | $this->eventDispatcher->dispatch('knp_pager.pagination', $paginationEvent); |
||
| 72 | if (!$paginationEvent->isPropagationStopped()) { |
||
| 73 | throw new \RuntimeException('One of listeners must create pagination view'); |
||
| 74 | } |
||
| 75 | // pagination class can be different, with different rendering methods |
||
| 76 | $paginationView = $paginationEvent->getPagination(); |
||
| 77 | $paginationView->setCustomParameters($itemsEvent->getCustomPaginationParameters()); |
||
| 78 | $paginationView->setCurrentPageNumber($page); |
||
| 79 | $paginationView->setItemNumberPerPage($limit); |
||
| 80 | $paginationView->setTotalItemCount($itemsEvent->count); |
||
| 81 | $paginationView->setPaginatorOptions($options); |
||
| 82 | $paginationView->setItems($itemsEvent->items); |
||
| 83 | |||
| 84 | // after |
||
| 85 | $afterEvent = new Event\AfterEvent($paginationView); |
||
| 86 | $this->eventDispatcher->dispatch('knp_pager.after', $afterEvent); |
||
| 87 | |||
| 88 | return $paginationView; |
||
| 89 | } |
||
| 90 | } |
||
| 91 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: