Completed
Pull Request — master (#524)
by thomas
71:45
created

Conditional   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A getOp() 0 4 1
A setValue() 0 8 2
A hasValue() 0 4 1
A getValue() 0 8 2
A providedBy() 0 4 1
A serialize() 0 8 4
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
    public function __construct($opcode)
30
    {
31
        if ($opcode !== Opcodes::OP_IF && $opcode !== Opcodes::OP_NOTIF) {
32
            throw new \RuntimeException("Opcode for conditional is only IF / NOTIF");
33
        }
34
35
        $this->opcode = $opcode;
36
    }
37
38
    /**
39
     * @return int
40
     */
41
    public function getOp()
42
    {
43
        return $this->opcode;
44
    }
45
46
    /**
47
     * @param bool $value
48
     */
49
    public function setValue($value)
50
    {
51
        if (!is_bool($value)) {
52
            throw new \RuntimeException("Invalid value for conditional");
53
        }
54
55
        $this->value = $value;
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function hasValue()
62
    {
63
        return null !== $this->value;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function getValue()
70
    {
71
        if (null === $this->value) {
72
            throw new \RuntimeException("Value not set on conditional");
73
        }
74
75
        return $this->value;
76
    }
77
78
    public function providedBy(Checksig $checksig)
79
    {
80
        $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
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function serialize()
87
    {
88
        if ($this->hasValue() && null === $this->providedBy) {
89
            return [$this->value ? new Buffer("\x01") : new Buffer()];
90
        }
91
92
        return [];
93
    }
94
}
95