Completed
Pull Request — master (#278)
by thomas
73:26 queued 01:49
created

ScriptWitness::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3.0175
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Bitcoin\Collection\StaticCollection;
6
use BitWasp\Bitcoin\Serializer\Script\ScriptWitnessSerializer;
7
use BitWasp\Buffertools\BufferInterface;
8
9
class ScriptWitness extends StaticCollection implements ScriptWitnessInterface
10
{
11
    /**
12
     * ScriptWitness constructor.
13
     * @param BufferInterface[] $sigValues
14
     */
15 90
    public function __construct(array $sigValues)
16
    {
17 90
        $this->set = new \SplFixedArray(count($sigValues));
18 90
        foreach ($sigValues as $idx => $push) {
19 30
            if (!$push instanceof BufferInterface) {
20
                throw new \InvalidArgumentException('Must provide BufferInterface[] to ScriptWitness');
21
            }
22 30
            $this->set->offsetSet($idx, $push);
23 90
        }
24 90
    }
25
26
    public function __clone()
27
    {
28
        $outputs = $this->set;
29
        $this->set = new \SplFixedArray(count($outputs));
30
31
        foreach ($outputs as $idx => $output) {
32
            $this->set->offsetSet($idx, $output);
33
        }
34
    }
35
36
    /**
37
     * @return BufferInterface
38
     */
39
    public function bottom()
40
    {
41
        return parent::offsetGet(count($this) - 1);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetGet() instead of bottom()). Are you sure this is correct? If so, you might want to change this to $this->offsetGet().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
42
    }
43
44
    /**
45
     * @return BufferInterface
46
     */
47 30
    public function current()
48
    {
49 30
        return $this->set->current();
50
    }
51
52
    /**
53
     * @param int $offset
54
     * @return BufferInterface
55
     */
56 30
    public function offsetGet($offset)
57
    {
58 30
        if (!$this->set->offsetExists($offset)) {
59
            throw new \OutOfRangeException('No offset found');
60
        }
61
62 30
        return $this->set->offsetGet($offset);
63
    }
64
65
    /**
66
     * @param int $start
67
     * @param int $length
68
     * @return ScriptWitness
69
     */
70 18
    public function slice($start, $length)
71
    {
72 18
        $end = $this->set->getSize();
73 18
        if ($start > $end || $length > $end) {
74
            throw new \RuntimeException('Invalid start or length');
75
        }
76
77 18
        $sliced = array_slice($this->set->toArray(), $start, $length);
78 18
        return new self($sliced);
79
    }
80
81
    /**
82
     * @param ScriptWitnessInterface $witness
83
     * @return bool
84 54
     */
85
    public function equals(ScriptWitnessInterface $witness)
86 54
    {
87
        $nStack = count($this);
88
        if ($nStack !== count($witness)) {
89
            return false;
90
        }
91
92
        for ($i = 0; $i < $nStack; $i++) {
93
            if (false === $this->offsetGet($i)->equals($witness->offsetGet($i))) {
94
                return false;
95
            }
96
        }
97
98
        return true;
99
    }
100
101
    /**
102
     * @return \BitWasp\Buffertools\BufferInterface
103
     */
104
    public function getBuffer()
105
    {
106
        return (new ScriptWitnessSerializer())->serialize($this);
107
    }
108
}
109