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

Filter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 37
ccs 12
cts 12
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A modify() 0 17 4
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