Conditions | 7 |
Paths | 6 |
Total Lines | 67 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
37 | public function display(array $property = null): ?string |
||
38 | { |
||
39 | // total items is less to pagination requirement |
||
40 | if (($this->page * $this->step) + 1 > $this->total) { |
||
41 | return null; |
||
42 | } |
||
43 | |||
44 | $lastPage = ceil($this->total / $this->step); // 6/5 ~ 2 = 0..2 |
||
45 | |||
46 | if ($lastPage <= 1) { |
||
47 | return null; |
||
48 | } |
||
49 | |||
50 | // prevent hack-boy's any try |
||
51 | if ($this->page > $lastPage) { |
||
52 | return null; |
||
53 | } |
||
54 | |||
55 | $items = []; |
||
56 | // more then 10 items in pagination |
||
57 | if ($lastPage > 10) { |
||
58 | if ($this->page < 4 || $lastPage - $this->page <= 4) { // list start, page in [0..4] |
||
59 | // from start to 4 |
||
60 | $items = $this->generateItems(0, 4); |
||
61 | |||
62 | // add "..." button |
||
63 | $items[] = [ |
||
64 | 'type' => 'link', 'link' => '#', 'text' => '...', 'property' => ['class' => 'disabled'] |
||
65 | ]; |
||
66 | // add middle page |
||
67 | $middlePage = ceil($lastPage / 2); |
||
68 | $items[] = [ |
||
69 | 'type' => 'link', 'link' => $this->setUrlPage($middlePage), 'text' => $middlePage + 1 |
||
70 | ]; |
||
71 | // add "..." button |
||
72 | $items[] = [ |
||
73 | 'type' => 'link', 'link' => '#', 'text' => '...', 'property' => ['class' => 'disabled'] |
||
74 | ]; |
||
75 | $items = Arr::merge($items, $this->generateItems($lastPage - 4, $lastPage)); |
||
76 | } else { // meanwhile on middle |
||
77 | // generate 1-2 pages |
||
78 | $items = $this->generateItems(0, 2); |
||
79 | |||
80 | // add "..." button |
||
81 | $items[] = [ |
||
82 | 'type' => 'link', 'link' => '#', 'text' => '...', 'property' => ['class' => 'disabled'] |
||
83 | ]; |
||
84 | |||
85 | // add middle variance -3..mid..+3 |
||
86 | $items = Arr::merge($items, $this->generateItems($this->page - 3, $this->page + 3)); |
||
87 | |||
88 | // add "..." button |
||
89 | $items[] = [ |
||
90 | 'type' => 'link', 'link' => '#', 'text' => '...', 'property' => ['class' => 'disabled'] |
||
91 | ]; |
||
92 | |||
93 | // add latest 2 items |
||
94 | $items = Arr::merge($items, $this->generateItems($lastPage - 2, $lastPage)); |
||
95 | } |
||
96 | } else { // less then 10 items in pagination |
||
97 | $items = $this->generateItems(0, $lastPage); |
||
98 | } |
||
99 | |||
100 | return Listing::display([ |
||
101 | 'type' => 'ul', |
||
102 | 'property' => $property, |
||
103 | 'items' => $items |
||
104 | ]); |
||
156 |