RemoveNullNode   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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