Completed
Push — master ( aa76e6...0e5776 )
by thomas
66:20 queued 34:22
created

TransactionInputSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A serialize() 0 8 1
A fromParser() 0 8 1
A parse() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Serializer\Transaction;
6
7
use BitWasp\Bitcoin\Script\Opcodes;
8
use BitWasp\Bitcoin\Script\Script;
9
use BitWasp\Bitcoin\Serializer\Types;
10
use BitWasp\Bitcoin\Transaction\TransactionInput;
11
use BitWasp\Bitcoin\Transaction\TransactionInputInterface;
12
use BitWasp\Buffertools\Buffer;
13
use BitWasp\Buffertools\BufferInterface;
14
use BitWasp\Buffertools\Parser;
15
16
class TransactionInputSerializer
17
{
18
    /**
19
     * @var OutPointSerializer
20
     */
21
    private $outpointSerializer;
22
23
    /**
24
     * @var \BitWasp\Buffertools\Types\VarString
25
     */
26
    private $varstring;
27
28
    /**
29
     * @var \BitWasp\Buffertools\Types\Uint32
30
     */
31
    private $uint32le;
32
33
    /**
34
     * @var Opcodes
35
     */
36
    private $opcodes;
37
38
    /**
39
     * TransactionInputSerializer constructor.
40
     * @param OutPointSerializerInterface $outPointSerializer
41
     * @param Opcodes|null $opcodes
42
     */
43 5236
    public function __construct(OutPointSerializerInterface $outPointSerializer, Opcodes $opcodes = null)
44
    {
45 5236
        $this->outpointSerializer = $outPointSerializer;
0 ignored issues
show
Documentation Bug introduced by
$outPointSerializer is of type object<BitWasp\Bitcoin\S...intSerializerInterface>, but the property $outpointSerializer was declared to be of type object<BitWasp\Bitcoin\S...ion\OutPointSerializer>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
46 5236
        $this->varstring = Types::varstring();
47 5236
        $this->uint32le = Types::uint32le();
48 5236
        $this->opcodes = $opcodes ?: new Opcodes();
49 5236
    }
50
51
    /**
52
     * @param TransactionInputInterface $input
53
     * @return BufferInterface
54
     */
55 5220
    public function serialize(TransactionInputInterface $input): BufferInterface
56
    {
57 5220
        return new Buffer(
58 5220
            $this->outpointSerializer->serialize($input->getOutPoint())->getBinary() .
59 5220
            $this->varstring->write($input->getScript()->getBuffer()) .
60 5220
            $this->uint32le->write($input->getSequence())
61
        );
62
    }
63
64
    /**
65
     * @param Parser $parser
66
     * @return TransactionInputInterface
67
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
68
     * @throws \Exception
69
     */
70 68
    public function fromParser(Parser $parser): TransactionInputInterface
71
    {
72 68
        return new TransactionInput(
73 68
            $this->outpointSerializer->fromParser($parser),
74 68
            new Script($this->varstring->read($parser), $this->opcodes),
75 68
            (int) $this->uint32le->read($parser)
76
        );
77
    }
78
79
    /**
80
     * @param BufferInterface $string
81
     * @return TransactionInput
82
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
83
     * @throws \Exception
84
     */
85 2
    public function parse(BufferInterface $string): TransactionInput
86
    {
87 2
        return $this->fromParser(new Parser($string));
88
    }
89
}
90