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

ScriptWitnessSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 9
cts 15
cp 0.6
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromParser() 0 10 2
A serialize() 0 10 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Script;
4
5
use BitWasp\Bitcoin\Script\ScriptWitness;
6
use BitWasp\Bitcoin\Script\ScriptWitnessInterface;
7
use BitWasp\Bitcoin\Serializer\Types;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Buffertools\BufferInterface;
10
use BitWasp\Buffertools\Buffertools;
11
use BitWasp\Buffertools\Parser;
12
use BitWasp\Buffertools\Template;
13
14
class ScriptWitnessSerializer
15
{
16
    /**
17
     * @var Template
18
     */
19
    private $varstring;
20
21 1332
    public function __construct()
22
    {
23 1332
        $this->varstring = Types::varstring();
0 ignored issues
show
Documentation Bug introduced by
It seems like \BitWasp\Bitcoin\Serializer\Types::varstring() of type object<BitWasp\Buffertools\Types\VarString> is incompatible with the declared type object<BitWasp\Buffertools\Template> of property $varstring.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24 1332
    }
25
26
    /**
27
     * @param Parser $parser
28
     * @param $size
29
     * @return ScriptWitness
30
     */
31
    public function fromParser(Parser $parser, $size)
32
    {
33
        $entries = [];
34
        for ($j = 0; $j < $size; $j++) {
35
            $data = $this->varstring->parse($parser);
36
            $entries[] = $data;
37
        }
38
39
        return new ScriptWitness($entries);
40
    }
41
42
    /**
43
     * @param ScriptWitnessInterface $witness
44
     * @return BufferInterface
45
     */
46 22
    public function serialize(ScriptWitnessInterface $witness)
47
    {
48 22
        $parser = new Parser();
49 22
        $parser->appendBuffer(Buffertools::numToVarInt($witness->count()));
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...
50 22
        foreach ($witness as $value) {
51 22
            $parser->appendBuffer(new Buffer($this->varstring->write($value)));
0 ignored issues
show
Documentation introduced by
$this->varstring->write($value) is of type object<BitWasp\Buffertools\Buffer>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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...
52
        }
53
54 22
        return $parser->getBuffer();
55
    }
56
}
57