Prune::getBlacklistedNodes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Vine\Commands;
4
5
use Vine\Node;
6
7
class Prune
8
{
9
    /**
10
     * Prune a node collection by a callback so only the filtered nodes are left as children.
11
     * Pruning a collection only keeps the filtered nodes and collapses the ancestor tree.
12
     * Shaking a collection retains the ancestors for each filtered node
13
     *
14
     * @param Node $node
15
     * @param callable $callback
16
     * @return Node
17
     * @internal param Node[] $nodes
18
     */
19 12 View Code Duplication
    public function __invoke(Node $node, Callable $callback): Node
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21 12
        $prunedNode = $node->isolatedCopy();
22 12
        $copiedNode = $node->copy();
23
24 12
        $prunedChildren = (new Slice())($copiedNode->children(), ...$this->getBlacklistedNodes($copiedNode, $callback));
25
26 12
        return $prunedNode->addChildren($prunedChildren);
27
    }
28
29
    /**
30
     * Blacklist of disallowed nodes
31
     * Note: the passed callback determines the nodes which should be kept but here
32
     * we reverse the callback so we get the nodes that need to be excluded
33
     *
34
     * @param $copiedNode
35
     * @param callable $callback
36
     * @return array
37
     */
38 12
    private function getBlacklistedNodes(Node $copiedNode, Callable $callback): array
39
    {
40 12
        $flatten = (new Flatten())($copiedNode->children());
41
42 12
        return array_filter($flatten->all(), function (Node $node) use ($callback)
43
        {
44 12
            return !$callback($node);
45 12
        });
46
    }
47
}
48