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