FulfillCapacity   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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