| Conditions | 10 |
| Paths | 14 |
| Total Lines | 39 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 41 | public function __invoke($paginatorName, $defaultParams = array(), $usePostParams = false) |
||
| 42 | { |
||
| 43 | if (is_bool($defaultParams)) { |
||
| 44 | $usePostParams = $defaultParams; |
||
| 45 | $defaultParams = array(); |
||
| 46 | } |
||
| 47 | |||
| 48 | if (!is_array($defaultParams) && !$defaultParams instanceof \Traversable) { |
||
| 49 | throw new \InvalidArgumentException('$defaultParams must be an array or implement \Traversable'); |
||
| 50 | } |
||
| 51 | |||
| 52 | /* @var $controller \Zend\Mvc\Controller\AbstractController |
||
| 53 | * @var $paginators \Core\Paginator\PaginatorService |
||
| 54 | * @var $request \Zend\Http\Request |
||
| 55 | */ |
||
| 56 | $controller = $this->getController(); |
||
| 57 | $services = $controller->getServiceLocator(); |
||
| 58 | $paginators = $services->get('Core/PaginatorService'); |
||
| 59 | $request = $controller->getRequest(); |
||
| 60 | $params = $usePostParams |
||
| 61 | ? $request->getPost()->toArray() |
||
| 62 | : $request->getQuery()->toArray(); |
||
| 63 | |||
| 64 | // We allow \Traversable so we cannot simply merge. |
||
| 65 | foreach ($defaultParams as $key => $val) { |
||
| 66 | if (!isset($params[$key])) { |
||
| 67 | $params[$key] = $val; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /* @var $paginator \Zend\Paginator\Paginator */ |
||
| 72 | $paginator = $paginators->get($paginatorName, $params); |
||
| 73 | $paginator->setCurrentPageNumber(isset($params['page']) ? $params['page'] : 1) |
||
| 74 | ->setItemCountPerPage(isset($params['count']) ? $params['count'] : 10) |
||
| 75 | ->setPageRange(isset($params['range']) ? $params['range'] : 5); |
||
| 76 | |||
| 77 | return $paginator; |
||
| 78 | |||
| 79 | } |
||
| 80 | } |
||
| 81 |