| Conditions | 9 |
| Paths | 100 |
| Total Lines | 57 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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); |
||
| 28 | protected function pagination($value, Field $field, HTMLElement $previous): HTMLElement |
||
| 29 | { |
||
| 30 | $pagesaround = intval($field->getExtension(self::PAGES_AROUND, 5)); |
||
| 31 | $perpage = intval($field->getExtension(self::PER_PAGE, 20)); |
||
| 32 | $baseurl = $field->getExtension(self::BASE_URL, '?'); |
||
| 33 | $numitems = $field->getExtension(self::TOTAL_ITEMS, 0); |
||
| 34 | $currentitem = intval($field->getExtension(self::CURRENT, 1)); |
||
| 35 | |||
| 36 | // $firstindex => first id, same as 'begin' |
||
| 37 | $firstindex = $currentitem - $pagesaround * $perpage; |
||
| 38 | if ($firstindex < 0) { |
||
| 39 | $firstindex = 0; |
||
| 40 | } |
||
| 41 | |||
| 42 | $maxindex = $currentitem + $pagesaround * $perpage; |
||
| 43 | if ($maxindex > $numitems) { |
||
| 44 | $maxindex = $numitems; |
||
| 45 | } |
||
| 46 | |||
| 47 | // only one page? don't show anything. |
||
| 48 | if ($maxindex <= $perpage) { |
||
| 49 | return HTMLElement::factory(''); |
||
| 50 | } |
||
| 51 | |||
| 52 | $query = array(); |
||
| 53 | $parsed = parse_url($baseurl); |
||
| 54 | if (isset($parsed['query'])) { |
||
| 55 | mb_parse_str($parsed['query'], $query); |
||
| 56 | } |
||
| 57 | |||
| 58 | if ($firstindex > 0) { |
||
| 59 | $pages[] = $this->getItem('...', '', 'formularium-disabled'); |
||
|
|
|||
| 60 | } |
||
| 61 | |||
| 62 | for ($i = $firstindex; $i < $maxindex; $i += $perpage) { |
||
| 63 | $currentpage = $i / $perpage + 1; |
||
| 64 | if ($i == $currentitem) { |
||
| 65 | $pages[] = $this->getItem((string)$currentpage, '', 'formularium-pagination-current'); |
||
| 66 | } else { |
||
| 67 | $parsed['query'] = array_merge($query, array('begin' => $i, 'total' => $perpage)); |
||
| 68 | $baseurl = $this->glue_url($parsed); |
||
| 69 | $pages[] = $this->getItem((string)$currentpage, $baseurl); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | if ($i < $numitems) { |
||
| 74 | // disabled |
||
| 75 | $pages[] = $this->getItem('...', '', 'formularium-disabled'); |
||
| 76 | } |
||
| 77 | |||
| 78 | return HTMLElement::factory( |
||
| 79 | 'nav', |
||
| 80 | ['class' => 'formularium-pagination-wrapper', 'aria-label' => "Page navigation"], |
||
| 81 | HTMLElement::factory( |
||
| 82 | 'ul', |
||
| 83 | ['class' => 'formularium-pagination'], |
||
| 84 | $pages |
||
| 85 | ) |
||
| 141 |