Completed
Push — master ( 0c9ae2...053212 )
by thomas
30:33 queued 10:39
created

TransactionInput::isCoinbase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
crap 2
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 1131
    public function __construct(OutPointInterface $outPoint, ScriptInterface $script, $sequence = self::SEQUENCE_FINAL)
39
    {
40 1131
        if (!is_numeric($sequence)) {
41 6
            throw new \InvalidArgumentException('TransactionInput: sequence must be numeric');
42
        }
43
44 1125
        $this->outPoint = $outPoint;
45 1125
        $this->script = $script;
46 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...
47
48 1125
        $this
49 1125
            ->initFunctionAlias('outpoint', 'getOutPoint')
50 1125
            ->initFunctionAlias('script', 'getScript')
51 1125
            ->initFunctionAlias('sequence', 'getSequence');
52
53 1125
    }
54
55
    /**
56
     * @return TransactionInput
57
     */
58
    public function __clone()
59
    {
60
        $this->script = clone $this->script;
61
    }
62
63
    /**
64
     * @return OutPointInterface
65
     */
66 345
    public function getOutPoint()
67
    {
68 345
        return $this->outPoint;
69
    }
70
71
    /**
72
     * @return Script
73
     */
74 993
    public function getScript()
75
    {
76 993
        return $this->script;
77
    }
78
79
    /**
80
     * @return int
81
     */
82 333
    public function getSequence()
83
    {
84 333
        return $this->sequence;
85
    }
86
87
    /**
88
     * Check whether this transaction is a Coinbase transaction
89
     *
90
     * @return boolean
91
     */
92 12
    public function isCoinbase()
93
    {
94 12
        $math = Bitcoin::getMath();
95 12
        $outpoint = $this->outPoint;
96 12
        return $outpoint->getTxId()->getBinary() === str_pad('', 32, "\x00")
97 12
            && $math->cmp($outpoint->getVout(), $math->hexDec('ffffffff')) === 0;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103 6
    public function isFinal()
104
    {
105 6
        $math = Bitcoin::getMath();
106 6
        return $math->cmp($this->getSequence(), self::SEQUENCE_FINAL) === 0;
107
    }
108
109
    /**
110
     * @return Buffer
111
     */
112 291
    public function getBuffer()
113
    {
114 291
        return (new TransactionInputSerializer(new OutPointSerializer()))->serialize($this);
115
    }
116
}
117