Completed
Pull Request — master (#220)
by thomas
69:26
created

TransactionInput::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 16
ccs 5
cts 6
cp 0.8333
rs 9.4286
cc 2
eloc 10
nc 2
nop 3
crap 2.0185
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
7
use BitWasp\Buffertools\Buffer;
8
use BitWasp\Bitcoin\Script\Script;
9
use BitWasp\Bitcoin\Script\ScriptInterface;
10
use BitWasp\Bitcoin\Serializable;
11
use BitWasp\Bitcoin\Serializer\Transaction\TransactionInputSerializer;
12
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
13
14
class TransactionInput extends Serializable implements TransactionInputInterface
15
{
16
    use FunctionAliasArrayAccess;
17
18
    /**
19
     * @var OutPointInterface
20
     */
21
    private $outPoint;
22
23
    /**
24
     * @var ScriptInterface
25
     */
26
    private $script;
27
28
    /**
29
     * @var string|int
30
     */
31
    private $sequence;
32
33
    /**
34
     * @param OutPointInterface $outPoint
35
     * @param ScriptInterface $script
36
     * @param int $sequence
37
     */
38
    public function __construct(OutPointInterface $outPoint, ScriptInterface $script, $sequence = self::SEQUENCE_FINAL)
39
    {
40
        if (!is_numeric($sequence)) {
41
            throw new \InvalidArgumentException('TransactionInput: sequence must be numeric');
42
        }
43 1138
44
        $this->outPoint = $outPoint;
45 1138
        $this->script = $script;
46 24
        $this->sequence = $sequence;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sequence can also be of type double. However, the property $sequence is declared as type string|integer. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
48
        $this
49 1114
            ->initFunctionAlias('outpoint', 'getOutPoint')
50
            ->initFunctionAlias('script', 'getScript')
51
            ->initFunctionAlias('sequence', 'getSequence');
52
53 1114
    }
54
55
    /**
56
     * @return TransactionInput
57 1114
     */
58 1114
    public function __clone()
59 1114
    {
60 1114
        $this->script = clone $this->script;
61 1114
    }
62 1114
63 1114
    /**
64 1114
     * @return OutPointInterface
65 1114
     */
66 1114
    public function getOutPoint()
67
    {
68
        return $this->outPoint;
69
    }
70
71
    /**
72
     * @return Script
73
     */
74
    public function getScript()
75
    {
76
        return $this->script;
77
    }
78
79
    /**
80
     * @return int
81 340
     */
82
    public function getSequence()
83 340
    {
84
        return $this->sequence;
85
    }
86
87
    /**
88
     * Check whether this transaction is a Coinbase transaction
89 352
     *
90
     * @return boolean
91 352
     */
92
    public function isCoinbase()
93
    {
94
        $math = Bitcoin::getMath();
95
        $outpoint = $this->outPoint;
96
        return $outpoint->getTxId()->getBinary() === str_pad('', 32, "\x00")
97 994
            && $math->cmp($outpoint->getVout(), $math->hexDec('ffffffff')) === 0;
98
    }
99 994
100
    /**
101
     * @return bool
102
     */
103
    public function isFinal()
104
    {
105 334
        $math = Bitcoin::getMath();
106
        return $math->cmp($this->getSequence(), self::SEQUENCE_FINAL) === 0;
107 334
    }
108
109
    /**
110
     * @return Buffer
111
     */
112
    public function getBuffer()
113
    {
114
        return (new TransactionInputSerializer(new OutPointSerializer()))->serialize($this);
115 12
    }
116
}
117