Test Setup Failed
Pull Request — master (#375)
by Arkadiusz
02:14
created

BinaryNode   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 75
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A parent() 0 4 1
A left() 0 4 1
A right() 0 4 1
A height() 0 4 3
A balance() 0 4 3
A setParent() 0 4 1
A attachLeft() 0 5 1
A detachLeft() 0 7 2
A attachRight() 0 5 1
A detachRight() 0 7 2
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
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
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