| Conditions | 14 |
| Paths | 416 |
| Total Lines | 78 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 declare(strict_types=1); |
||
| 35 | protected function pagination($value, Field $field, HTMLElement $previous): HTMLElement |
||
| 36 | { |
||
| 37 | if (!is_array($value)) { |
||
| 38 | $value = []; |
||
| 39 | } |
||
| 40 | |||
| 41 | $pagesaround = intval( |
||
| 42 | $value[self::PAGES_AROUND] ?? $field->getRenderable(self::PAGES_AROUND, 5) |
||
| 43 | ); |
||
| 44 | $perpage = intval( |
||
| 45 | $value[self::PER_PAGE] ?? $field->getRenderable(self::PER_PAGE, 20) |
||
| 46 | ); |
||
| 47 | $baseurl = $value[self::BASE_URL] ?? $field->getRenderable(self::BASE_URL, '?'); |
||
| 48 | $numitems = $value[self::TOTAL_ITEMS] ?? $field->getRenderable(self::TOTAL_ITEMS, 0); |
||
| 49 | |||
| 50 | // use $currentPage when defined |
||
| 51 | if (array_key_exists(self::CURRENT_PAGE, $value) || $field->getRenderable(self::CURRENT_PAGE, null) !== null) { |
||
| 52 | $currentPage = $value[self::CURRENT_PAGE] ?? intval($field->getRenderable(self::CURRENT_PAGE, 1)); |
||
| 53 | $currentitem = $currentPage * ($perpage - 1); |
||
| 54 | } else { |
||
| 55 | $currentitem = $value[self::CURRENT] ?? intval($field->getRenderable(self::CURRENT, 0)); |
||
| 56 | $currentPage = ceil($currentitem / $perpage); |
||
|
|
|||
| 57 | } |
||
| 58 | |||
| 59 | // $firstindex => first id, same as 'begin' |
||
| 60 | $firstindex = $currentitem - $pagesaround * $perpage; |
||
| 61 | if ($firstindex < 0) { |
||
| 62 | $firstindex = 0; |
||
| 63 | } |
||
| 64 | |||
| 65 | $maxindex = $currentitem + $pagesaround * $perpage; |
||
| 66 | if ($maxindex > $numitems) { |
||
| 67 | $maxindex = $numitems; |
||
| 68 | } |
||
| 69 | |||
| 70 | // only one page? don't show anything. |
||
| 71 | if ($maxindex <= $perpage) { |
||
| 72 | return HTMLElement::factory(''); |
||
| 73 | } |
||
| 74 | |||
| 75 | $query = array(); |
||
| 76 | $parsed = parse_url($baseurl); |
||
| 77 | if ($parsed === false) { |
||
| 78 | throw new Exception('Invalid url' . $baseurl); |
||
| 79 | } |
||
| 80 | if (isset($parsed['query'])) { |
||
| 81 | mb_parse_str($parsed['query'], $query); |
||
| 82 | } |
||
| 83 | |||
| 84 | $pages = []; |
||
| 85 | |||
| 86 | if ($firstindex > 0) { |
||
| 87 | $pages[] = $this->getItem('...', '', 'formularium-ellipsis'); |
||
| 88 | } |
||
| 89 | |||
| 90 | for ($i = $firstindex; $i < $maxindex; $i += $perpage) { |
||
| 91 | $page = $i / $perpage + 1; |
||
| 92 | if ($i < $currentitem && $i + $perpage >= $currentitem) { |
||
| 93 | $pages[] = $this->getItem((string)$page, '', 'formularium-pagination-current'); |
||
| 94 | } else { |
||
| 95 | $parsed['query'] = array_merge($query, array('begin' => $i, 'total' => $perpage)); |
||
| 96 | $baseurl = $this->glue_url($parsed); |
||
| 97 | $pages[] = $this->getItem((string)$page, $baseurl); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($i < $numitems) { |
||
| 102 | // disabled |
||
| 103 | $pages[] = $this->getItem('...', '', 'formularium-ellipsis'); |
||
| 104 | } |
||
| 105 | |||
| 106 | return HTMLElement::factory( |
||
| 107 | 'nav', |
||
| 108 | ['class' => 'formularium-pagination-wrapper', 'aria-label' => "Page navigation"], |
||
| 109 | HTMLElement::factory( |
||
| 110 | 'ul', |
||
| 111 | ['class' => 'formularium-pagination'], |
||
| 112 | $pages |
||
| 113 | ) |
||
| 169 |