Completed
Push — master ( 595279...40a4e1 )
by thomas
27:02
created

Conditional::getOp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction\Factory;
4
5
use BitWasp\Bitcoin\Script\Opcodes;
6
use BitWasp\Buffertools\Buffer;
7
8
class Conditional
9
{
10
    /**
11
     * @var int
12
     */
13
    private $opcode;
14
15
    /**
16
     * @var bool
17
     */
18
    private $value;
19
20
    /**
21
     * @var null
22
     */
23
    private $providedBy = null;
24
25
    /**
26
     * Conditional constructor.
27
     * @param int $opcode
28
     */
29 22
    public function __construct($opcode)
30
    {
31 22
        if ($opcode !== Opcodes::OP_IF && $opcode !== Opcodes::OP_NOTIF) {
32
            throw new \RuntimeException("Opcode for conditional is only IF / NOTIF");
33
        }
34
35 22
        $this->opcode = $opcode;
36 22
    }
37
38
    /**
39
     * @return int
40
     */
41
    public function getOp()
42
    {
43
        return $this->opcode;
44
    }
45
46
    /**
47
     * @param bool $value
48
     */
49 22
    public function setValue($value)
50
    {
51 22
        if (!is_bool($value)) {
52
            throw new \RuntimeException("Invalid value for conditional");
53
        }
54
55 22
        $this->value = $value;
56 22
    }
57
58
    /**
59
     * @return bool
60
     */
61 22
    public function hasValue()
62
    {
63 22
        return null !== $this->value;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69 22
    public function getValue()
70
    {
71 22
        if (null === $this->value) {
72
            throw new \RuntimeException("Value not set on conditional");
73
        }
74
75 22
        return $this->value;
76
    }
77
78 8
    public function providedBy(Checksig $checksig)
79
    {
80 8
        $this->providedBy = $checksig;
0 ignored issues
show
Documentation Bug introduced by
It seems like $checksig of type object<BitWasp\Bitcoin\T...ction\Factory\Checksig> is incompatible with the declared type null of property $providedBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81 8
    }
82
83
    /**
84
     * @return array
85
     */
86 22
    public function serialize()
87
    {
88 22
        if ($this->hasValue() && null === $this->providedBy) {
89 18
            return [$this->value ? new Buffer("\x01") : new Buffer()];
90
        }
91
92 10
        return [];
93
    }
94
}
95