Completed
Pull Request — master (#114)
by Anton
05:57
created

CountingPaginator::getOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Pagination;
9
10
class CountingPaginator implements PredictableInterface, \Countable
11
{
12
    /**
13
     * @var int
14
     */
15
    private $pageNumber = 1;
16
17
    /**
18
     * @var int
19
     */
20
    private $countPages = 1;
21
22
    /**
23
     * @var int
24
     */
25
    private $limit = 25;
26
27
    /**
28
     * @var int
29
     */
30
    private $count = 0;
31
32
    /**
33
     * @param int $limit
34
     */
35
    public function __construct($limit = 25)
36
    {
37
        $this->setLimit($limit);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function setPage($number)
44
    {
45
        $this->pageNumber = abs(intval($number));
0 ignored issues
show
Documentation Bug introduced by
It seems like abs(intval($number)) can also be of type double. However, the property $pageNumber is declared as type integer. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
46
47
        //Real page number
48
        return $this->getPage();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 View Code Duplication
    public function getPage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        if ($this->pageNumber < 1) {
57
            return 1;
58
        }
59
60
        if ($this->pageNumber > $this->countPages) {
61
            return $this->countPages;
62
        }
63
64
        return $this->pageNumber;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function setLimit($limit)
71
    {
72
        $this->limit = $limit;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getLimit()
79
    {
80
        return $this->limit;
81
    }
82
83
    /**
84
     * Get pagination offset.
85
     *
86
     * @return int
87
     */
88
    public function getOffset()
89
    {
90
        return ($this->getPage() - 1) * $this->limit;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function paginate(PaginableInterface $paginable)
97
    {
98
        $this->setCount($paginable->count());
99
100
        $paginable->offset($this->getOffset());
101
        $paginable->limit($this->getLimit());
102
103
        return $paginable;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 View Code Duplication
    public function setCount($count)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $this->count = abs(intval($count));
0 ignored issues
show
Documentation Bug introduced by
It seems like abs(intval($count)) can also be of type double. However, the property $count is declared as type integer. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
112
        if ($this->count > 0) {
113
            $this->countPages = ceil($this->count / $this->limit);
0 ignored issues
show
Documentation Bug introduced by
The property $countPages was declared of type integer, but ceil($this->count / $this->limit) is of type double. 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
        } else {
115
            $this->countPages = 1;
116
        }
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return int
123
     */
124
    public function count()
125
    {
126
        return $this->count;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function countPages()
133
    {
134
        return $this->countPages;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function countDisplayed()
141
    {
142
        if ($this->getPage() == $this->countPages) {
143
            return $this->count - $this->getOffset();
144
        }
145
146
        return $this->limit;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function isRequired()
153
    {
154
        return ($this->countPages > 1);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function nextPage()
161
    {
162
        if ($this->getPage() != $this->countPages) {
163
            return $this->getPage() + 1;
164
        }
165
166
        return false;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function previousPage()
173
    {
174
        if ($this->getPage() > 1) {
175
            return $this->getPage() - 1;
176
        }
177
178
        return false;
179
    }
180
}