Completed
Push — master ( 410ea8...0c9ae2 )
by thomas
105:23 queued 33:17
created

TransactionInput   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 82.76%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 10
c 5
b 0
f 1
lcom 2
cbo 8
dl 0
loc 103
ccs 24
cts 29
cp 0.8276
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A __clone() 0 4 1
A getOutPoint() 0 4 1
A getScript() 0 4 1
A getSequence() 0 4 1
A isCoinbase() 0 7 2
A isFinal() 0 5 1
A getBuffer() 0 4 1
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