Completed
Pull Request — master (#524)
by thomas
71:45
created

LogicOpNode::getParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Path;
4
5
use BitWasp\Bitcoin\Script\Parser\Operation;
6
7
class LogicOpNode
8
{
9
10
    /**
11
     * @var LogicOpNode|null
12
     */
13
    private $parent;
14
15
    /**
16
     * @var bool
17
     */
18
    private $value;
19
20
    /**
21
     * @var LogicOpNode[]
22
     */
23
    private $children = [];
24
25
    /**
26
     * @var Operation[]
27
     */
28
    private $ops = [];
29
30
    /**
31
     * MASTNode constructor.
32
     * @param self|null $parent
33
     * @param bool|null $value
34
     */
35
    public function __construct(self $parent = null, $value = null)
36
    {
37
        $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...
38
        $this->value = $value;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function flags()
45
    {
46
        if (count($this->children) > 0) {
47
            $values = [];
48
            foreach ($this->children as $k => $child) {
49
                $flags = $child->flags();
50
                foreach ($flags as $branch) {
51
                    $values[] = array_merge($this->isRoot() ? [] : [$this->value], is_array($branch) ? $branch : [$branch]);
52
                }
53
            }
54
55
            return $values;
56
        } else {
57
            $value = $this->value;
58
            return [$value];
59
        }
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isRoot()
66
    {
67
        return $this->parent == null;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function hasChildren()
74
    {
75
        return count($this->children) > 0;
76
    }
77
78
    /**
79
     * @return LogicOpNode|null
80
     */
81
    public function getParent()
82
    {
83
        return $this->parent;
84
    }
85
86
    /**
87
     * @return bool|null
88
     */
89
    public function getValue()
90
    {
91
        return $this->value;
92
    }
93
94
    /**
95
     * @param Operation $op
96
     */
97
    public function assign(Operation $op)
98
    {
99
        $this->ops[] = $op;
100
    }
101
102
    /**
103
     * @param $value
104
     * @return LogicOpNode
105
     */
106
    public function getChild($value)
107
    {
108
        if (!isset($this->children[$value])) {
109
            throw new \RuntimeException("Child not found");
110
        }
111
        return $this->children[$value];
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function split()
118
    {
119
        if (count($this->children) > 0) {
120
            throw new \RuntimeException("Sanity check - dont split twice");
121
        }
122
123
        $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...
124
        foreach ($children as $child) {
125
            $this->children[] = $child;
126
        }
127
        return $children;
128
    }
129
}
130