Completed
Pull Request — master (#825)
by thomas
29:27 queued 24:26
created

ScriptWitness::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Script;
6
7
use BitWasp\Bitcoin\Collection\StaticBufferCollection;
0 ignored issues
show
Bug introduced by
The type BitWasp\Bitcoin\Collection\StaticBufferCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use BitWasp\Bitcoin\Serializer\Script\ScriptWitnessSerializer;
9
use BitWasp\Buffertools\BufferInterface;
10
11
class ScriptWitness implements ScriptWitnessInterface
12
{
13
    /**
14
     * @var BufferInterface[]
15
     */
16
    protected $set = [];
17
18
    /**
19
     * @var int
20
     */
21
    protected $position = 0;
22
23
    /**
24
     * StaticBufferCollection constructor.
25
     * @param BufferInterface ...$values
26
     */
27 362
    public function __construct(BufferInterface... $values)
28
    {
29 362
        $this->set = $values;
30 362
    }
31
32
    /**
33
     * @param ScriptWitnessInterface $witness
34
     * @return bool
35
     */
36 50
    public function equals(ScriptWitnessInterface $witness): bool
37
    {
38 50
        $nStack = count($this);
39 50
        if ($nStack !== count($witness)) {
40
            return false;
41
        }
42
43 50
        for ($i = 0; $i < $nStack; $i++) {
44 22
            if (false === $this->offsetGet($i)->equals($witness->offsetGet($i))) {
45
                return false;
46
            }
47
        }
48
49 50
        return true;
50
    }
51
52
    /**
53
     * @return BufferInterface
54
     */
55
    public function getBuffer(): BufferInterface
56
    {
57
        return (new ScriptWitnessSerializer())->serialize($this);
58
    }
59
60
    /**
61
     * @return BufferInterface[]
62
     */
63 49
    public function all(): array
64
    {
65 49
        return $this->set;
66
    }
67
68
    /**
69
     * @param int $start
70
     * @param int $length
71
     * @return ScriptWitnessInterface
72
     */
73 228
    public function slice(int $start, int $length): ScriptWitnessInterface
74
    {
75 228
        $end = count($this->set);
76 228
        if ($start > $end || $length > $end) {
77
            throw new \RuntimeException('Invalid start or length');
78
        }
79
80 228
        $sliced = array_slice($this->set, $start, $length);
81 228
        return new static(...$sliced);
82
    }
83
84
    /**
85
     * @return int
86
     */
87 453
    public function count(): int
88
    {
89 453
        return count($this->set);
90
    }
91
92
    /**
93
     * @return BufferInterface
94
     */
95 24
    public function bottom(): BufferInterface
96
    {
97 24
        if (count($this->set) === 0) {
98
            throw new \RuntimeException('No bottom for empty collection');
99
        }
100
101 24
        return $this->offsetGet(count($this) - 1);
102
    }
103
104
    /**
105
     * @return BufferInterface
106
     */
107
    public function top(): BufferInterface
108
    {
109
        if (count($this->set) === 0) {
110
            throw new \RuntimeException('No top for empty collection');
111
        }
112
113
        return $this->offsetGet(0);
114
    }
115
116
    /**
117
     * @return bool
118
     */
119 44
    public function isNull(): bool
120
    {
121 44
        return count($this->set) === 0;
122
    }
123
124
    /**
125
     * @return void
126
     */
127 374
    public function rewind()
128
    {
129 374
        $this->position = 0;
130 374
    }
131
132
    /**
133
     * @return BufferInterface
134
     */
135 339
    public function current(): BufferInterface
136
    {
137 339
        return $this->set[$this->position];
138
    }
139
140
    /**
141
     * @return int
142
     */
143
    public function key(): int
144
    {
145
        return $this->position;
146
    }
147
148
    /**
149
     * @return void
150
     */
151 338
    public function next()
152
    {
153 338
        ++$this->position;
154 338
    }
155
156
    /**
157
     * @return bool
158
     */
159 374
    public function valid(): bool
160
    {
161 374
        return isset($this->set[$this->position]);
162
    }
163
164
    /**
165
     * @param int $offset
166
     * @return bool
167
     */
168
    public function offsetExists($offset)
169
    {
170
        return array_key_exists($offset, $this->set);
171
    }
172
173
    /**
174
     * @param int $offset
175
     */
176
    public function offsetUnset($offset)
177
    {
178
        throw new \RuntimeException('ScriptWitness is immutable');
179
    }
180
181
    /**
182
     * @param int $offset
183
     * @return BufferInterface
184
     */
185 264
    public function offsetGet($offset)
186
    {
187 264
        if (!array_key_exists($offset, $this->set)) {
188 2
            throw new \OutOfRangeException('Nothing found at this offset');
189
        }
190
191 262
        return $this->set[$offset];
192
    }
193
194
    /**
195
     * @param int $offset
196
     * @param mixed $value
197
     */
198
    public function offsetSet($offset, $value)
199
    {
200
        throw new \RuntimeException('ScriptWitness is immutable');
201
    }
202
}
203