Completed
Push — master ( 40a4e1...e12674 )
by thomas
37:14 queued 34:53
created

LogicOpNode   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 112
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B flags() 0 20 7
A isRoot() 0 4 1
A hasChildren() 0 4 1
A getParent() 0 4 1
A getValue() 0 4 1
A getChild() 0 7 2
A split() 0 12 3
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 230
    public function __construct(self $parent = null, $value = null)
28
    {
29 230
        $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 230
        $this->value = $value;
31 230
    }
32
33
    /**
34
     * @return array
35
     */
36 212
    public function flags()
37
    {
38 212
        if (count($this->children) > 0) {
39 70
            $values = [];
40 70
            foreach ($this->children as $k => $child) {
41 70
                $flags = $child->flags();
42 70
                foreach ($flags as $branch) {
43 70
                    $values[] = array_merge($this->isRoot() ? [] : [$this->value], is_array($branch) ? $branch : [$branch]);
44
                }
45
            }
46
47 70
            return $values;
48
        } else {
49 212
            $value = $this->value;
50 212
            if ($value === null) {
51 142
                return [[]];
52
            }
53 70
            return [$value];
54
        }
55
    }
56
57
    /**
58
     * @return bool
59
     */
60 222
    public function isRoot()
61
    {
62 222
        return $this->parent == null;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68 178
    public function hasChildren()
69
    {
70 178
        return count($this->children) > 0;
71
    }
72
73
    /**
74
     * @return LogicOpNode|null
75
     */
76 74
    public function getParent()
77
    {
78 74
        return $this->parent;
79
    }
80
81
    /**
82
     * @return bool|null
83
     */
84 66
    public function getValue()
85
    {
86 66
        return $this->value;
87
    }
88
89
    /**
90
     * @param $value
91
     * @return LogicOpNode
92
     */
93 68
    public function getChild($value)
94
    {
95 68
        if (!isset($this->children[$value])) {
96 2
            throw new \RuntimeException("Child not found");
97
        }
98 66
        return $this->children[$value];
99
    }
100
101
    /**
102
     * @return array
103
     */
104 76
    public function split()
105
    {
106 76
        if (count($this->children) > 0) {
107 2
            throw new \RuntimeException("Sanity check - don't split twice");
108
        }
109
110 76
        $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...
111 76
        foreach ($children as $child) {
112 76
            $this->children[] = $child;
113
        }
114 76
        return $children;
115
    }
116
}
117