Prune   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 21.95 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 9
loc 41
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 9 9 1
A getBlacklistedNodes() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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