StaticCollection   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 52.38%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 153
ccs 22
cts 42
cp 0.5238
rs 10
c 0
b 0
f 0
wmc 20

15 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 3 1
A isNull() 0 3 1
A bottom() 0 7 2
A all() 0 3 1
A offsetGet() 0 7 2
A current() 0 3 1
A offsetExists() 0 3 1
A slice() 0 9 3
A count() 0 3 1
A key() 0 3 1
A rewind() 0 3 1
A valid() 0 3 1
A top() 0 7 2
A offsetUnset() 0 3 1
A next() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Collection;
6
7
use BitWasp\Buffertools\BufferInterface;
8
9
/**
10
 * @deprecated v2.0.0
11
 */
12
abstract class StaticCollection implements CollectionInterface
0 ignored issues
show
Deprecated Code introduced by
The interface BitWasp\Bitcoin\Collection\CollectionInterface has been deprecated: v2.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

12
abstract class StaticCollection implements /** @scrutinizer ignore-deprecated */ CollectionInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $set;
18
19
    /**
20
     * @var int
21
     */
22
    protected $position = 0;
23
24
    /**
25
     * @return array
26
     */
27 49
    public function all(): array
28
    {
29 49
        return $this->set;
30
    }
31
32
    /**
33
     * @param int $start
34
     * @param int $length
35
     * @return self
36
     */
37 228
    public function slice(int $start, int $length)
38
    {
39 228
        $end = count($this->set);
40 228
        if ($start > $end || $length > $end) {
41
            throw new \RuntimeException('Invalid start or length');
42
        }
43
44 228
        $sliced = array_slice($this->set, $start, $length);
45 228
        return new static(...$sliced);
0 ignored issues
show
Unused Code introduced by
The call to BitWasp\Bitcoin\Collecti...llection::__construct() has too many arguments starting with $sliced. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        return /** @scrutinizer ignore-call */ new static(...$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. Please note the @ignore annotation hint above.

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