Completed
Push — master ( 4963c9...d80e29 )
by thomas
18:26 queued 14:36
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.1474

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