Completed
Pull Request — master (#195)
by thomas
26:54 queued 01:40
created

ScriptNum::__construct()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 27
ccs 0
cts 23
cp 0
rs 4.9091
cc 9
eloc 15
nc 9
nop 4
crap 90
1
<?php
2
3
4
namespace BitWasp\Bitcoin\Script\Interpreter;
5
6
use BitWasp\Bitcoin\Exceptions\ScriptStackException;
7
use BitWasp\Bitcoin\Flags;
8
use BitWasp\Bitcoin\Math\Math;
9
use BitWasp\Bitcoin\Script\Interpreter\InterpreterInterface;
10
use BitWasp\Buffertools\Buffer;
11
12
class ScriptNum extends Buffer
13
{
14
    /**
15
     * @param Math $math
16
     * @param Flags $flags
17
     * @param Buffer $vch
18
     * @param int $size
19
     * @throws ScriptStackException
20
     * @throws \Exception
21
     */
22
    public function __construct(Math $math, Flags $flags, Buffer $vch, $size = 4)
23
    {
24
        if (!is_numeric($size)) {
25
            throw new \RuntimeException('ScriptNum size must be numeric');
26
        }
27
28
        $bufferSize = $vch->getSize();
29
        if ($bufferSize > $size) {
30
            throw new ScriptStackException('a'.InterpreterInterface::VERIFY_MINIMALDATA);//, 'Script number overflow');
31
        }
32
33
        $str = $vch->getBinary();
34
        if ($flags->checkFlags(InterpreterInterface::VERIFY_MINIMALDATA) && $bufferSize > 0) {
35
            if ((ord($str[0]) & 0x7f) === 0) {
36
                if ($bufferSize <= 1 || (ord($str[1]) & 0x7f) === 0) {
37
                    throw new ScriptStackException('b'.InterpreterInterface::VERIFY_MINIMALDATA);//, 'Non-minimally encoded integer');
38
                }
39
            }
40
        }
41
42
        if ($bufferSize === 0) {
43
            $str = "\x00";
44
        }
45
46
        $this->math = $math;
47
        parent::__construct($str, $size, $math);
48
    }
49
50
    /**
51
     * @return Buffer
52
     */
53
    public function getBuffer()
54
    {
55
        return new Buffer($this->buffer, null, $this->math);
56
    }
57
}
58