Passed
Push — master ( f2e6bb...d5f140 )
by Pol
01:55
created

FulfillCapacity   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 36
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A modify() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\phptree\Modifier;
6
7
use loophp\phptree\Node\NodeInterface;
8
use loophp\phptree\Traverser\PostOrder;
9
use loophp\phptree\Traverser\TraverserInterface;
10
11
/**
12
 * Class FulfillCapacity.
13
 */
14
class FulfillCapacity implements ModifierInterface
15
{
16
    /**
17
     * @var \loophp\phptree\Traverser\PreOrder|\loophp\phptree\Traverser\TraverserInterface
18
     */
19
    private $traverser;
20
21
    /**
22
     * FulfillCapacity constructor.
23
     *
24
     * @param \loophp\phptree\Traverser\TraverserInterface|null $traverser
25
     */
26 10
    public function __construct(?TraverserInterface $traverser = null)
27
    {
28 10
        $this->traverser = $traverser ?? new PostOrder();
29 10
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 7
    public function modify(NodeInterface $tree): NodeInterface
35
    {
36
        /** @var \loophp\phptree\Node\NaryNodeInterface $item */
37 7
        foreach ($this->traverser->traverse($tree) as $item) {
38 7
            $capacity = $item->capacity();
39
40 7
            if (false === $item->isLeaf()) {
41 7
                $children = iterator_to_array($item->children());
42
43 7
                while ($item->degree() !== $capacity) {
44 5
                    $item->add(current($children)->clone());
45
                }
46
            }
47
        }
48
49 7
        return $tree;
50
    }
51
}
52