Completed
Push — dev-master ( b7e24f...52af7d )
by Derek Stephen
03:02
created

Paginator   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 227
Duplicated Lines 9.25 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 2
dl 21
loc 227
ccs 0
cts 144
cp 0
rs 9.2
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 4 1
A setPageCount() 0 4 1
A setPageCountByTotalRecords() 0 4 2
A setUrl() 0 4 1
A setUrlPart() 0 4 1
A setCurrentPage() 0 4 1
A setCustomPrev() 0 4 1
A setCustomNext() 0 4 1
A setPagerSize() 0 7 2
A getPagerSize() 0 7 2
A ensurePageCount() 0 6 2
A ensureUrl() 0 6 2
A ensureCurrentPage() 0 6 2
A calculateStart() 0 16 4
F render() 21 73 17

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Paginator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Paginator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Bone\View\Helper;
4
5
use Bone\View\Helper\Exception\PaginatorException;
6
use Del\Icon;
7
8
class Paginator
9
{
10
    private $currentPage = 1;
11
    private $customNext;
12
    private $customPrev;
13
    private $pageCount;
14
    private $pagerSize = 5;
15
    private $url;
16
    private $urlPart = ':page';
17
18
    /**
19
     * @param int $pageNum
20
     * @return string
21
     */
22
    private function url(int $pageNum): string
23
    {
24
        return str_replace($this->urlPart, $pageNum, $this->url);
25
    }
26
27
    /**
28
     * @param $pageCount
29
     */
30
    public function setPageCount(int $pageCount): void
31
    {
32
        $this->pageCount = $pageCount;
33
    }
34
35
    /**
36
     * @param $pageCount
37
     */
38
    public function setPageCountByTotalRecords(int $rowCount, int $numPerPage): void
39
    {
40
        $this->pageCount = (int) ceil($rowCount / $numPerPage) ?: 1;
41
    }
42
43
    /**
44
     * @param $url
45
     */
46
    public function setUrl(string $url): void
47
    {
48
        $this->url = $url;
49
    }
50
51
    /**
52
     * @param $replace
53
     */
54
    public function setUrlPart(string $replace): void
55
    {
56
        $this->urlPart = $replace;
57
    }
58
59
    /**
60
     * @param int $page_no
61
     */
62
    public function setCurrentPage(int $page_no): void
63
    {
64
        $this->currentPage = $page_no;
65
    }
66
67
68
    /**
69
     * @param $url
70
     */
71
    public function setCustomPrev($url): void
72
    {
73
        $this->customPrev = '<a href="' . $url . '/"><i class="icon-backward"></i></a>';
74
    }
75
76
77
    /**
78
     * @param $url
79
     */
80
    public function setCustomNext(string $url): void
81
    {
82
        $this->customNext = '<a href="' . $url . '/"><i class="icon-forward"></i></a>';
83
    }
84
85
    /**
86
     * @param int $numBoxes an ODD number!
87
     */
88
    public function setPagerSize(int $numBoxes): void
89
    {
90
        if ($numBoxes % 2 === 0) {
91
            $numBoxes--;
92
        }
93
        $this->pagerSize = $numBoxes;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getPagerSize(): int
100
    {
101
        if (!$this->pagerSize) {
102
            $this->pagerSize = 5;
103
        }
104
        return $this->pagerSize;
105
    }
106
107
    /**
108
     * @throws PaginatorException
109
     */
110
    private function ensurePageCount()
111
    {
112
        if (!$this->pageCount) {
113
            throw new PaginatorException(PaginatorException::NO_PAGE_COUNT);
114
        }
115
    }
116
117
    /**
118
     * @throws PaginatorException
119
     */
120
    private function ensureUrl()
121
    {
122
        if (!$this->url) {
123
            throw new PaginatorException(PaginatorException::NO_URL);
124
        }
125
    }
126
127
    /**
128
     * @throws PaginatorException
129
     */
130
    private function ensureCurrentPage()
131
    {
132
        if (!$this->url) {
133
            throw new PaginatorException(PaginatorException::NO_CURRENT_PAGE);
134
        }
135
    }
136
137
    /**
138
     * @return int
139
     */
140
    private function calculateStart(int $pages): int
141
    {
142
        $half = ($pages - 1) / 2;
143
        if ($this->currentPage < 3) {
144
            $start = 1;
145
        } elseif ($this->currentPage >= ($this->pageCount - $half)) {
146
            $start = $this->pageCount - ($this->getPagerSize() - 1);
147
        } else {
148
            $start = $this->currentPage - $half;
149
            if ($start < 1) {
150
                $start = 1;
151
            }
152
        }
153
154
        return $start;
155
    }
156
157
    /**
158
     * @return string
159
     * @throws PaginatorException
160
     */
161
    public function render(): string
162
    {
163
        $this->ensurePageCount();
164
        $this->ensureUrl();
165
        $this->ensureCurrentPage();
166
167
        $html = '<nav><ul class="pagination">';
168
169
        if ($this->pageCount > ($this->getPagerSize() - 1)) {
170
            $pages = $this->getPagerSize();
171
            $start = $this->calculateStart($pages);
172
        } else {
173
            $pages = $this->pageCount;
174
            $start = 1;
175
        }
176
177
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
178
        if (isset($this->customPrev)) {
179
            $html .= $this->customPrev;
180
        } elseif ($this->currentPage === 1) {
181
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::FAST_BACKWARD, 'disabled') . '</a>';
182
        } else {
183
            $html .= '<a class="page-link"  href ="' . $this->url(1) . '">' . Icon::FAST_BACKWARD . '</a>';
184
        }
185
        $html .= '</li>';
186
187
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
188 View Code Duplication
        if (isset($this->customPrev)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
            $html .= $this->customPrev;
190
        } elseif ($this->currentPage === 1) {
191
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::BACKWARD, 'disabled') . '</a>';
192
        } else {
193
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage - 1) . '">' . Icon::BACKWARD . '</a>';
194
        }
195
        $html .= '</li>';
196
197
        for ($x = $start; $x <= ($start + ($pages - 1)); $x++) {
198
            $html .= '<li class="page-item ';
199
            if ($this->currentPage === $x) {
200
                $html .= ' active" aria-current="page';
201
            }
202
            $html .= '">';
203
            if ($this->currentPage === $x) {
204
                $html .= '<a class="page-link" href="#">' . $x . '</a>';
205
            } else {
206
                $html .= '<a class="page-link" href="' . $this->url($x) .'">' . $x . '</a>';
207
            }
208
            $html .= '</li>';
209
        }
210
211
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' :'<li class="page-item">';
212 View Code Duplication
        if (isset($this->customNext)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
            $html .= $this->customNext;
214
        } elseif ($this->currentPage >= $this->pageCount) {
215
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FORWARD, 'disabled') . '</a>';
216
        } else {
217
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage + 1) . '">' . Icon::FORWARD . '</i></a>';
218
        }
219
        $html .= '</li>';
220
221
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' : '<li class="page-item">';
222 View Code Duplication
        if (isset($this->customNext)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
            $html .= $this->customNext;
224
        } elseif ($this->currentPage >= $this->pageCount) {
225
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FAST_FORWARD, 'disabled') . '</a>';
226
        } else {
227
            $html .= '<a class="page-link"  href ="' . $this->url($this->pageCount) . '">' . Icon::FAST_FORWARD . '</i></a>';
228
        }
229
        $html .= '</li>';
230
        $html .= '</ul></nav>';
231
232
        return $html;
233
    }
234
}
235