Passed
Push — master ( 105432...a023ec )
by Adam
01:37
created

Pagination   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 212
ccs 46
cts 52
cp 0.8846
rs 8.72
c 0
b 0
f 0
wmc 46

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getPage() 0 10 5
A paging() 0 17 4
A postLinks() 0 7 4
A preLinks() 0 7 4
A setAClass() 0 5 2
A getPaginationClass() 0 2 1
A setAActiveClass() 0 5 2
A setLiClass() 0 5 2
A setPaginationClass() 0 5 2
A getLiActiveClass() 0 2 1
A setLiActiveClass() 0 5 2
A buildQueryString() 0 3 2
A getAActiveClass() 0 2 1
A getLiClass() 0 2 1
A getAClass() 0 2 1
C buildLink() 0 2 12

How to fix   Complexity   

Complex Class

Complex classes like Pagination 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.

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 Pagination, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Pager;
3
4
class Pagination {
5
    public static $current;
6
    protected $queryString;
7
8
    protected static $page;
9
    protected static $pageURL;
10
    protected static $lastpage;
11
    protected static $totalPages;
12
    
13
    public $pagerClass = 'pagination justify-content-center';
14
    public $liClass = 'page-item';
15
    public $liActiveClass = 'active';
16
    public $aClass = 'page-link';
17
    public $aActiveClass = '';
18
    
19
    /**
20 1
     * Sets the class assigned to the UL element of the pagination object
21 1
     * @param string $class This should be the class or classes that you wish to give to the pagination object 
22 1
     * @return $this
23
     */
24 1
    public function setPaginationClass($class) {
25
        if((!empty(trim($class)))) {
26
            $this->pagerClass = $class;
27
        }
28
        return $this;
29
    }
30
    
31 2
    /**
32 2
     * Returns the class to give to the pagination object
33
     * @return string The pagination class will be returned
34
     */
35
    public function getPaginationClass() {
36
        return $this->pagerClass;
37
    }
38
    
39
    /**
40 1
     * Sets the default li class
41 1
     * @param string $class This should be the class that you want to all to all li elements
42 1
     * @return $this
43
     */
44 1
    public function setLiClass($class){
45
        if(is_string(trim($class))) {
0 ignored issues
show
introduced by
The condition is_string(trim($class)) is always true.
Loading history...
46
            $this->liClass = trim($class);
47
        }
48
        return $this;
49
    }
50
    
51 2
    /**
52 2
     * Gets the current li class
53
     * @return string This should be the class to assign on li elements
54
     */
55
    public function getLiClass() {
56
        return $this->liClass;
57
    }
58
    
59
    /**
60
     * Sets the active class to assign on the li elements
61
     * @param string $class This should be the class to assign on active elements
62
     * @return $this
63
     */
64
    public function setLiActiveClass($class) {
65
        if(is_string(trim($class))) {
0 ignored issues
show
introduced by
The condition is_string(trim($class)) is always true.
Loading history...
66 2
            $this->liActiveClass = trim($class);
67 2
        }
68 2
        return $this;
69 2
    }
70 1
71 1
    /**
72 1
     * Returns the class to assign to active li elements
73
     * @return string This should be the class to assign on active elements
74 1
     */
75 1
    public function getLiActiveClass() {
76 1
        return $this->liActiveClass;
77 1
    }
78
    
79 1
    
80
    /**
81 1
     * Sets the default class on a elements
82
     * @param string $class This should be the class to add to a elements
83
     * @return $this
84
     */
85
    public function setAClass($class){
86
        if(is_string(trim($class))) {
0 ignored issues
show
introduced by
The condition is_string(trim($class)) is always true.
Loading history...
87
            $this->aClass = trim($class);
88
        }
89
        return $this;
90
    }
91 1
    
92 1
    /**
93
     * Returns the class assigned to a elements
94
     * @return string Returns the class assigned to a elements
95
     */
96
    public function getAClass() {
97
        return $this->aClass;
98
    }
99
    
100 1
    /**
101 1
     * Sets the class to assign to active a elements
102 1
     * @param string $class This should be the class to add to active a elements
103
     * @return $this
104 1
     */
105
    public function setAActiveClass($class){
106
        if(is_string(trim($class))) {
0 ignored issues
show
introduced by
The condition is_string(trim($class)) is always true.
Loading history...
107
            $this->aActiveClass = trim($class);
108
        }
109
        return $this;
110
    }
111
    
112
    /**
113
     * Returns the class assigned to active a elements 
114 1
     * @return string This should be the class to add to active a elements
115 1
     */
116 1
    public function getAActiveClass() {
117
        return $this->aActiveClass;
118
    }
119
    
120
    /**
121
     * Returns paging buttons for the number of records
122
     * @param int $records The total number of records
123 1
     * @param string $pageURL The URL of the page you are creating the paging for
124 1
     * @param int $start The start number for the results
125
     * @param int $maxshown The number of records that are shown on each page
126
     * @param int $numpagesshown The number of pagination buttons to display
127
     * @param boolean $arrows If you want arrows to display before and after for next and previous set to true (default) else set to false
128
     * @param array $additional Any additional get values to include in the URL
129
     * @return string|false Returns the pagination menu if required else will return false
130
     */
131 1
    public function paging($records, $pageURL, $start = 0, $maxshown = 50, $numpagesshown = 11, $arrows = true, $additional = array()) {
132 1
        self::$pageURL = $pageURL;
133 1
        $this->queryString = $additional;
134
        if ($records > $maxshown) {
135
            self::$current = $start >= 1 ? intval($start) : 1;
136
            self::$totalPages = ceil($records / $maxshown);
137 1
            self::$lastpage = self::$totalPages;
138
            $this->getPage($records, $maxshown, $numpagesshown);
139
            
140
            $paging = '<ul class="'.$this->getPaginationClass().'">'.$this->preLinks($arrows);
141
            while (self::$page <= self::$lastpage) {
142
                $paging .= $this->buildLink(self::$page, self::$page, (self::$current == self::$page));
143
                self::$page = (self::$page + 1);
144
            }
145 1
            return $paging.$this->postLinks($arrows).'</ul>';
146 1
        }
147 1
        return false;
148 1
    }
149 1
    
150
    /**
151 1
     * Build a link item with the given values
152
     * @param string $link This should be any additional items to be included as part of the link
153
     * @param mixed $page This should be the link test on the link normally set as numbers but may be anything like arrows or dots etc
154
     * @param boolean $current If this is the current link item set this as true so the class is added to the link item
155
     * @return string This will return the paging item as a string
156
     */
157
    protected function buildLink($link, $page, $current = false) {
158
        return '<li'.(!empty($this->getLiClass()) || ($current === true && !empty($this->getLiActiveClass())) ? ' class="'.trim($this->getLiClass().(($current === true && !empty($this->getLiActiveClass())) ? ' '.$this->getLiActiveClass() : '').'"') : '').'><a href="'.self::$pageURL.(!empty($this->buildQueryString($link)) ? '?'.$this->buildQueryString($link) : '').'" title="Page '.$page.'"'.(!empty($this->getAClass()) || ($current === true && !empty($this->getAActiveClass())) ? ' class="'.trim($this->getAClass().(($current === true && !empty($this->getAActiveClass())) ? ' '.$this->getAActiveClass() : '')).'"' : '').'>'.$page.'</a></li>';
159
    }
160
    
161
    /**
162
     * Builds the query string to add to the URL
163
     * @param mixed $page If the page variable is set to a number will add the page number to the query string else will not add any additional items
164
     * @return string The complete string will be returned to add to the link item
165
     */
166
    protected function buildQueryString($page) {
167
        $pageInfo = is_numeric($page) ? ['page' => $page] : [];
168
        return http_build_query(array_filter(array_merge($pageInfo, $this->queryString)), '', '&amp;');
169
    }
170
    
171
    /**
172
     * Gets the current page
173
     * @param int $records The total number of records
174
     * @param int $maxshown The number of records that are shown on each page
175
     * @param int $numpages The number of pagination buttons to display
176
     * return void Nothing is returned
177
     */
178
    protected function getPage($records, $maxshown, $numpages) {
179
        $show = floor($numpages / 2);
180
        if (self::$lastpage > $numpages) {
181
            self::$page = (self::$current > $show ? (self::$current - $show) : 1);
182
            if (self::$current < (self::$lastpage - $show)) {
183
                self::$lastpage = ((self::$current <= $show) ? (self::$current + ($numpages - self::$current)) : (self::$current + $show));
184
            }
185
            else { self::$page = self::$current - ($numpages - ((ceil($records / $maxshown) - self::$current)) - 1); }
186
        }
187
        else { self::$page = 1; }
188
    }
189
    
190
    /**
191
     * Returns the previous arrows as long as arrows is set to true and the page is not the first page
192
     * @param boolean $arrows If you want to display previous arrows set to true else set to false
193
     * @return string Any previous link arrows will be returned as a string
194
     */
195
    protected function preLinks($arrows = true) {
196
        $paging = '';
197
        if (self::$current != 1 && $arrows) {
198
            if (self::$current != 2) { $paging .= $this->buildLink('', '&laquo;'); }
199
            $paging .= $this->buildLink((self::$current - 1), '&lt;');
200
        }
201
        return $paging;
202
    }
203
    
204
    /**
205
     * Returns the next arrows as long as arrows is set to true and the page is not the last page
206
     * @param boolean $arrows If you want to display next arrows set to true else set to false
207
     * @return string Any next link arrows will be returned as a string
208
     */
209
    protected function postLinks($arrows = true) {
210
        $paging = '';
211
        if (self::$current != self::$totalPages && $arrows) {
212
            $paging .= $this->buildLink((self::$current + 1), '&gt;');
213
            if (self::$current != (self::$totalPages - 1)) { $paging .= $this->buildLink(self::$totalPages, '&raquo;'); }
214
        }
215
        return $paging;
216
    }
217
}
218