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

TransactionOutput::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Script\ScriptInterface;
6
use BitWasp\Bitcoin\Serializable;
7
use BitWasp\Bitcoin\Serializer\Transaction\TransactionOutputSerializer;
8
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
9
10
class TransactionOutput extends Serializable implements TransactionOutputInterface
11
{
12
    use FunctionAliasArrayAccess;
13
14
    /**
15
     * @var string|int
16
     */
17
    private $value;
18
19
    /**
20
     * @var ScriptInterface
21
     */
22
    private $script;
23
24
    /**
25
     * Initialize class
26
     *
27
     * @param int $value
28
     * @param ScriptInterface $script
29
     */
30 483
    public function __construct($value, ScriptInterface $script)
31
    {
32 483
        if (!is_numeric($value)) {
33
            throw new \InvalidArgumentException('TransactionInput: vout must be numeric');
34
        }
35
36 483
        $this->value = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value can also be of type double. However, the property $value 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...
37 483
        $this->script = $script;
38 483
        $this
39 483
            ->initFunctionAlias('value', 'getValue')
40 483
            ->initFunctionAlias('script', 'getScript');
41 483
    }
42
43
    /**
44
     * @return void
45
     */
46
    public function __clone()
47
    {
48
        $this->script = clone $this->script;
49
    }
50
51
    /**
52
     * @see TransactionOutputInterface::getValue()
53
     */
54 369
    public function getValue()
55
    {
56 369
        return $this->value;
57
    }
58
59
    /**
60
     * @see TransactionOutputInterface::getScript()
61
     */
62 369
    public function getScript()
63
    {
64 369
        return $this->script;
65
    }
66
67
    /**
68
     * @see \BitWasp\Bitcoin\SerializableInterface::getBuffer()
69
     */
70 297
    public function getBuffer()
71
    {
72 297
        return (new TransactionOutputSerializer())->serialize($this);
73
    }
74
}
75