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

RemoveNullNode   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 41
ccs 14
cts 14
cp 1
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A modify() 0 21 5
A __construct() 0 3 1
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 RemoveNullNode.
13
 */
14
class RemoveNullNode implements ModifierInterface
15
{
16
    /**
17
     * @var \loophp\phptree\Traverser\PreOrder|\loophp\phptree\Traverser\TraverserInterface
18
     */
19
    private $traverser;
20
21
    /**
22
     * RemoveNullNode 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\ValueNodeInterface $item */
37 7
        foreach ($this->traverser->traverse($tree) as $item) {
38 7
            if (null === $parent = $item->getParent()) {
39 7
                continue;
40
            }
41
42 7
            if (false === $item->isLeaf()) {
43 4
                continue;
44
            }
45
46 7
            if (null !== $item->getValue()) {
47 7
                continue;
48
            }
49
50 2
            $parent->remove($item);
51 2
            $this->modify($parent);
52
        }
53
54 7
        return $tree;
55
    }
56
}
57