Completed
Pull Request — master (#593)
by thomas
15:02
created

ScriptWitness::equals()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 20
rs 9.2
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;
8
use BitWasp\Bitcoin\Serializer\Script\ScriptWitnessSerializer;
9
use BitWasp\Buffertools\BufferInterface;
10
11
class ScriptWitness extends StaticBufferCollection implements ScriptWitnessInterface
12
{
13
    /**
14
     * @param ScriptWitnessInterface $witness
15
     * @return bool
16
     */
17
    public function equals(ScriptWitnessInterface $witness): bool
18
    {
19
        $nStack = count($this);
20
        if ($nStack !== count($witness)) {
21
            return false;
22
        }
23
24
        for ($i = 0; $i < $nStack; $i++) {
25
            if (false === $this->offsetGet($i)->equals($witness->offsetGet($i))) {
26
                return false;
27
            }
28
        }
29
30
        return true;
31
    }
32
33
    /**
34
     * @return \BitWasp\Buffertools\BufferInterface
35
     */
36
    public function getBuffer(): BufferInterface
37
    {
38
        return (new ScriptWitnessSerializer())->serialize($this);
39
    }
40
}
41