These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the ONGR package. |
||
5 | * |
||
6 | * (c) NFQ Technologies UAB <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace ONGR\FilterManagerBundle\Pager; |
||
13 | |||
14 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||
15 | |||
16 | /** |
||
17 | * Returns all the required data to paginate. |
||
18 | */ |
||
19 | class PagerService |
||
20 | { |
||
21 | /** |
||
22 | * @var int Current page. |
||
23 | */ |
||
24 | private $page; |
||
25 | |||
26 | /** |
||
27 | * @var int Number of items per page. |
||
28 | */ |
||
29 | private $limit; |
||
30 | |||
31 | /** |
||
32 | * @var int Maximum number of pages. |
||
33 | */ |
||
34 | private $maxPages; |
||
35 | |||
36 | /** |
||
37 | * Constructor. |
||
38 | * |
||
39 | * @param PagerAdapterInterface $adapter The pager adapter. |
||
40 | * @param array $options Additional options. |
||
41 | */ |
||
42 | public function __construct(PagerAdapterInterface $adapter, array $options = []) |
||
43 | { |
||
44 | $this->adapter = $adapter; |
||
45 | $resolver = new OptionsResolver(); |
||
46 | $this->setRequiredOptions($resolver); |
||
47 | $options = $resolver->resolve($options); |
||
48 | $this->setLimit($options['limit']); |
||
49 | $this->setPage($options['page']); |
||
50 | $this->setMaxPages($options['max_pages']); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Sets the required options. |
||
55 | * |
||
56 | * @param OptionsResolver $resolver |
||
57 | */ |
||
58 | private function setRequiredOptions(OptionsResolver $resolver) |
||
59 | { |
||
60 | $resolver |
||
61 | ->setRequired(['limit', 'page', 'max_pages']) |
||
62 | ->setDefaults(['max_pages' => 8, 'limit' => 10, 'page' => 1]); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Sets the current page number. |
||
67 | * |
||
68 | * @param int $page The current page number. |
||
69 | */ |
||
70 | public function setPage($page) |
||
71 | { |
||
72 | $this->page = min($page > 0 ? $page : $this->getFirstPage(), $this->getLastPage()); |
||
0 ignored issues
–
show
|
|||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Returns the current page number. |
||
77 | * |
||
78 | * @return int |
||
79 | */ |
||
80 | public function getPage() |
||
81 | { |
||
82 | return $this->page; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Sets the results limit for one page. |
||
87 | * |
||
88 | * @param int $limit |
||
89 | */ |
||
90 | public function setLimit($limit) |
||
91 | { |
||
92 | $this->limit = $limit > 0 ? $limit : 1; |
||
93 | |||
94 | $this->setPage($this->page); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Returns the current results limit for one page. |
||
99 | * |
||
100 | * @return int |
||
101 | */ |
||
102 | public function getLimit() |
||
103 | { |
||
104 | return $this->limit; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Sets the number of pages shown. |
||
109 | * |
||
110 | * @param int $maxPages |
||
111 | */ |
||
112 | public function setMaxPages($maxPages) |
||
113 | { |
||
114 | $this->maxPages = $maxPages; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Returns the number of pages shown. |
||
119 | * |
||
120 | * @return int |
||
121 | */ |
||
122 | public function getMaxPages() |
||
123 | { |
||
124 | return $this->maxPages; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Returns the next page number. |
||
129 | * |
||
130 | * @return int |
||
131 | */ |
||
132 | public function getNextPage() |
||
133 | { |
||
134 | $lastPage = $this->getLastPage(); |
||
135 | |||
136 | return $this->page < $lastPage ? $this->page + 1 : $lastPage; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Returns the previous page number. |
||
141 | * |
||
142 | * @return int |
||
143 | */ |
||
144 | public function getPreviousPage() |
||
145 | { |
||
146 | return $this->page > $this->getFirstPage() ? $this->page - 1 : $this->getFirstPage(); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns true if the current page is first. |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function isFirstPage() |
||
155 | { |
||
156 | return $this->page == 1; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Returns the first page number. |
||
161 | * |
||
162 | * @return int |
||
163 | */ |
||
164 | public function getFirstPage() |
||
165 | { |
||
166 | return 1; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Returns true if the current page is last. |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function isLastPage() |
||
175 | { |
||
176 | return $this->page == $this->getLastPage(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Returns the last page number. |
||
181 | * |
||
182 | * @return int |
||
183 | */ |
||
184 | public function getLastPage() |
||
185 | { |
||
186 | return $this->hasResults() ? ceil($this->adapter->getTotalResults() / $this->limit) : $this->getFirstPage(); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Returns true if the current result set requires pagination. |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function isPaginable() |
||
195 | { |
||
196 | return $this->adapter->getTotalResults() > $this->limit; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Generates a page list. |
||
201 | * |
||
202 | * @return array The page list. |
||
203 | */ |
||
204 | public function getPages() |
||
205 | { |
||
206 | $pages = $this->getMaxPages(); |
||
207 | |||
208 | $tmpBegin = $this->page - floor($pages / 2); |
||
209 | $tmpEnd = $tmpBegin + $pages - 1; |
||
210 | |||
211 | if ($tmpBegin < $this->getFirstPage()) { |
||
212 | $tmpEnd += $this->getFirstPage() - $tmpBegin; |
||
213 | $tmpBegin = $this->getFirstPage(); |
||
214 | } |
||
215 | |||
216 | if ($tmpEnd > $this->getLastPage()) { |
||
217 | $tmpBegin -= $tmpEnd - $this->getLastPage(); |
||
218 | $tmpEnd = $this->getLastPage(); |
||
219 | } |
||
220 | |||
221 | $begin = max($tmpBegin, $this->getFirstPage()); |
||
222 | $end = $tmpEnd; |
||
223 | |||
224 | return range($begin, $end, 1); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Returns true if the current result set is not empty. |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function hasResults() |
||
233 | { |
||
234 | return $this->adapter->getTotalResults() > 0; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Returns results list for the current page and limit. |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | public function getResults() |
||
243 | { |
||
244 | return $this->hasResults() ? $this->adapter->getResults($this->getOffset(), $this->limit) : []; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Returns offset. |
||
249 | * |
||
250 | * @return int |
||
251 | */ |
||
252 | public function getOffset() |
||
253 | { |
||
254 | return ($this->page - 1) * $this->limit; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Returns the current adapter instance. |
||
259 | * |
||
260 | * @return PagerAdapterInterface |
||
261 | */ |
||
262 | public function getAdapter() |
||
263 | { |
||
264 | return $this->adapter; |
||
265 | } |
||
266 | } |
||
267 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.