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

Operation   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A encode() 0 8 2
A isPush() 0 4 1
A isLogical() 0 4 2
A getOp() 0 4 1
A getData() 0 4 1
A getDataSize() 0 8 2
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 2566
37
    /**
38 2566
     * Operation constructor.
39 2566
     * @param int $opCode
40 2566
     * @param BufferInterface $pushData
41 2566
     * @param int $pushDataSize
42 2566
     */
43
    public function __construct($opCode, BufferInterface $pushData, $pushDataSize = 0)
44
    {
45
        $this->push = $opCode >= 0 && $opCode <= Opcodes::OP_PUSHDATA4;
46
        $this->opCode = $opCode;
47 2528
        $this->pushData = $pushData;
48
        $this->pushDataSize = $pushDataSize;
49 2528
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function encode()
55 2546
    {
56
        if ($this->push) {
57 2546
            return $this->pushData;
58
        } else {
59
            return $this->opCode;
60
        }
61
    }
62
63 2532
    /**
64
     * @return bool
65 2532
     */
66
    public function isPush()
67
    {
68
        return $this->push;
69
    }
70
71 1900
    /**
72
     * @return bool
73 1900
     */
74
    public function isLogical()
75
    {
76
        return !$this->isPush() && in_array($this->opCode, self::$logical);
77 1900
    }
78
79
80
    /**
81
     * @return int
82
     */
83
    public function getOp()
84
    {
85
        return $this->opCode;
86
    }
87
88
    /**
89
     * @return BufferInterface
90
     */
91
    public function getData()
92
    {
93
        return $this->pushData;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getDataSize()
100
    {
101
        if (!$this->push) {
102
            throw new \RuntimeException("Op wasn't a push operation");
103
        }
104
105
        return $this->pushDataSize;
106
    }
107
}
108