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

BlockLocatorSerializer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A fromParser() 0 12 2
A parse() 0 4 1
A serialize() 0 10 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Chain;
4
5
use BitWasp\Bitcoin\Chain\BlockLocator;
6
use BitWasp\Bitcoin\Serializer\Types;
7
use BitWasp\Buffertools\Buffer;
8
use BitWasp\Buffertools\BufferInterface;
9
use BitWasp\Buffertools\Buffertools;
10
use BitWasp\Buffertools\Parser;
11
use BitWasp\Buffertools\Template;
12
13
class BlockLocatorSerializer
14
{
15
16
    /**
17
     * @var Template
18
     */
19
    private $template;
20
21 2
    public function __construct()
22
    {
23 2
        $this->varint = Types::varint();
0 ignored issues
show
Bug introduced by
The property varint 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...
24 2
        $this->bytestring32 = $bytestring32 = Types::bytestringle(32);
0 ignored issues
show
Bug introduced by
The property bytestring32 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...
25 2
        $this->template = new Template([
26 2
            Types::vector(function (Parser $parser) use ($bytestring32) {
27
                return $bytestring32->read($parser);
28 2
            }),
29 2
            $bytestring32
30
        ]);
31 2
    }
32
33
    /**
34
     * @param Parser $parser
35
     * @return BlockLocator
36
     */
37 2
    public function fromParser(Parser $parser)
38
    {
39 2
        $numHashes = $this->varint->read($parser);
40 2
        $hashes = [];
41 2
        for ($i = 0; $i < $numHashes; $i++) {
42 2
            $hashes[] = $this->bytestring32->read($parser);
43
        }
44
45 2
        $hashStop = $this->bytestring32->read($parser);
46
47 2
        return new BlockLocator($hashes, $hashStop);
48
    }
49
50
    /**
51
     * @param BufferInterface|string $data
52
     * @return BlockLocator
53
     */
54 2
    public function parse($data)
55
    {
56 2
        return $this->fromParser(new Parser($data));
57
    }
58
59
    /**
60
     * @param BlockLocator $blockLocator
61
     * @return \BitWasp\Buffertools\BufferInterface
62
     */
63 2
    public function serialize(BlockLocator $blockLocator)
64
    {
65 2
        $binary = Buffertools::numToVarInt(count($blockLocator->getHashes()))->getBinary();
66 2
        foreach ($blockLocator->getHashes() as $hash) {
67 2
            $binary .= Buffertools::flipBytes($hash->getBinary());
68
        }
69
70 2
        $binary .= $blockLocator->getHashStop()->getBinary();
71 2
        return new Buffer($binary);
72
    }
73
}
74