Completed
Push — master ( 838d3a...3b658f )
by thomas
22:41 queued 01:10
created

TransactionInput::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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|Buffer $script
41
     * @param int $sequence
42
     */
43 1137
    public function __construct($hashPrevOut, $nPrevOut, ScriptInterface $script = null, $sequence = self::SEQUENCE_FINAL)
44
    {
45 1137
        if (!is_numeric($nPrevOut)) {
46 6
            throw new \InvalidArgumentException('TransactionInput: vout must be numeric');
47
        }
48
49 1131
        if (!is_numeric($sequence)) {
50 6
            throw new \InvalidArgumentException('TransactionInput: sequence must be numeric');
51
        }
52
53 1125
        $this->hashPrevOut = $hashPrevOut;
54 1125
        $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...
55 1125
        $this->script = $script ?: new Script();
56 1125
        $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...
57 1125
        $this
58 1125
            ->initFunctionAlias('txid', 'getTransactionId')
59 1125
            ->initFunctionAlias('vout', 'getVout')
60 1125
            ->initFunctionAlias('script', 'getScript')
61 1125
            ->initFunctionAlias('sequence', 'getSequence');
62 1125
    }
63
64
    /**
65
     * @return TransactionInput
66
     */
67
    public function __clone()
68
    {
69
        $this->script = clone $this->script;
70
    }
71
72
    /**
73
     * Return the transaction ID buffer
74
     *
75
     * @return string
76
     */
77 339
    public function getTransactionId()
78
    {
79 339
        return $this->hashPrevOut;
80
    }
81
82
    /**
83
     * @return int
84
     */
85 351
    public function getVout()
86
    {
87 351
        return $this->nPrevOut;
88
    }
89
90
    /**
91
     * @return Script
92
     */
93 993
    public function getScript()
94
    {
95 993
        return $this->script;
96
    }
97
98
    /**
99
     * @return int
100
     */
101 333
    public function getSequence()
102
    {
103 333
        return $this->sequence;
104
    }
105
106
    /**
107
     * Check whether this transaction is a Coinbase transaction
108
     *
109
     * @return boolean
110
     */
111 12
    public function isCoinbase()
112
    {
113 12
        $math = Bitcoin::getMath();
114 12
        return $this->getTransactionId() === '0000000000000000000000000000000000000000000000000000000000000000'
115 12
            && $math->cmp($this->getVout(), $math->hexDec('ffffffff')) === 0;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121 6
    public function isFinal()
122
    {
123 6
        $math = Bitcoin::getMath();
124 6
        return $math->cmp($this->getSequence(), self::SEQUENCE_FINAL) === 0;
125
    }
126
127
    /**
128
     * @return Buffer
129
     */
130 291
    public function getBuffer()
131
    {
132 291
        return (new TransactionInputSerializer())->serialize($this);
133
    }
134
}
135