Passed
Push — master ( 28c5ec...7a4f55 )
by Pol
04:19
created

Filter::modify()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\phptree\Modifier;
6
7
use loophp\phptree\Node\NodeInterface;
8
9
/**
10
 * Class Filter.
11
 */
12
class Filter implements ModifierInterface
13
{
14
    /**
15
     * @var callable
16
     */
17
    private $filter;
18
19
    /**
20
     * Filter constructor.
21
     *
22
     * @param callable $filter
23
     */
24 2
    public function __construct(callable $filter)
25
    {
26 2
        $this->filter = $filter;
27 2
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function modify(NodeInterface $tree): NodeInterface
33
    {
34
        /** @var \loophp\phptree\Node\MerkleNode $item */
35 1
        foreach ($tree->all() as $item) {
36 1
            if (null === $parent = $item->getParent()) {
37 1
                continue;
38
            }
39
40 1
            if (false === (bool) ($this->filter)($item)) {
41 1
                continue;
42
            }
43
44 1
            $parent->remove($item);
45 1
            $this->modify($parent);
46
        }
47
48 1
        return $tree;
49
    }
50
}
51