Completed
Push — master ( ba6dcb...69e1d9 )
by Gabriel
02:13
created

AbstractPaginator::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Nip\Records\Navigator\Pagination;
4
5
use Nip\Database\Query\Select as SelectQuery;
6
7
/**
8
 * Class AbstractPaginator
9
 * @package Nip\Records\Navigator\Pagination
10
 */
11
abstract class AbstractPaginator
12
{
13
14
    /**
15
     * @var SelectQuery
16
     */
17
    protected $query = null;
18
19
    /**
20
     * @var int
21
     */
22
    protected $page = 1;
23
24
    /**
25
     * @var int
26
     */
27
    protected $itemsPerPage = 20;
28
29
    /**
30
     * @var null|int
31
     */
32
    protected $count = null;
33
34
    /**
35
     * @var int
36
     */
37
    protected $pages;
38
39
    /**
40
     * @param SelectQuery $query
41
     * @return SelectQuery
42
     */
43
    public function paginate($query)
44
    {
45
        $query->limit($this->getLimitStart(), $this->getItemsPerPage());
0 ignored issues
show
Documentation introduced by
$this->getItemsPerPage() is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
47
        $this->setQuery($query);
48
49
        return $query;
50
    }
51
52
    /**
53
     * @return SelectQuery
54
     */
55
    public function getQuery()
56
    {
57
        return $this->query;
58
    }
59
60
    /**
61
     * @param SelectQuery $query
62
     */
63
    public function setQuery($query)
64
    {
65
        $this->query = $query;
66
    }
67
68
    /**
69
     * @deprecated Method is called automatically on getCount
70
     */
71
    public function count()
72
    {
73
        $this->getCount();
74
    }
75
76
    /**
77
     *
78
     */
79
    protected function doCount()
80
    {
81
        $query = $this->getCountQuery();
82
        $result = $query->execute()->fetchResult();
83
84
        $this->count = intval(reset($result['count']));
85
        $this->pages = intval($this->count / $this->itemsPerPage);
86
87
        if ($this->count % $this->itemsPerPage != 0) {
88
            $this->pages++;
89
        }
90
91
        if ($this->pages == 0) {
92
            $this->pages = 1;
93
        }
94
    }
95
96
    /**
97
     * @return SelectQuery
98
     */
99
    protected function getCountQuery()
100
    {
101
        $query = clone $this->getQuery();
102
        $query->count('*', 'count');
0 ignored issues
show
Unused Code introduced by
The call to Select::count() has too many arguments starting with '*'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103
        return $query;
104
    }
105
106
    /**
107
     * @param bool $page
108
     * @return $this
109
     */
110
    public function setPage($page = false)
111
    {
112
        if ($page) {
113
            $this->page = $page;
0 ignored issues
show
Documentation Bug introduced by
The property $page was declared of type integer, but $page is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
114
        }
115
        return $this;
116
    }
117
118
    /**
119
     * @return int
120
     */
121
    public function getPage()
122
    {
123
        return $this->page;
124
    }
125
126
    /**
127
     * @return mixed
128
     */
129
    public function getPages()
130
    {
131
        return $this->pages;
132
    }
133
134
    /**
135
     * @param $items
136
     * @return $this
137
     */
138
    public function setItemsPerPage($items)
139
    {
140
        if ($items > 0) {
141
            $this->itemsPerPage = $items;
142
        }
143
        return $this;
144
    }
145
146
    /**
147
     * @return int
148
     */
149
    public function getItemsPerPage()
150
    {
151
        return $this->itemsPerPage;
152
    }
153
154
    /**
155
     * @return float|int
156
     */
157
    public function getLimitStart()
158
    {
159
        return ($this->getPage() - 1) * $this->getItemsPerPage();
160
    }
161
162
    /**
163
     * @return int
164
     */
165
    public function getCount()
166
    {
167
        if ($this->count === null) {
168
            $this->doCount();
169
        }
170
        return $this->count;
171
    }
172
}
173