Completed
Pull Request — master (#446)
by thomas
105:00 queued 101:56
created

BlockHeaderSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 11 1
A __construct() 0 6 1
A parse() 0 4 1
A fromParser() 0 15 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Block;
4
5
use BitWasp\Bitcoin\Block\BlockHeader;
6
use BitWasp\Bitcoin\Block\BlockHeaderInterface;
7
use BitWasp\Bitcoin\Serializer\Types;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Buffertools\Exceptions\ParserOutOfRange;
10
use BitWasp\Buffertools\Parser;
11
12
class BlockHeaderSerializer
13
{
14 414
    public function __construct()
15
    {
16 414
        $this->hash = Types::bytestringle(32);
0 ignored issues
show
Bug introduced by
The property hash 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...
17 414
        $this->uint32le = Types::uint32le();
0 ignored issues
show
Bug introduced by
The property uint32le 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...
18 414
        $this->int32le = Types::int32le();
0 ignored issues
show
Bug introduced by
The property int32le does not seem to exist. Did you mean uint32le?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
19 414
    }
20
21
    /**
22
     * @param \BitWasp\Buffertools\BufferInterface|string $string
23
     * @return BlockHeader
24
     * @throws ParserOutOfRange
25
     */
26 10
    public function parse($string)
27
    {
28 10
        return $this->fromParser(new Parser($string));
29
    }
30
31
    /**
32
     * @param Parser $parser
33
     * @return BlockHeader
34
     * @throws ParserOutOfRange
35
     */
36 36
    public function fromParser(Parser $parser)
37
    {
38
        try {
39 36
            return new BlockHeader(
40 36
                $this->int32le->read($parser),
0 ignored issues
show
Bug introduced by
The property int32le does not seem to exist. Did you mean uint32le?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41 32
                $this->hash->read($parser),
42 32
                $this->hash->read($parser),
43 30
                (int) $this->uint32le->read($parser),
44 30
                (int) $this->uint32le->read($parser),
45 30
                (int) $this->uint32le->read($parser)
46
            );
47 6
        } catch (ParserOutOfRange $e) {
48 6
            throw new ParserOutOfRange('Failed to extract full block header from parser');
49
        }
50
    }
51
52
    /**
53
     * @param BlockHeaderInterface $header
54
     * @return \BitWasp\Buffertools\BufferInterface
55
     */
56 396
    public function serialize(BlockHeaderInterface $header)
57
    {
58 396
        return new Buffer(
59 396
            $this->int32le->write($header->getVersion()) .
0 ignored issues
show
Bug introduced by
The property int32le does not seem to exist. Did you mean uint32le?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60 396
            $this->hash->write($header->getPrevBlock()) .
61 396
            $this->hash->write($header->getMerkleRoot()) .
62 396
            $this->uint32le->write($header->getTimestamp()) .
63 396
            $this->uint32le->write($header->getBits()) .
64 396
            $this->uint32le->write($header->getNonce())
65
        );
66
    }
67
}
68