Completed
Pull Request — master (#348)
by thomas
72:50 queued 67:29
created

StaticCollection::offsetSet()   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
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Collection;
4
5
abstract class StaticCollection implements CollectionInterface
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $set;
11
12
    /**
13
     * @var int
14
     */
15
    protected $position = 0;
16
17
    /**
18
     * @return array
19
     */
20 12
    public function all()
21
    {
22 12
        return $this->set;
23
    }
24
25
    /**
26
     * @param int $start
27
     * @param int $length
28
     * @return self
29
     */
30 27
    public function slice($start, $length)
31
    {
32 27
        $end = count($this->set);
33 27
        if ($start > $end || $length > $end) {
34
            throw new \RuntimeException('Invalid start or length');
35
        }
36
37 27
        $sliced = array_slice($this->set, $start, $length);
38 27
        return new static($sliced);
0 ignored issues
show
Unused Code introduced by
The call to StaticCollection::__construct() has too many arguments starting with $sliced.

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...
39
    }
40
41
    /**
42
     * @return int
43
     */
44 56
    public function count()
45
    {
46 56
        return count($this->set);
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function bottom()
53
    {
54
        if (count($this->set) === 0) {
55
            throw new \RuntimeException('No bottom for empty collection');
56
        }
57
        
58
        return $this->offsetGet(count($this) - 1);
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function top()
65
    {
66
        if (count($this->set) === 0) {
67
            throw new \RuntimeException('No top for empty collection');
68
        }
69
70
        return $this->offsetGet(0);
71
    }
72
73
    /**
74
     * @return bool
75
     */
76 23
    public function isNull()
77
    {
78 23
        return count($this->set) === 0;
79
    }
80
81
    /**
82
     * @return void
83
     */
84 53
    public function rewind()
85
    {
86 53
        $this->position = 0;
87 53
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    public function current()
93
    {
94
        return $this->set[$this->position];
95
    }
96
97
    /**
98
     * @return int
99
     */
100
    public function key()
101
    {
102
        return $this->position;
103
    }
104
105
    /**
106
     * @return void
107
     */
108 39
    public function next()
109
    {
110 39
        ++$this->position;
111 39
    }
112
113
    /**
114
     * @return bool
115
     */
116 53
    public function valid()
117
    {
118 53
        return isset($this->set[$this->position]);
119
    }
120
121
    /**
122
     * @param int $offset
123
     * @return bool
124
     */
125
    public function offsetExists($offset)
126
    {
127
        return array_key_exists($offset, $this->set);
128
    }
129
130
    /**
131
     * @param int $offset
132
     */
133
    public function offsetUnset($offset)
134
    {
135
        throw new \RuntimeException('Cannot unset from a Static Collection');
136
    }
137
138
    /**
139
     * @param int $offset
140
     * @return mixed
141
     */
142
    public function offsetGet($offset)
143
    {
144
        if (!array_key_exists($offset, $this->set)) {
145
            throw new \OutOfRangeException('Nothing found at this offset');
146
        }
147
148
        return $this->set[$offset];
149
    }
150
151
    /**
152
     * @param int $offset
153
     * @param mixed $value
154
     */
155
    public function offsetSet($offset, $value)
156
    {
157
        throw new \RuntimeException('Cannot add to a Static Collection');
158
    }
159
}
160