Completed
Pull Request — master (#446)
by thomas
157:14 queued 87:18
created

BloomFilterSerializer::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Bloom;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Bloom\BloomFilter;
7
use BitWasp\Bitcoin\Serializer\Types;
8
use BitWasp\Buffertools\BufferInterface;
9
use BitWasp\Buffertools\Buffertools;
10
use BitWasp\Buffertools\Parser;
11
use BitWasp\Buffertools\Template;
12
13
class BloomFilterSerializer
14
{
15
    /**
16
     * @var Template
17
     */
18 48
    private $template;
19
20 48
    public function __construct()
21 48
    {
22 36
        $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...
23 48
        $this->uint8le = Types::uint8le();
0 ignored issues
show
Bug introduced by
The property uint8le 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 48
        $this->template = new Template([
25 48
            Types::vector(function (Parser $parser) {
26 48
                return $parser->readBytes(1)->getInt();
27 48
            }),
28
            $this->uint32le,
29
            $this->uint32le,
30
            $this->uint8le
31
        ]);
32
    }
33
34 24
    /**
35
     * @param BloomFilter $filter
36 24
     * @return BufferInterface
37
     */
38 24
    public function serialize(BloomFilter $filter)
39 24
    {
40 24
        $parser = new Parser();
41 12
        $parser->appendBuffer(Buffertools::numToVarInt(count($filter->getData())));
0 ignored issues
show
Bug introduced by
The method appendBuffer() does not seem to exist on object<BitWasp\Buffertools\Parser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        foreach ($filter->getData() as $i) {
43 24
            $parser->writeRawBinary(1, pack('c', $i));
0 ignored issues
show
Bug introduced by
The method writeRawBinary() does not seem to exist on object<BitWasp\Buffertools\Parser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44 24
        }
45 24
46 24
        $parser->writeRawBinary(4, $this->uint32le->write($filter->getNumHashFuncs()));
0 ignored issues
show
Bug introduced by
The method writeRawBinary() does not seem to exist on object<BitWasp\Buffertools\Parser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47 24
        $parser->writeRawBinary(4, $this->uint32le->write($filter->getTweak()));
0 ignored issues
show
Bug introduced by
The method writeRawBinary() does not seem to exist on object<BitWasp\Buffertools\Parser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48 12
        $parser->writeRawBinary(1, $this->uint8le->write($filter->getFlags()));
0 ignored issues
show
Bug introduced by
The method writeRawBinary() does not seem to exist on object<BitWasp\Buffertools\Parser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
50
        return $parser->getBuffer();
51
    }
52
53
    /**
54
     * @param Parser $parser
55 36
     * @return BloomFilter
56
     */
57 36
    public function fromParser(Parser $parser)
58
    {
59 36
        list ($vData, $numHashFuncs, $nTweak, $flags) = $this->template->parse($parser);
60 36
61 18
        return new BloomFilter(
62 18
            Bitcoin::getMath(),
63 18
            $vData,
64
            $numHashFuncs,
65 18
            $nTweak,
66
            $flags
67
        );
68
    }
69
70
    /**
71
     * @param string|BufferInterface $data
72 36
     * @return BloomFilter
73
     */
74 36
    public function parse($data)
75
    {
76
        return $this->fromParser(new Parser($data));
77
    }
78
}
79