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

functions.php ➔ encodeOpN()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 1
dl 0
loc 12
ccs 3
cts 3
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
function decodeOpN($op)
6
{
7 1560
    if ($op === Opcodes::OP_0) {
8 172
        return 0;
9
    }
10
11 1426
    if (!($op === Opcodes::OP_1NEGATE || $op >= Opcodes::OP_1 && $op <= Opcodes::OP_16)) {
12 1426
        throw new \RuntimeException("Invalid opcode");
13
    }
14
15
    return (int) $op - (Opcodes::OP_1 - 1);
16
}
17 68
18 24
function encodeOpN($op)
19
{
20
    if ($op === 0) {
21 44
        return Opcodes::OP_0;
22 44
    }
23
24
    if (!($op === -1 || $op >= 1 && $op <= 16)) {
25
        throw new \RuntimeException("Invalid value");
26
    }
27
28
    return (int) Opcodes::OP_1 + $op - 1;
29
}
30