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

Conditional::providedBy()   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 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Transaction\Factory;
6
7
use BitWasp\Bitcoin\Script\Opcodes;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Buffertools\BufferInterface;
10
11
class Conditional
12
{
13
    /**
14
     * @var int
15
     */
16
    private $opcode;
17
18
    /**
19
     * @var bool
20
     */
21
    private $value;
22
23
    /**
24
     * @var null
25
     */
26
    private $providedBy = null;
27
28
    /**
29
     * Conditional constructor.
30
     * @param int $opcode
31
     */
32
    public function __construct(int $opcode)
33
    {
34
        if ($opcode !== Opcodes::OP_IF && $opcode !== Opcodes::OP_NOTIF) {
35
            throw new \RuntimeException("Opcode for conditional is only IF / NOTIF");
36
        }
37
38
        $this->opcode = $opcode;
39
    }
40
41
    /**
42
     * @return int
43
     */
44
    public function getOp(): int
45
    {
46
        return $this->opcode;
47
    }
48
49
    /**
50
     * @param bool $value
51
     */
52
    public function setValue(bool $value)
53
    {
54
        $this->value = $value;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function hasValue(): bool
61
    {
62
        return null !== $this->value;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function getValue(): bool
69
    {
70
        if (null === $this->value) {
71
            throw new \RuntimeException("Value not set on conditional");
72
        }
73
74
        return $this->value;
75
    }
76
77
    /**
78
     * @param Checksig $checksig
79
     */
80
    public function providedBy(Checksig $checksig)
81
    {
82
        $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...
83
    }
84
85
    /**
86
     * @return BufferInterface[]
87
     */
88
    public function serialize(): array
89
    {
90
        if ($this->hasValue() && null === $this->providedBy) {
91
            return [$this->value ? new Buffer("\x01") : new Buffer()];
92
        }
93
94
        return [];
95
    }
96
}
97