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

TransactionInputSerializer::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Transaction;
4
5
use BitWasp\Bitcoin\Script\Script;
6
use BitWasp\Bitcoin\Serializer\Types;
7
use BitWasp\Bitcoin\Transaction\TransactionInput;
8
use BitWasp\Bitcoin\Transaction\TransactionInputInterface;
9
use BitWasp\Buffertools\Buffer;
10
use BitWasp\Buffertools\BufferInterface;
11
use BitWasp\Buffertools\Parser;
12
13
class TransactionInputSerializer
14
{
15
    /**
16
     * @var OutPointSerializer
17
     */
18
    private $outpointSerializer;
19
20
    /**
21
     * @var \BitWasp\Buffertools\Types\VarString
22
     */
23
    private $varstring;
24
25
    /**
26
     * @var \BitWasp\Buffertools\Types\Uint32
27
     */
28
    private $uint32le;
29
30
    /**
31
     * TransactionInputSerializer constructor.
32
     * @param OutPointSerializerInterface $outPointSerializer
33
     */
34 1336
    public function __construct(OutPointSerializerInterface $outPointSerializer)
35
    {
36 1336
        $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...
37 1336
        $this->varstring = Types::varstring();
38 1336
        $this->uint32le = Types::uint32le();
39 1336
    }
40
41
    /**
42
     * @param TransactionInputInterface $input
43
     * @return BufferInterface
44
     */
45 1306
    public function serialize(TransactionInputInterface $input)
46
    {
47 1306
        return new Buffer(
48 1306
            $this->outpointSerializer->serialize($input->getOutPoint())->getBinary() .
49 1306
            $this->varstring->write($input->getScript()->getBuffer()) .
50 1306
            $this->uint32le->write($input->getSequence())
51
        );
52
    }
53
54
    /**
55
     * @param Parser $parser
56
     * @return TransactionInput
57
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
58
     */
59 66
    public function fromParser(Parser $parser)
60
    {
61 66
        return new TransactionInput(
62 66
            $this->outpointSerializer->fromParser($parser),
63 66
            new Script($this->varstring->read($parser)),
64 66
            $this->uint32le->read($parser)
65
        );
66
    }
67
68
    /**
69
     * @param BufferInterface|string $string
70
     * @return TransactionInput
71
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
72
     */
73 4
    public function parse($string)
74
    {
75 4
        return $this->fromParser(new Parser($string));
76
    }
77
}
78