Completed
Push — master ( c7419f...b9ebc8 )
by thomas
35:04 queued 31:19
created

TransactionInput::isFinal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Buffertools\Buffer;
7
use BitWasp\Bitcoin\Script\Script;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Bitcoin\Serializable;
10
use BitWasp\Bitcoin\Serializer\Transaction\TransactionInputSerializer;
11
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
12
13
class TransactionInput extends Serializable implements TransactionInputInterface
14
{
15
    use FunctionAliasArrayAccess;
16
17
    /**
18
     * @var string
19
     */
20
    private $hashPrevOut;
21
22
    /**
23
     * @var string|int
24
     */
25
    private $nPrevOut;
26
27
    /**
28
     * @var ScriptInterface
29
     */
30
    private $script;
31
32
    /**
33
     * @var string|int
34
     */
35
    private $sequence;
36
37
    /**
38
     * @param string $hashPrevOut
39
     * @param string $nPrevOut
40
     * @param ScriptInterface $script
41
     * @param int $sequence
42
     */
43 1137
    public function __construct($hashPrevOut, $nPrevOut, ScriptInterface $script, $sequence = self::SEQUENCE_FINAL)
44
    {
45 1137
        if (!is_string($hashPrevOut) || strlen($hashPrevOut) !== 64) {
46 24
            throw new \InvalidArgumentException('TransactionInput: hash must be a hex string');
47
        }
48
49 1113
        if (!is_numeric($nPrevOut)) {
50
            throw new \InvalidArgumentException('TransactionInput: vout must be numeric');
51
        }
52
53 1113
        if (!is_numeric($sequence)) {
54
            throw new \InvalidArgumentException('TransactionInput: sequence must be numeric');
55
        }
56
57 1113
        $this->hashPrevOut = $hashPrevOut;
58 1113
        $this->nPrevOut = $nPrevOut;
0 ignored issues
show
Documentation Bug introduced by
It seems like $nPrevOut can also be of type double. However, the property $nPrevOut 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...
59 1113
        $this->script = $script;
60 1113
        $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...
61 1113
        $this
62 1113
            ->initFunctionAlias('txid', 'getTransactionId')
63 1113
            ->initFunctionAlias('vout', 'getVout')
64 1113
            ->initFunctionAlias('script', 'getScript')
65 1113
            ->initFunctionAlias('sequence', 'getSequence');
66 1113
    }
67
68
    /**
69
     * @return TransactionInput
70
     */
71
    public function __clone()
72
    {
73
        $this->script = clone $this->script;
74
    }
75
76
    /**
77
     * Return the transaction ID buffer
78
     *
79
     * @return string
80
     */
81 339
    public function getTransactionId()
82
    {
83 339
        return $this->hashPrevOut;
84
    }
85
86
    /**
87
     * @return int
88
     */
89 351
    public function getVout()
90
    {
91 351
        return $this->nPrevOut;
92
    }
93
94
    /**
95
     * @return Script
96
     */
97 993
    public function getScript()
98
    {
99 993
        return $this->script;
100
    }
101
102
    /**
103
     * @return int
104
     */
105 333
    public function getSequence()
106
    {
107 333
        return $this->sequence;
108
    }
109
110
    /**
111
     * Check whether this transaction is a Coinbase transaction
112
     *
113
     * @return boolean
114
     */
115 12
    public function isCoinbase()
116
    {
117 12
        $math = Bitcoin::getMath();
118 12
        return $this->getTransactionId() === '0000000000000000000000000000000000000000000000000000000000000000'
119 12
            && $math->cmp($this->getVout(), $math->hexDec('ffffffff')) === 0;
120
    }
121
122
    /**
123
     * @return bool
124
     */
125 6
    public function isFinal()
126
    {
127 6
        $math = Bitcoin::getMath();
128 6
        return $math->cmp($this->getSequence(), self::SEQUENCE_FINAL) === 0;
129
    }
130
131
    /**
132
     * @return Buffer
133
     */
134 291
    public function getBuffer()
135
    {
136 291
        return (new TransactionInputSerializer())->serialize($this);
137
    }
138
}
139