Completed
Push — develop ( dfd31d...f6e11d )
by
unknown
17:02
created

PaginationList::getNext()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 10
rs 8.8571
cc 5
eloc 5
nc 3
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** PaginationList.php */
11
namespace Applications\Repository;
12
13
/**
14
 * class for accessing a pagination list.
15
 */
16
class PaginationList
17
{
18
    
19
    /**
20
     * List of ids.
21
     * @var array
22
     */
23
    protected $ids = array();
24
    
25
    /**
26
     * Pointer
27
     * @var int|bool
28
     */
29
    protected $pointer = false;
30
    
31
    /**
32
     * Count of list items
33
     * @var int
34
     */
35
    protected $count = 0;
36
    
37
    /**
38
     * Creates a new PaginationList
39
     * @param array $ids
40
     */
41
    public function __construct(array $ids = array())
42
    {
43
        $this->setList($ids);
44
    }
45
    
46
    /**
47
     * Sets the list entries
48
     *
49
     * @param array $ids
50
     * @return \Applications\Repository\PaginationList
51
     */
52
    public function setList(array $ids)
53
    {
54
        $this->ids = $ids;
55
        $this->count = count($ids);
56
        return $this;
57
    }
58
    
59
    /**
60
     * Set current list entry (move pointer).
61
     *
62
     * @param string $id
63
     * @return int|bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|integer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
64
     */
65
    public function setCurrent($id)
66
    {
67
        $this->pointer = array_search($id, $this->ids);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_search($id, $this->ids) can also be of type string. However, the property $pointer is declared as type integer|boolean. 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...
68
        return $this->pointer;
69
    }
70
    
71
    /**
72
     * Gets current list entry.
73
     *
74
     * @return NULL|array
75
     */
76
    public function getCurrent()
77
    {
78
        if (false === $this->pointer || empty($this->ids)) {
79
            return null;
80
        }
81
        return $this->ids[$this->pointer];
82
    }
83
    
84
    /**
85
     * Gets the current position.
86
     *
87
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
88
     */
89
    public function getPosition()
90
    {
91
        return $this->pointer + 1;
92
    }
93
    
94
    /**
95
     * Gets the total count.
96
     *
97
     * @return int
98
     */
99
    public function getCount()
100
    {
101
        return $this->count;
102
    }
103
    
104
    /**
105
     * gets the id BEFORE the current entry.
106
     *
107
     * @return null|string
108
     */
109
    public function getPrevious()
110
    {
111
        if (false === $this->pointer || !$this->count || 0 == $this->pointer) {
112
            return null;
113
        }
114
        return $this->ids[$this->pointer - 1];
115
    }
116
    
117
    /**
118
     * Gets the id BEHIND the current entry.
119
     *
120
     * @return string
121
     * @return NULL|multitype:
0 ignored issues
show
Documentation introduced by
The doc-type NULL|multitype: could not be parsed: Unknown type name "multitype:" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
122
     */
123
    public function getNext()
124
    {
125
        if (false === $this->pointer || !$this->count || $this->count == $this->pointer) {
126
            return null;
127
        }
128
        
129
        $pointer = $this->pointer + 1;
130
        
131
		return isset($this->ids[$pointer]) ? $this->ids[$pointer] : null;
132
    }
133
}
134