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

TransactionInputSerializer::getInputTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 0
dl 0
loc 7
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\Serializer\Types;
6
use BitWasp\Buffertools\Buffer;
7
use BitWasp\Buffertools\BufferInterface;
8
use BitWasp\Buffertools\Parser;
9
use BitWasp\Bitcoin\Script\Script;
10
use BitWasp\Bitcoin\Transaction\TransactionInput;
11
use BitWasp\Bitcoin\Transaction\TransactionInputInterface;
12
13
class TransactionInputSerializer
14
{
15
    /**
16
     * @var OutPointSerializer
17
     */
18
    private $outpointSerializer;
19
20
    /**
21
     * TransactionInputSerializer constructor.
22
     * @param OutPointSerializerInterface $outPointSerializer
23
     */
24
    public function __construct(OutPointSerializerInterface $outPointSerializer)
25
    {
26
        $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...
27
        $this->varstring = Types::varstring();
0 ignored issues
show
Bug introduced by
The property varstring 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...
28
        $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...
29 2692
    }
30
31 2692
    /**
32 2692
     * @param TransactionInputInterface $input
33 2692
     * @return BufferInterface
34
     */
35
    public function serialize(TransactionInputInterface $input)
36
    {
37
        return new Buffer(
38 2692
            $this->outpointSerializer->serialize($input->getOutPoint())->getBinary() .
39
            $this->varstring->write($input->getScript()->getBuffer()) .
40 2692
            $this->uint32le->write($input->getSequence())
41 2692
        );
42 2692
    }
43 2692
44
    /**
45
     * @param Parser $parser
46
     * @return TransactionInput
47
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
48
     */
49
    public function fromParser(Parser $parser)
50 2602
    {
51
        return new TransactionInput(
52 2602
            $this->outpointSerializer->fromParser($parser),
53 2602
            new Script($this->varstring->read($parser)),
54 2602
            $this->uint32le->read($parser)
55 2602
        );
56 2602
    }
57 1301
58 1301
    /**
59
     * @param BufferInterface|string $string
60
     * @return TransactionInput
61
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
62
     */
63
    public function parse($string)
64
    {
65
        return $this->fromParser(new Parser($string));
66 198
    }
67
}
68