| Conditions | 12 |
| Paths | 208 |
| Total Lines | 70 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 declare(strict_types=1); |
||
| 19 | public function render(array $parameters, HTMLNode $previous): HTMLNode |
||
| 20 | { |
||
| 21 | $pagesaround = intval($parameters[self::PAGES_AROUND]) ?? 5; |
||
| 22 | $perpage = intval($parameters[self::PER_PAGE]) ?? 20; |
||
| 23 | $baseurl = $parameters[self::BASE_URL] ?? '?'; |
||
| 24 | $numitems = $parameters[self::TOTAL_ITEMS] ?? 0; |
||
| 25 | |||
| 26 | // use $currentPage when defined |
||
| 27 | if (array_key_exists(self::CURRENT_PAGE, $parameters)) { |
||
| 28 | $currentPage = intval($parameters[self::CURRENT_PAGE]); |
||
| 29 | $currentitem = $currentPage * ($perpage - 1); |
||
| 30 | } else { |
||
| 31 | $currentitem = intval($parameters[self::CURRENT]) ?? 0; |
||
| 32 | $currentPage = ceil($currentitem / $perpage); |
||
|
|
|||
| 33 | } |
||
| 34 | |||
| 35 | // $firstindex => first id, same as 'begin' |
||
| 36 | $firstindex = $currentitem - $pagesaround * $perpage; |
||
| 37 | if ($firstindex < 0) { |
||
| 38 | $firstindex = 0; |
||
| 39 | } |
||
| 40 | |||
| 41 | $maxindex = $currentitem + $pagesaround * $perpage; |
||
| 42 | if ($maxindex > $numitems) { |
||
| 43 | $maxindex = $numitems; |
||
| 44 | } |
||
| 45 | |||
| 46 | // only one page? don't show anything. |
||
| 47 | if ($maxindex <= $perpage) { |
||
| 48 | return HTMLNode::factory(''); |
||
| 49 | } |
||
| 50 | |||
| 51 | $query = array(); |
||
| 52 | $parsed = parse_url($baseurl); |
||
| 53 | if ($parsed === false) { |
||
| 54 | throw new Exception('Invalid url' . $baseurl); |
||
| 55 | } |
||
| 56 | if (isset($parsed['query'])) { |
||
| 57 | mb_parse_str($parsed['query'], $query); |
||
| 58 | } |
||
| 59 | |||
| 60 | $pages = []; |
||
| 61 | |||
| 62 | if ($firstindex > 0) { |
||
| 63 | $pages[] = $this->getItem('...', '', 'formularium-ellipsis'); |
||
| 64 | } |
||
| 65 | |||
| 66 | for ($i = $firstindex; $i < $maxindex; $i += $perpage) { |
||
| 67 | $page = $i / $perpage + 1; |
||
| 68 | if ($i < $currentitem && $i + $perpage >= $currentitem) { |
||
| 69 | $pages[] = $this->getItem((string)$page, '', 'formularium-pagination-current'); |
||
| 70 | } else { |
||
| 71 | $parsed['query'] = array_merge($query, array('begin' => $i, 'total' => $perpage)); |
||
| 72 | $baseurl = $this->glue_url($parsed); |
||
| 73 | $pages[] = $this->getItem((string)$page, $baseurl); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($i < $numitems) { |
||
| 78 | // disabled |
||
| 79 | $pages[] = $this->getItem('...', '', 'formularium-ellipsis'); |
||
| 80 | } |
||
| 81 | |||
| 82 | return HTMLNode::factory( |
||
| 83 | 'nav', |
||
| 84 | ['class' => 'formularium-pagination-wrapper', 'aria-label' => "Page navigation"], |
||
| 85 | HTMLNode::factory( |
||
| 86 | 'ul', |
||
| 87 | ['class' => 'formularium-pagination'], |
||
| 88 | $pages |
||
| 89 | ) |
||
| 146 |