Completed
Push — master ( 410ea8...0c9ae2 )
by thomas
105:23 queued 33:17
created

BlockHeader::__construct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.1475

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 37
ccs 21
cts 25
cp 0.84
rs 8.439
cc 6
eloc 24
nc 6
nop 6
crap 6.1475
1
<?php
2
3
namespace BitWasp\Bitcoin\Block;
4
5
use BitWasp\Buffertools\Buffer;
6
use BitWasp\Bitcoin\Crypto\Hash;
7
use BitWasp\Bitcoin\Serializable;
8
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer;
9
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
10
11
class BlockHeader extends Serializable implements BlockHeaderInterface
12
{
13
    use FunctionAliasArrayAccess;
14
15
    /**
16
     * @var int|string
17
     */
18
    private $version;
19
20
    /**
21
     * @var Buffer
22
     */
23
    private $prevBlock;
24
25
    /**
26
     * @var Buffer
27
     */
28
    private $merkleRoot;
29
30
    /**
31
     * @var int|string
32
     */
33
    private $timestamp;
34
35
    /**
36
     * @var Buffer
37
     */
38
    private $bits;
39
40
    /**
41
     * @var int|string
42
     */
43
    private $nonce;
44
45
    /**
46
     * @param int|string $version
47
     * @param Buffer $prevBlock
48
     * @param Buffer $merkleRoot
49
     * @param int|string $timestamp
50
     * @param Buffer $bits
51
     * @param int|string $nonce
52
     */
53 150
    public function __construct($version, Buffer $prevBlock, Buffer $merkleRoot, $timestamp, Buffer $bits, $nonce)
54
    {
55 150
        if (!is_numeric($version)) {
56 6
            throw new \InvalidArgumentException('Block header version must be numeric');
57
        }
58
59 144
        if ($prevBlock->getSize() !== 32) {
60
            throw new \InvalidArgumentException('Block header prevBlock must be a 32-byte Buffer');
61
        }
62
63 144
        if ($merkleRoot->getSize() !== 32) {
64
            throw new \InvalidArgumentException('Block header prevBlock must be a 32-byte Buffer');
65
        }
66
67 144
        if (!is_numeric($timestamp)) {
68
            throw new \InvalidArgumentException('Block header timestamp must be numeric');
69
        }
70
71 144
        if (!is_numeric($nonce)) {
72
            throw new \InvalidArgumentException('Block header nonce must be numeric');
73
        }
74
75 144
        $this->version = $version;
0 ignored issues
show
Documentation Bug introduced by
It seems like $version can also be of type double. However, the property $version is declared as type integer|string. 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...
76 144
        $this->prevBlock = $prevBlock;
77 144
        $this->merkleRoot = $merkleRoot;
78 144
        $this->timestamp = $timestamp;
0 ignored issues
show
Documentation Bug introduced by
It seems like $timestamp can also be of type double. However, the property $timestamp is declared as type integer|string. 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...
79 144
        $this->bits = $bits;
80 144
        $this->nonce = $nonce;
0 ignored issues
show
Documentation Bug introduced by
It seems like $nonce can also be of type double. However, the property $nonce is declared as type integer|string. 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...
81
82 144
        $this
83 144
            ->initFunctionAlias('version', 'getVersion')
84 144
            ->initFunctionAlias('prevBlock', 'getPrevBlock')
85 144
            ->initFunctionAlias('merkleRoot', 'getMerkleRoot')
86 144
            ->initFunctionAlias('timestamp', 'getTimestamp')
87 144
            ->initFunctionAlias('bits', 'getBits')
88 144
            ->initFunctionAlias('nonce', 'getNonce');
89 144
    }
90
91
    /**
92
     * @return Buffer
93
     */
94 1158
    public function getHash()
95
    {
96 1158
        return Hash::sha256d($this->getBuffer())->flip();
97
    }
98
99
    /**
100
     * Get the version for this block
101
     *
102
     * {@inheritdoc}
103
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getVersion()
104
     */
105 1218
    public function getVersion()
106
    {
107 1218
        return $this->version;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getPrevBlock()
113
     */
114 1212
    public function getPrevBlock()
115
    {
116 1212
        return $this->prevBlock;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getMerkleRoot()
122
     */
123 1230
    public function getMerkleRoot()
124
    {
125 1230
        return $this->merkleRoot;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getBits()
131
     */
132 1206
    public function getBits()
133
    {
134 1206
        return $this->bits;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getNonce()
140
     */
141 1212
    public function getNonce()
142
    {
143 1212
        return $this->nonce;
144
    }
145
146
    /**
147
     * Get the timestamp for this block
148
     *
149
     * {@inheritdoc}
150
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getTimestamp()
151
     */
152 1212
    public function getTimestamp()
153
    {
154 1212
        return $this->timestamp;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     * @see \BitWasp\Buffertools\SerializableInterface::getBuffer()
160
     */
161 1164
    public function getBuffer()
162
    {
163 1164
        return (new BlockHeaderSerializer())->serialize($this);
164
    }
165
}
166