ABNode   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 34
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A add() 0 32 6
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\phptree\Node;
11
12
/**
13
 * Class ABNode.
14
 *
15
 * An auto-balanced node.
16
 */
17
class ABNode extends NaryNode
18
{
19 1
    public function add(NodeInterface ...$nodes): NodeInterface
20
    {
21 1
        foreach ($nodes as $node) {
22 1
            if (0 === $this->count()) {
23 1
                parent::add($node);
24
25 1
                continue;
26
            }
27
28 1
            $count = [];
29
30 1
            foreach ($this->children() as $child) {
31 1
                $count[$child->count()] = $child;
32
            }
33
34 1
            $keys = array_keys($count);
35 1
            $keys[] = 0;
36
37 1
            if (min($keys) === max($keys)) {
38 1
                parent::add($node);
39
40 1
                continue;
41
            }
42
43 1
            ksort($count);
44
45 1
            if (null !== $child = array_shift($count)) {
46 1
                $child->add($node);
47
            }
48
        }
49
50 1
        return $this;
51
    }
52
}
53