Completed
Pull Request — master (#348)
by thomas
70:36
created

StaticCollection::bottom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
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 12
    protected $position = 0;
16
17 12
    /**
18
     * @return array
19
     */
20
    public function all()
21
    {
22
        return $this->set;
23 37
    }
24
25 37
    /**
26
     * @param int $start
27
     * @param int $length
28
     * @return self
29
     */
30
    public function slice($start, $length)
31 127
    {
32
        $end = count($this->set);
33 127
        if ($start > $end || $length > $end) {
34
            throw new \RuntimeException('Invalid start or length');
35
        }
36
37
        $sliced = array_slice($this->set, $start, $length);
38
        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 103
    }
40
41 103
    /**
42 103
     * @return int
43
     */
44
    public function count()
45
    {
46
        return count($this->set);
47 3
    }
48
49 3
    /**
50
     * @return mixed
51
     */
52
    public function bottom()
53
    {
54
        return $this->offsetGet(count($this) - 1);
55 39
    }
56
57 39
    /**
58
     * @return bool
59
     */
60
    public function isNull()
61
    {
62
        return count($this->set) === 0;
63 102
    }
64
65 102
    /**
66 102
     * @return void
67
     */
68
    public function rewind()
69
    {
70
        $this->position = 0;
71 103
    }
72
73 103
    /**
74
     * @return mixed
75
     */
76
    public function current()
77
    {
78
        return $this->set[$this->position];
79
    }
80 30
81
    /**
82 30
     * @return int
83
     */
84
    public function key()
85
    {
86
        return $this->position;
87
    }
88 3
89
    /**
90 3
     * @return void
91
     */
92
    public function next()
93
    {
94
        ++$this->position;
95
    }
96
97 12
    /**
98
     * @return bool
99 12
     */
100 6
    public function valid()
101
    {
102 6
        return isset($this->set[$this->position]);
103
    }
104
105
    /**
106
     * @param int $offset
107
     * @return bool
108
     */
109 6
    public function offsetExists($offset)
110
    {
111 6
        return array_key_exists($offset, $this->set);
112
    }
113
114
    /**
115
     * @param int $offset
116
     */
117
    public function offsetUnset($offset)
118
    {
119
        throw new \RuntimeException('Cannot unset from a Static Collection');
120
    }
121
122
    /**
123
     * @param int $offset
124
     * @return mixed
125
     */
126
    public function offsetGet($offset)
127
    {
128
        if (!array_key_exists($offset, $this->set)) {
129
            throw new \OutOfRangeException('Nothing found at this offset');
130
        }
131
132
        return $this->set[$offset];
133
    }
134
135
    /**
136
     * @param int $offset
137
     * @param mixed $value
138
     */
139
    public function offsetSet($offset, $value)
140
    {
141
        throw new \RuntimeException('Cannot add to a Static Collection');
142
    }
143
}
144