DLpaginate   F
last analyzed

Complexity

Total Complexity 76

Size/Duplication

Total Lines 426
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 76
eloc 162
c 4
b 1
f 0
dl 0
loc 426
ccs 0
cts 178
cp 0
rs 2.32

21 Methods

Rating   Name   Duplication   Size   Complexity  
A prevLabel() 0 5 1
A limit() 0 5 1
A mainTpl() 0 5 1
A renderItemTPL() 0 3 1
A urlFriendly() 0 8 2
A nextIcon() 0 5 1
A adjacents() 0 5 1
A showCounter() 0 5 2
A target() 0 5 1
A items() 0 5 1
A prevIcon() 0 5 1
A getOutput() 0 12 4
A setMode() 0 6 1
A nextLabel() 0 5 1
A getPageQuery() 0 15 5
A currentPage() 0 5 1
A parameterName() 0 5 1
A changeClass() 0 5 1
A show() 0 3 1
A get_pagenum_link() 0 14 5
F calculate() 0 113 43

How to fix   Complexity   

Complex Class

Complex classes like DLpaginate 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 DLpaginate, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Class DLpaginate
5
 */
6
class DLpaginate
7
{
8
    /**
9
     * Script Name: *Digg Style Paginator Class
10
     * Script URI: http://www.mis-algoritmos.com/2007/05/27/digg-style-pagination-class/
11
     * Description: Class in PHP that allows to use a pagination like a digg or sabrosus style.
12
     * Script Version: 0.5
13
     * Author: Victor De la Rocha
14
     * Author: Agel Nash
15
     * Author URI: http://www.mis-algoritmos.com
16
     */
17
18
    /**Default values*/
19
    public $total_pages = -1; //items
20
    public $limit = null;
21
    public $target = "";
22
    public $page = 1;
23
    public $adjacents = 2;
24
    public $showCounter = false;
25
    public $className = "pagination";
26
    public $parameterName = "page";
27
    public $urlF = null; //urlFriendly
28
29
    /**Buttons next and previous*/
30
    public $nextT = ' <a href="[+link+]">Next</a> ';
31
    public $nextI = "&#187;"; //&#9658;
32
    public $prevT = ' <a href="[+link+]">Previous</a> ';
33
    public $prevI = "&#171;"; //&#9668;
34
35
    /**Buttons last and first*/
36
    public $lastT = ' <a href="[+link+]">Last</a> ';
37
    public $lastI = "&#187;&#187;"; //&#9658;
38
    public $firstT = ' <a href="[+link+]">First</a> ';
39
    public $firstI = "&#171;&#171;"; //&#9668;
40
41
    public $numberT = ' <a href="[+link+]">[+num+]</a> ';
42
    public $currentT = ' <b>[+num+]</b> ';
43
44
    public $mainTpl = '<div class="[+classname+]">[+wrap+]</div>';
45
46
    public $dotsT = ' ... ';
47
48
    /*****/
49
    protected $mode = null;
50
    protected $modeConfig = array();
51
52
    private $calculate = false;
53
    private $pagination;
54
55
    /**
56
     * @param $mode
57
     * @param array $config
58
     * @return $this
59
     */
60
    public function setMode($mode, array $config = array())
61
    {
62
        $this->mode = $mode;
63
        $this->modeConfig = $config;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Total items
70
     *
71
     * @param $value
72
     * @return $this
73
     */
74
    public function items($value)
75
    {
76
        $this->total_pages = (int)$value;
77
78
        return $this;
79
    }
80
81
    /**
82
     * how many items to show per page
83
     *
84
     * @param $value
85
     * @return $this
86
     */
87
    public function limit($value)
88
    {
89
        $this->limit = (int)$value;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Page to sent the page value
96
     *
97
     * @param $value
98
     * @return $this
99
     */
100
    public function target($value)
101
    {
102
        $this->target = $value;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Current page
109
     *
110
     * @param $value
111
     * @return $this
112
     */
113
    public function currentPage($value)
114
    {
115
        $this->page = (int)$value;
116
117
        return $this;
118
    }
119
120
    /**
121
     * How many adjacent pages should be shown on each side of the current page?
122
     *
123
     * @param $value
124
     * @return $this
125
     */
126
    public function adjacents($value)
127
    {
128
        $this->adjacents = (int)$value;
129
130
        return $this;
131
    }
132
133
    /**
134
     * show counter?
135
     *
136
     * @param string $value
137
     * @return $this
138
     */
139
    public function showCounter($value = "")
140
    {
141
        $this->showCounter = ($value === true) ? true : false;
0 ignored issues
show
introduced by
The condition $value === true is always false.
Loading history...
142
143
        return $this;
144
    }
145
146
    /**
147
     * to change the class name of the pagination div
148
     *
149
     * @param string $value
150
     * @return $this
151
     */
152
    public function changeClass($value = "")
153
    {
154
        $this->className = $value;
155
156
        return $this;
157
    }
158
159
    /**
160
     * @param $value
161
     * @return $this
162
     */
163
    public function mainTpl($value)
164
    {
165
        $this->mainTpl = $value;
166
167
        return $this;
168
    }
169
170
    /**
171
     * @param $value
172
     * @return $this
173
     */
174
    public function nextLabel($value)
175
    {
176
        $this->nextT = $value;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param $value
183
     * @return $this
184
     */
185
    public function nextIcon($value)
186
    {
187
        $this->nextI = $value;
188
189
        return $this;
190
    }
191
192
    /**
193
     * @param $value
194
     * @return $this
195
     */
196
    public function prevLabel($value)
197
    {
198
        $this->prevT = $value;
199
200
        return $this;
201
    }
202
203
    /**
204
     * @param $value
205
     * @return $this
206
     */
207
    public function prevIcon($value)
208
    {
209
        $this->prevI = $value;
210
211
        return $this;
212
    }
213
214
    /**
215
     * to change the class name of the pagination div
216
     *
217
     * @param string $value
218
     * @return $this
219
     */
220
    public function parameterName($value = "")
221
    {
222
        $this->parameterName = $value;
223
224
        return $this;
225
    }
226
227
    /**
228
     * to change urlFriendly
229
     *
230
     * @param string $value
231
     * @return $this
232
     */
233
    public function urlFriendly($value = "%")
234
    {
235
        if (preg_match('/^\s/', $value)) {
236
            $this->urlF = false;
237
        }
238
        $this->urlF = $value;
239
240
        return $this;
241
    }
242
243
    public function show()
244
    {
245
        echo $this->getOutput();
246
    }
247
248
    /**
249
     * @return string
250
     */
251
    public function getOutput()
252
    {
253
        $out = '';
254
        if (!$this->calculate && $this->calculate() && !empty($this->pagination)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
255
            $out = str_replace(
256
                array("[+class+]", "[+wrap+]"),
257
                array($this->className, $this->pagination),
258
                $this->mainTpl
259
            ) . "\n";
260
        }
261
262
        return $out;
263
    }
264
265
    /**
266
     * @param $page
267
     * @return int|mixed
268
     */
269
    protected function getPageQuery($page)
270
    {
271
        switch ($this->mode) {
272
            case 'offset':
273
                $display = isset($this->modeConfig['display']) ? $this->modeConfig['display'] : 0;
274
                $out = $display * ($page - 1);
275
                break;
276
            case 'back':
277
            case 'pages':
278
            default:
279
                $out = $page;
280
                break;
281
        }
282
283
        return $out;
284
    }
285
286
    /**
287
     * @param $id
288
     * @return mixed|string
289
     */
290
    public function get_pagenum_link($id)
0 ignored issues
show
Coding Style introduced by
Method name "DLpaginate::get_pagenum_link" is not in camel caps format
Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
291
    {
292
        $flag = (strpos($this->target, '?') === false);
293
        $value = $this->getPageQuery($id);
294
        if ($flag && !empty($this->urlF)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
295
            $out = str_replace($this->urlF, $value, $this->target);
296
        } else {
297
            $out = $this->target;
298
            if ($id > 1) {
299
                $out .= ($flag ? "?" : "&") . $this->parameterName . "=" . $value;
300
            }
301
        }
302
303
        return $out;
304
    }
305
306
    /**
307
     * @return bool
308
     */
309
    public function calculate()
0 ignored issues
show
Coding Style introduced by
Function's nesting level (4) exceeds 3; consider refactoring the function
Loading history...
310
    {
311
        $this->pagination = "";
312
        $this->calculate = true;
313
        $error = false;
314
315
        if (!empty($this->urlF) && is_scalar($this->urlF) && $this->urlF != '%' && strpos(
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
316
            $this->target,
317
            $this->urlF
318
        ) === false
319
        ) {
320
            //Es necesario especificar el comodin para sustituir
321
            $error = true;
322
        } elseif (!empty($this->urlF) && is_scalar($this->urlF) && $this->urlF == '%' && strpos(
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
323
            $this->target,
324
            $this->urlF
325
        ) === false
326
        ) {
327
            $error = true;
328
        }
329
330
        if ($this->total_pages < 0) {
331
            $error = true;
332
        }
333
        if (!is_int($this->limit)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
334
            $error = true;
335
        }
336
        if ($error) {
337
            return false;
338
        }
339
340
        $counter = 0;
341
342
        /* Setup page vars for display. */
343
        $prev = ($this->page <= 1) ? 0 : $this->page - 1; //previous page is page - 1
344
        $next = (($this->page == $this->total_pages) ? 0 : ($this->page + 1)); //next page is page + 1
345
        $lastpage = $this->total_pages;
346
        if ($this->limit > 1 && $lastpage > $this->limit) {
347
            $lastpage = $this->limit;
348
        }
349
        $lpm1 = $lastpage - 1; //last page minus 1
350
351
        /*
352
                Now we apply our rules and draw the pagination object.
353
                We're actually saving the code to a variable in case we want to draw it more than once.
354
        */
355
        if ($lastpage > 1) {
356
            if ($this->page) {
357
                if ($this->page > 1) {
358
                    $this->pagination .= $this->firstT ? $this->renderItemTPL($this->firstT, 0) : '';
359
                    $this->pagination .= $this->prevT ? $this->renderItemTPL($this->prevT, $prev) : '';
360
                } else {
361
                    $this->pagination .= $this->firstI ? $this->renderItemTPL($this->firstI, 0) : '';
362
                    $this->pagination .= $this->prevI ? $this->renderItemTPL($this->prevI, $prev) : '';
363
                }
364
            }
365
            //pages
366
            if ($lastpage < 7 + ($this->adjacents * 2)) { //not enough pages to bother breaking it up
367
                for ($counter = 1; $counter <= $lastpage; $counter++) {
368
                    $tpl = ($counter == $this->page) ? $this->currentT : $this->numberT;
369
                    $this->pagination .= $this->renderItemTPL($tpl, $counter);
370
                }
371
            } elseif ($lastpage > 5 + ($this->adjacents * 2)) { //enough pages to hide some
372
                //close to beginning; only hide later pages
373
                if ($this->page <= 2 + ($this->adjacents * 2)) {
374
                    for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++) {
375
                        $tpl = ($counter == $this->page) ? $this->currentT : $this->numberT;
376
                        $this->pagination .= $this->renderItemTPL($tpl, $counter);
377
                    }
378
                    $this->pagination .= $this->renderItemTPL($this->dotsT, $counter);
379
                    $this->pagination .= $this->renderItemTPL($this->numberT, $lpm1);
380
                    $this->pagination .= $this->renderItemTPL($this->numberT, $lastpage);
381
                } //in middle; hide some front and some back
382
                elseif ($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)) {
383
                    $this->pagination .= $this->renderItemTPL($this->numberT, 1);
384
                    $this->pagination .= $this->renderItemTPL($this->numberT, 2);
385
                    $this->pagination .= $this->renderItemTPL($this->dotsT, 3);
386
387
                    for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++) {
388
                        $tpl = ($counter == $this->page) ? $this->currentT : $this->numberT;
389
                        $this->pagination .= $this->renderItemTPL($tpl, $counter);
390
                    }
391
                    $this->pagination .= $this->renderItemTPL($this->dotsT, $counter);
392
                    $this->pagination .= $this->renderItemTPL($this->numberT, $lpm1);
393
                    $this->pagination .= $this->renderItemTPL($this->numberT, $lastpage);
394
                } //close to end; only hide early pages
395
                else {
396
                    $this->pagination .= $this->renderItemTPL($this->numberT, 1);
397
                    $this->pagination .= $this->renderItemTPL($this->numberT, 2);
398
                    $this->pagination .= $this->renderItemTPL($this->dotsT, 3);
399
400
                    for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++) {
401
                        $tpl = ($counter == $this->page) ? $this->currentT : $this->numberT;
402
                        $this->pagination .= $this->renderItemTPL($tpl, $counter);
403
                    }
404
                }
405
            }
406
            if ($this->page) {
407
                if ($this->page < $counter - 1) {
408
                    $this->pagination .= $this->nextT ? $this->renderItemTPL($this->nextT, $next) : '';
409
                    $this->pagination .= $this->lastT ? $this->renderItemTPL($this->lastT, $lastpage) : '';
410
                } else {
411
                    $this->pagination .= $this->nextI ? $this->renderItemTPL($this->nextI, $next) : '';
412
                    $this->pagination .= $this->lastI ? $this->renderItemTPL($this->lastI, $lastpage) : '';
413
                }
414
415
                if ($this->showCounter) {
416
                    $this->pagination .= "<div class=\"pagination_data\">($this->total_pages Pages)</div>";
417
                }
418
            }
419
        }
420
421
        return true;
422
    }
423
424
    /**
425
     * @param $tpl
426
     * @param $num
427
     * @return mixed
428
     */
429
    protected function renderItemTPL($tpl, $num)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
430
    {
431
        return str_replace(array('[+num+]', '[+link+]'), array($num, $this->get_pagenum_link($num)), $tpl);
432
    }
433
434
}
435