AbstractBranch   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
lcom 2
cbo 1
dl 0
loc 222
rs 10
c 1
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setParent() 0 5 1
A hasParent() 0 7 2
A getParent() 0 7 2
A getRootParent() 0 11 3
A addChild() 0 4 1
A hasChildren() 0 7 2
A getChildren() 0 7 2
A setDepth() 0 5 1
A getDepth() 0 4 1
A setLeft() 0 5 1
A getLeft() 0 4 1
A setRight() 0 5 1
A getRight() 0 4 1
A setIsLeaf() 0 5 1
A isLeaf() 0 4 1
A getAsArray() 0 12 2
1
<?php
2
namespace ezTreeBuilder\Base;
3
4
/**
5
 * Branch abstract class - Limited concern to \TreeBuilder\Tree
6
 *
7
 * @package ezTreeBuilder
8
 * @author Adrian Tilita <[email protected]>
9
 * @version 1.0
10
 * @abstract
11
 */
12
abstract class AbstractBranch
13
{
14
    /**
15
     * Container for current branch children
16
     * @var array
17
     */
18
    private $children = array();
19
20
    /**
21
     * Reference to the current branch parent
22
     * @var Branch
23
     */
24
    private $parent;
25
26
    /**
27
     * The depth of the item
28
     * @var int
29
     */
30
    private $depth;
31
32
    /**
33
     * The left number of the branch
34
     * @var int
35
     */
36
    private $left;
37
38
    /**
39
     * The right number of the branch
40
     * @var int
41
     */
42
    private $right;
43
44
    /**
45
     * Establish if the item is a leaf
46
     * @var boolean
47
     */
48
    private $isLeaf;
49
50
    /**
51
     * Set the Parent of the current branch
52
     * @param Branch $item
53
     * @return Branch
54
     */
55
    public function setParent(Branch $item)
56
    {
57
        $this->parent = $item;
58
        return $this;
59
    }
60
61
    /**
62
     * Verify if the current branch has a parent
63
     * @return boolean
64
     */
65
    public function hasParent()
66
    {
67
        if (!isset($this->parent)) {
68
            return false;
69
        }
70
        return true;
71
    }
72
73
    /**
74
     * Get the current branch's parent
75
     * @return Branch
76
     * @throws \Exception
77
     */
78
    public function getParent()
79
    {
80
        if ($this->hasParent() === false) {
81
            throw new \Exception('The current branch has no parent set!');
82
        }
83
        return $this->parent;
84
    }
85
86
    /**
87
     * Get the root parent of the current branch
88
     * @return Branch
89
     */
90
    public function getRootParent()
91
    {
92
        if ($this->hasParent() === false) {
93
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (ezTreeBuilder\Base\AbstractBranch) is incompatible with the return type documented by ezTreeBuilder\Base\AbstractBranch::getRootParent of type ezTreeBuilder\Base\Branch.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
94
        }
95
        $parent = $this->getParent();
96
        while ($parent->hasParent() === true) {
97
            $parent = $parent->getParent();
98
        }
99
        return $parent;
100
    }
101
102
    /**
103
     * Adds a new branch child
104
     * @param Branch $item
105
     */
106
    public function addChild(Branch $item)
107
    {
108
        $this->children[] = $item;
109
    }
110
 
111
    /**
112
     * Verify if the current branch has any children
113
     * @return boolean
114
     */
115
    public function hasChildren()
116
    {
117
        if (empty($this->children)) {
118
            return false;
119
        }
120
        return true;
121
    }
122
123
    /**
124
     * Return the current branch children
125
     * @return array - array(Branch, Branch)
126
     * @throws \Exception
127
     */
128
    public function getChildren()
129
    {
130
        if ($this->hasChildren() === false) {
131
            throw new \Exception('The current branch has no childs!');
132
        }
133
        return $this->children;
134
    }
135
136
    /**
137
     * Set the branch depth
138
     * @param int $depth
139
     * @return AbstractBranch
140
     */
141
    public function setDepth($depth)
142
    {
143
        $this->depth = $depth;
144
        return $this;
145
    }
146
147
    /**
148
     * Get the branch depth
149
     * @return int
150
     */
151
    public function getDepth()
152
    {
153
        return $this->depth;
154
    }
155
156
    /**
157
     * Set Tree Left value
158
     * @param int $value
159
     * @return AbstractBranch
160
     */
161
    public function setLeft($value)
162
    {
163
        $this->left = $value;
164
        return $this;
165
    }
166
167
    /**
168
     * Get Tree Left value
169
     * @return int
170
     */
171
    public function getLeft()
172
    {
173
        return $this->left;
174
    }
175
176
177
    /**
178
     * Set Tree Right value
179
     * @param int $value
180
     * @return AbstractBranch
181
     */
182
    public function setRight($value)
183
    {
184
        $this->right = $value;
185
        return $this;
186
    }
187
188
    /**
189
     * Get Tree Right value
190
     * @return int
191
     */
192
    public function getRight()
193
    {
194
        return $this->right;
195
    }
196
197
    /**
198
     * Set the Leaf state
199
     * @param boolean $isLeaf
200
     * @return AbstractBranch
201
     */
202
    public function setIsLeaf($isLeaf)
203
    {
204
        $this->isLeaf = $isLeaf;
205
        return $this;
206
    }
207
208
    /**
209
     * Return the leaf state of the item
210
     * @return boolean
211
     */
212
    public function isLeaf()
213
    {
214
        return $this->isLeaf;
215
    }
216
217
    /**
218
     * Get the entire Item as array
219
     * @return array
220
     */
221
    public function getAsArray()
222
    {
223
        $returnedArray = array();
224
        $returnedArray['id'] = $this->getId();
225
        $returnedArray['parent_id'] = $this->hasParent() ? $this->getParentId() : 0;
0 ignored issues
show
Bug introduced by
The method getParentId() does not exist on ezTreeBuilder\Base\AbstractBranch. Did you maybe mean getParent()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
226
        $returnedArray['data'] = $this->getData();
227
        $returnedArray['depth'] = $this->getDepth();
228
        $returnedArray['left'] = $this->getLeft();
229
        $returnedArray['right'] = $this->getRight();
230
        $returnedArray['is_leaf'] = $this->isLeaf();
231
        return $returnedArray;
232
    }
233
}
234