Completed
Pull Request — master (#525)
by thomas
26:53
created

Operation::isPush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Parser;
4
5
use BitWasp\Bitcoin\Script\Opcodes;
6
use BitWasp\Buffertools\BufferInterface;
7
8
class Operation
9
{
10
    /**
11
     * @var int[]
12
     */
13
    protected static $logical = [
14
        Opcodes::OP_IF, Opcodes::OP_NOTIF, Opcodes::OP_ELSE, Opcodes::OP_ENDIF,
15
    ];
16
17
    /**
18
     * @var bool
19
     */
20
    private $push;
21
22
    /**
23
     * @var int
24
     */
25
    private $opCode;
26
27
    /**
28
     * @var BufferInterface
29
     */
30
    private $pushData;
31
32
    /**
33
     * @var int
34
     */
35
    private $pushDataSize;
36
37
    /**
38
     * Operation constructor.
39
     * @param int $opCode
40
     * @param BufferInterface $pushData
41
     * @param int $pushDataSize
42
     */
43 2648
    public function __construct($opCode, BufferInterface $pushData, $pushDataSize = 0)
44
    {
45 2648
        $this->push = $opCode >= 0 && $opCode <= Opcodes::OP_PUSHDATA4;
46 2648
        $this->opCode = $opCode;
47 2648
        $this->pushData = $pushData;
48 2648
        $this->pushDataSize = $pushDataSize;
49 2648
    }
50
51
    /**
52
     * @return string
53
     */
54 154
    public function encode()
55
    {
56 154
        if ($this->push) {
57 152
            return $this->pushData;
58
        } else {
59 154
            return $this->opCode;
60
        }
61
    }
62
63
    /**
64
     * @return bool
65
     */
66 2610
    public function isPush()
67
    {
68 2610
        return $this->push;
69
    }
70
71
    /**
72
     * @return bool
73
     */
74 154
    public function isLogical()
75
    {
76 154
        return !$this->isPush() && in_array($this->opCode, self::$logical);
77
    }
78
79
80
    /**
81
     * @return int
82
     */
83 2628
    public function getOp()
84
    {
85 2628
        return $this->opCode;
86
    }
87
88
    /**
89
     * @return BufferInterface
90
     */
91 2596
    public function getData()
92
    {
93 2596
        return $this->pushData;
94
    }
95
96
    /**
97
     * @return int
98
     */
99 1964
    public function getDataSize()
100
    {
101 1964
        if (!$this->push) {
102
            throw new \RuntimeException("Op wasn't a push operation");
103
        }
104
105 1964
        return $this->pushDataSize;
106
    }
107
}
108