Completed
Pull Request — master (#526)
by thomas
30:16
created

LogicOpNode::getParent()   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

Importance

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