ABNode::add()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 16
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 32
ccs 17
cts 17
cp 1
crap 6
rs 9.1111
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