Test Setup Failed
Push — master ( 8544cf...91812f )
by Arkadiusz
02:16
created

BinaryNode::setParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\Tree\Node;
6
7
use Phpml\Tree\Node;
8
9
class BinaryNode implements Node
10
{
11
    /**
12
     * @var self|null
13
     */
14
    private $parent;
15
16
    /**
17
     * @var self|null
18
     */
19
    private $left;
20
21
    /**
22
     * @var self|null
23
     */
24
    private $right;
25
26
    public function parent(): ?self
27
    {
28
        return $this->parent;
29
    }
30
31
    public function left(): ?self
32
    {
33
        return $this->left;
34
    }
35
36
    public function right(): ?self
37
    {
38
        return $this->right;
39
    }
40
41
    public function height(): int
42
    {
43
        return 1 + max($this->left !== null ? $this->left->height() : 0, $this->right !== null ? $this->right->height() : 0);
44
    }
45
46
    public function balance(): int
47
    {
48
        return ($this->right !== null ? $this->right->height() : 0) - ($this->left !== null ? $this->left->height() : 0);
49
    }
50
51
    public function setParent(?self $node = null): void
52
    {
53
        $this->parent = $node;
54
    }
55
56
    public function attachLeft(self $node): void
57
    {
58
        $node->setParent($this);
59
        $this->left = $node;
0 ignored issues
show
Documentation Bug introduced by Arkadiusz Kondas
It seems like $node of type object<self> is incompatible with the declared type object<Phpml\Tree\Node\BinaryNode>|null of property $left.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
    }
61
62
    public function detachLeft(): void
63
    {
64
        if ($this->left !== null) {
65
            $this->left->setParent();
66
            $this->left = null;
67
        }
68
    }
69
70
    public function attachRight(self $node): void
71
    {
72
        $node->setParent($this);
73
        $this->right = $node;
0 ignored issues
show
Documentation Bug introduced by Arkadiusz Kondas
It seems like $node of type object<self> is incompatible with the declared type object<Phpml\Tree\Node\BinaryNode>|null of property $right.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
    }
75
76
    public function detachRight(): void
77
    {
78
        if ($this->right !== null) {
79
            $this->right->setParent();
80
            $this->right = null;
81
        }
82
    }
83
}
84