RemoveNullNode::modify()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
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