GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( eca488...1ed15e )
by Asao
02:34
created

Inputs::getPagination()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 8.8571
cc 5
eloc 8
nc 9
nop 2
1
<?php
2
namespace Tuum\Pagination;
3
4
use Tuum\Pagination\Paginate\PaginateInterface;
5
use Tuum\Pagination\Paginate\PaginateMini;
6
use Tuum\Pagination\ToHtml\ToBootstrap;
7
use Tuum\Pagination\ToHtml\ToHtmlInterface;
8
9
class Inputs
10
{
11
    /**
12
     * @var string
13
     */
14
    public $pagerKey = '_page';
15
16
    /**
17
     * @var string
18
     */
19
    public $limitKey = '_limit';
20
21
    /**
22
     * @var int|null
23
     */
24
    private $total = null;
25
26
    /**
27
     * @var array
28
     */
29
    private $list = [];
30
31
    /**
32
     * @var string
33
     */
34
    public $path = '';
35
36
    /**
37
     * @var array
38
     */
39
    public $inputs = [];
40
41
    /**
42
     * @var PaginateInterface
43
     */
44
    private $paginate;
45
46
    /**
47
     * @var ToHtmlInterface
48
     */
49
    private $toHtml;
50
51
    /**
52
     *
53
     */
54
    public function __construct()
55
    {
56
    }
57
58
    /**
59
     * get the limit, i.e. number of data per page.
60
     *
61
     * @return int
62
     */
63
    public function getLimit()
64
    {
65
        return (int)isset($this->inputs[$this->limitKey]) ? $this->inputs[$this->limitKey] : 20;
66
    }
67
68
    /**
69
     * get the offset for retrieving data.
70
     *
71
     * @return int
72
     */
73
    public function getOffset()
74
    {
75
        return $this->getLimit() * ($this->getPage() - 1);
76
    }
77
78
    /**
79
     * get the current page number, starting from 1.
80
     *
81
     * @return int
82
     */
83
    public function getPage()
84
    {
85
        return (int)isset($this->inputs[$this->pagerKey]) ? $this->inputs[$this->pagerKey] : 1;
86
    }
87
88
    /**
89
     * get any key from query.
90
     *
91
     * @param string     $key
92
     * @param null|mixed $alt
93
     * @return null|mixed
94
     */
95
    public function get($key, $alt = null)
96
    {
97
        return array_key_exists($key, $this->inputs)
98
            ? $this->inputs[$key]
99
            : $this->inputs[$key] = $alt;
100
    }
101
102
    /**
103
     * get total number of data.
104
     * - total: number of all the possible data which can be retrieved.
105
     * - count: number of data in the current list.
106
     *
107
     * @return int|null
108
     */
109
    public function getTotal()
110
    {
111
        return $this->total;
112
    }
113
114
    /**
115
     * set the total of data.
116
     *
117
     * @param int|null $total
118
     */
119
    public function setTotal($total)
120
    {
121
        $this->total = $total;
122
    }
123
124
    /**
125
     * set the data for list.
126
     *
127
     * @param mixed $list
128
     */
129
    public function setList($list)
130
    {
131
        $this->list = $list;
0 ignored issues
show
Documentation Bug introduced by
It seems like $list of type * is incompatible with the declared type array of property $list.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
132
    }
133
134
    /**
135
     * get the data for list.
136
     *
137
     * @return null|array|mixed
138
     */
139
    public function getList()
140
    {
141
        return $this->list;
142
    }
143
144
    /**
145
     * get the count, i.e. number of data in the current list.
146
     * count is the number of data in the current list.
147
     *
148
     * @return int
149
     */
150
    public function getCount()
151
    {
152
        if (isset($this->list) && is_array($this->list)) {
153
            return count($this->list);
154
        }
155
        return 0;
156
    }
157
158
    /**
159
     * same as getPage() method.
160
     *
161
     * @return int
162
     */
163
    public function calcSelfPage()
164
    {
165
        return $this->getPage();
166
    }
167
168
    /**
169
     * calculates the first page number, that is 1.
170
     *
171
     * @return int
172
     */
173
    public function calcFirstPage()
174
    {
175
        return 1;
176
    }
177
178
    /**
179
     * calculates the last pager number.
180
     *
181
     * @return int
182
     */
183
    public function calcLastPage()
184
    {
185
        $total = $this->getTotal();
186
        if (!$total) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $total of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
187
            return $this->getPage() + 1;
188
        }
189
        $pages = $this->getLimit();
190
        return (integer)(ceil($total / $pages));
191
    }
192
193
    /**
194
     * calculates the next page number.
195
     *
196
     * @return int
197
     */
198
    public function calcNextPage()
199
    {
200
        return min($this->getPage() + 1, $this->calcLastPage());
201
    }
202
203
    /**
204
     * check if the next page exists (i.e. current page is not the last page).
205
     *
206
     * @return bool
207
     */
208
    public function existsNextPage()
209
    {
210
        return $this->getPage() < $this->calcLastPage();
211
    }
212
213
    /**
214
     * calculates the previous page number.
215
     *
216
     * @return int
217
     */
218
    public function calcPrevPage()
219
    {
220
        return max($this->getPage() - 1, $this->calcFirstPage());
221
    }
222
223
    /**
224
     * @return bool
225
     */
226
    public function existsPrevPage()
227
    {
228
        return $this->getPage() > $this->calcFirstPage();
229
    }
230
231
    /**
232
     * @param int $numLinks
233
     * @return array
234
     */
235
    public function calcPageList($numLinks)
236
    {
237
        $currPage = $this->getPage();
238
        $lastPage = $this->calcLastPage();
239
240
        $extra_1  = max(0, $numLinks - $currPage);
241
        $extra_2  = max(0, $numLinks - ($lastPage - $currPage));
242
        if ($extra_1 > 0 || $currPage === $numLinks) {
243
            $extra_2 += $extra_1 + 1;
244
        }
245
        if ($extra_2 > 0) {
246
            $extra_1 += $extra_2;
247
        }
248
        $start    = max($currPage - $numLinks - $extra_1, $this->calcFirstPage());
249
        $last     = min($currPage + $numLinks + $extra_2, $this->calcLastPage());
250
251
        $pages = [];
252
        for ($p = $start; $p <= $last; $p++) {
253
            $pages[] = $p;
254
        }
255
        return $pages;
256
    }
257
258
    /**
259
     * @param null|int $page
260
     * @return string
261
     */
262
    public function getPath($page = null)
263
    {
264
        if (is_null($page)) {
265
            return $this->path;
266
        }
267
        $page = (int)$page;
268
        return $this->path . '?' . $this->pagerKey . '=' . $page;
269
    }
270
271
    /**
272
     * @param null|PaginateInterface $paginate
273
     * @param null|ToHtmlInterface   $toHtml
274
     * @return ToHtmlInterface
275
     */
276
    public function getPagination($paginate = null, $toHtml = null)
277
    {
278
        if (!$paginate) {
279
            $paginate = $this->paginate ?: new PaginateMini();
280
        }
281
        if (!$toHtml) {
282
            $toHtml   = $this->toHtml ?: new ToBootstrap();
283
        }
284
        $paginate = $paginate->withInputs($this);
285
        $toHtml   = $toHtml->withPaginate($paginate);
286
        
287
        return $toHtml;
288
    }
289
290
    /**
291
     * @param null|PaginateInterface $paginate
292
     * @param null|ToHtmlInterface $toHtml
293
     */
294
    public function setPagination($paginate = null, $toHtml = null)
295
    {
296
        $this->paginate = $paginate;
297
        $this->toHtml   = $toHtml;
298
    }
299
}