1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package |
4
|
|
|
* |
5
|
|
|
* @package AnimeDb |
6
|
|
|
* @author Peter Gribanov <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace AnimeDb\Bundle\PaginationBundle\Service; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* NavigateRange |
15
|
|
|
* @package AnimeDb\Bundle\PaginationBundle\Service |
16
|
|
|
*/ |
17
|
|
|
class NavigateRange |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Configuration |
21
|
|
|
*/ |
22
|
|
|
protected $config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $left_offset = -1; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $right_offset = -1; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Configuration $config |
36
|
|
|
*/ |
37
|
11 |
|
public function __construct(Configuration $config) { |
38
|
11 |
|
$this->config = $config; |
39
|
11 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
10 |
|
public function getLeftOffset() |
45
|
|
|
{ |
46
|
10 |
|
return $this->buildOffset()->left_offset; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
10 |
|
public function getRightOffset() |
53
|
|
|
{ |
54
|
10 |
|
return $this->buildOffset()->right_offset; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return NavigateRange |
59
|
|
|
*/ |
60
|
10 |
|
protected function buildOffset() |
61
|
|
|
{ |
62
|
10 |
|
if (($this->left_offset < 0 || $this->right_offset < 0) && $this->config->getTotalPages() > 1) { |
63
|
|
|
// definition of offset to the left and to the right of the selected page |
64
|
9 |
|
$this->left_offset = (int)floor(($this->config->getMaxNavigate() - 1) / 2); |
65
|
9 |
|
$this->right_offset = (int)ceil(($this->config->getMaxNavigate() - 1) / 2); |
66
|
|
|
|
67
|
|
|
// adjustment, if the offset is too large left |
68
|
9 |
|
if ($this->config->getCurrentPage() - $this->left_offset < 1) { |
69
|
4 |
|
$offset = abs($this->config->getCurrentPage() - 1 - $this->left_offset); |
70
|
4 |
|
$this->left_offset = $this->left_offset - $offset; |
|
|
|
|
71
|
4 |
|
$this->right_offset = $this->right_offset + $offset; |
|
|
|
|
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
// adjustment, if the offset is too large right |
75
|
9 |
|
if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) { |
76
|
4 |
|
$offset = abs($this->config->getTotalPages() - $this->config->getCurrentPage() - $this->right_offset); |
77
|
4 |
|
$this->left_offset = $this->left_offset + $offset; |
|
|
|
|
78
|
4 |
|
$this->right_offset = $this->right_offset - $offset; |
|
|
|
|
79
|
4 |
|
} |
80
|
|
|
|
81
|
|
|
// left offset should point not lower of the first page |
82
|
9 |
|
if ($this->left_offset >= $this->config->getCurrentPage()) { |
83
|
2 |
|
$this->left_offset = $this->config->getCurrentPage() - 1; |
84
|
2 |
|
} |
85
|
9 |
|
} |
86
|
|
|
|
87
|
10 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.