Completed
Pull Request — master (#239)
by thomas
49:01 queued 45:29
created

ScriptWitness   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 80
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A __clone() 0 9 2
A bottom() 0 4 1
A current() 0 4 1
A offsetGet() 0 8 2
A slice() 0 10 3
A getBuffer() 0 4 1
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
    public function __construct(array $sigValues)
16
    {
17
        $this->set = new \SplFixedArray(count($sigValues));
18
        foreach ($sigValues as $idx => $push) {
19
            if (!$push instanceof BufferInterface) {
20
                throw new \InvalidArgumentException('Must provide BufferInterface[] to ScriptWitness');
21
            }
22
            $this->set->offsetSet($idx, $push);
23
        }
24
    }
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
    public function current()
48
    {
49
        return $this->set->current();
50
    }
51
52
    /**
53
     * @param int $offset
54
     * @return BufferInterface
55
     */
56
    public function offsetGet($offset)
57
    {
58
        if (!$this->set->offsetExists($offset)) {
59
            throw new \OutOfRangeException('No offset found');
60
        }
61
62
        return $this->set->offsetGet($offset);
63
    }
64
65
    /**
66
     * @param int $start
67
     * @param int $length
68
     * @return ScriptWitness
69
     */
70
    public function slice($start, $length)
71
    {
72
        $end = $this->set->getSize();
73
        if ($start > $end || $length > $end) {
74
            throw new \RuntimeException('Invalid start or length');
75
        }
76
77
        $sliced = array_slice($this->set->toArray(), $start, $length);
78
        return new self($sliced);
79
    }
80
81
    /**
82
     * @return \BitWasp\Buffertools\BufferInterface
83
     */
84
    public function getBuffer()
85
    {
86
        return (new ScriptWitnessSerializer())->serialize($this);
87
    }
88
}
89