Completed
Pull Request — master (#492)
by thomas
106:30 queued 33:27
created

LogicOpNode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Path;
4
5
class LogicOpNode
6
{
7
8
    /**
9
     * @var LogicOpNode|null
10
     */
11
    private $parent;
12
13
    /**
14
     * @var bool
15
     */
16
    private $value;
17
18
    /**
19
     * @var LogicOpNode[]
20
     */
21
    private $children = [];
22
23
    /**
24
     * MASTNode constructor.
25
     * @param self|null $parent
26
     * @param bool|null $value
27
     */
28
    public function __construct(self $parent = null, $value = null)
29
    {
30
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent can also be of type object<self>. However, the property $parent is declared as type object<BitWasp\Bitcoin\S...\Path\LogicOpNode>|null. 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...
31
        $this->value = $value;
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function flags()
38
    {
39
        if (count($this->children) > 0) {
40
            $values = [];
41
            foreach ($this->children as $k => $child) {
42
                $flags = $child->flags();
43
                foreach ($flags as $branch) {
44
                    $values[] = array_merge($this->isRoot() ? [] : [$this->value], is_array($branch) ? $branch : [$branch]);
45
                }
46
            }
47
48
            return $values;
49
        } else {
50
            $value = $this->value;
51
            return [$value];
52
        }
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    public function isRoot()
59
    {
60
        return $this->parent == null;
61
    }
62
63
    /**
64
     * @return LogicOpNode|null
65
     */
66
    public function getParent()
67
    {
68
        return $this->parent;
69
    }
70
71
    /**
72
     * @return bool|null
73
     */
74
    public function getValue()
75
    {
76
        return $this->value;
77
    }
78
79
    /**
80
     * @param $value
81
     * @return LogicOpNode
82
     */
83
    public function getChild($value)
84
    {
85
        if (!isset($this->children[$value])) {
86
            throw new \RuntimeException("Child not found");
87
        }
88
        return $this->children[$value];
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function split()
95
    {
96
        if (count($this->children) > 0) {
97
            throw new \RuntimeException("Sanity check - dont split twice");
98
        }
99
100
        $children = [new LogicOpNode($this, false), new LogicOpNode($this, true)];
0 ignored issues
show
Documentation introduced by
$this is of type this<BitWasp\Bitcoin\Script\Path\LogicOpNode>, but the function expects a null|object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
        foreach ($children as $child) {
102
            $this->children[] = $child;
103
        }
104
        return $children;
105
    }
106
}
107