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

Number   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80.65%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 144
wmc 24
lcom 1
cbo 3
ccs 50
cts 62
cp 0.8065
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A int() 0 7 2
B buffer() 0 21 8
B parseBuffer() 0 26 4
C serialize() 0 26 7
A getBuffer() 0 4 1
A getInt() 0 4 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Interpreter;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Flags;
7
use BitWasp\Bitcoin\Math\Math;
8
use BitWasp\Bitcoin\Serializable;
9
use BitWasp\Buffertools\Buffer;
10
11
class Number extends Serializable
12
{
13
    const MAX_NUM_SIZE = 4;
14
15
    /**
16
     * @var int
17
     */
18
    protected $number;
19
20
    /**
21
     * @var Flags
22
     */
23
    protected $flags;
24
25
    /**
26
     * Number constructor.
27
     * @param int $number
28
     * @param Math $math
29
     */
30 762
    public function __construct($number, Math $math)
31
    {
32 762
        $this->number = $number;
33 762
        $this->math = $math;
0 ignored issues
show
Bug introduced by
The property math does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34 762
    }
35
36
    /**
37
     * @param int $number
38
     * @param Math|null $math
39
     * @return self
40
     */
41 222
    public static function int($number, Math $math = null)
42
    {
43 222
        return new self(
44 222
            $number,
45 222
            $math ?: Bitcoin::getMath()
46 222
        );
47
    }
48
49
    /**
50
     * @param Buffer $vch
51
     * @param bool $fRequireMinimal
52
     * @param int $maxNumSize
53
     * @param Math|null $math
54
     * @return self
55
     */
56 762
    public static function buffer(Buffer $vch, $fRequireMinimal, $maxNumSize = self::MAX_NUM_SIZE, Math $math = null)
57
    {
58 762
        $size = $vch->getSize();
59 762
        if ($size > $maxNumSize) {
60
            throw new \RuntimeException('Script number overflow');
61
        }
62
63 762
        if ($fRequireMinimal && $size > 0) {
64
            $binary = $vch->getBinary();
65
            if (ord($binary[$size - 1]) & 0x7f === 0) {
66
                if ($size <= 1 || ord($binary[$size - 2]) & 0x80 === 0) {
67
                    throw new \RuntimeException('Non-minimally encoded script number');
68
                }
69
            }
70
        }
71
72 762
        $math = $math ?: Bitcoin::getMath();
73 762
        $number = new self(0, $math);
74 762
        $number->number = $number->parseBuffer($vch);
0 ignored issues
show
Documentation Bug introduced by
It seems like $number->parseBuffer($vch) can also be of type string. However, the property $number is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75 762
        return $number;
76
    }
77
78
    /**
79
     * @param Buffer $buffer
80
     * @return int|string
81
     */
82 762
    private function parseBuffer(Buffer $buffer)
83
    {
84 762
        $size = $buffer->getSize();
85 762
        if ($size === 0) {
86 762
            return '0';
87
        }
88
89
        $chars = array_map(function ($binary) {
90 762
            return ord($binary);
91
92 762
        }, str_split($buffer->flip()->getBinary(), 1));
93
94 762
        $result = 0;
95 762
        for ($i = 0; $i < $size; $i++) {
96 762
            $mul = $this->math->mul($i, 8);
97 762
            $byte = $this->math->leftShift($chars[$i], $mul);
98 762
            $result = $this->math->bitwiseOr($result, $byte);
99 762
        }
100
101 762
        if ($chars[0] & 0x80) {
102
            $mask = gmp_strval(gmp_com($this->math->leftShift(0x80, (8 * ($size - 1)))), 10);
103
            return $this->math->sub(0, $this->math->bitwiseAnd($result, $mask));
104
        }
105
106 762
        return $result;
107
    }
108
109
    /**
110
     * @return Buffer
111
     */
112 762
    private function serialize()
113
    {
114 762
        if ($this->math->cmp($this->number, '0') === 0) {
115 762
            return new Buffer('', 4);
116
        }
117
118
        // Using array of integers instead of bytes
119 762
        $result = [];
120 762
        $negative = $this->math->cmp($this->number, 0) < 0;
121 762
        $abs = $negative ? $this->math->sub(0, $this->number) : $this->number;
122 762
        while ($this->math->cmp($abs, 0) > 0) {
123 762
            array_unshift($result, (int)$this->math->bitwiseAnd($abs, 0xff));
124 762
            $abs = $this->math->rightShift($abs, 8);
125 762
        }
126
127 762
        if ($result[0] & 0x80) {
128
            array_unshift($result, $negative ? 0x80 : 0x00);
129 762
        } else if ($negative) {
130
            array_unshift($result, 0x80);
131
        }
132
133 762
        return new Buffer(array_reduce($result, function ($agg, $current) {
134 762
            return $agg . chr($current);
135 762
        }), 4, $this->math);
136
137
    }
138
139
    /**
140
     * @return Buffer
141
     */
142 762
    public function getBuffer()
143
    {
144 762
        return $this->serialize();
145
    }
146
147
    /**
148
     * @return int
149
     */
150 192
    public function getInt()
151
    {
152 192
        return $this->number;
153
    }
154
}
155