Pluck   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 30
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 20 6
1
<?php
2
3
namespace Vine\Queries;
4
5
use Vine\Node;
6
7
class Pluck
8
{
9
    /**
10
     * @param Node $node
11
     * @param $key
12
     * @param null $value
13
     * @param bool $down | down: pluck from children, up: pluck from ancestors
14
     * @return array
15
     */
16 12
    public function __invoke(Node $node, $key, $value = null, $down = true): array
17
    {
18 12
        $values = $value
19 6
            ? [$node->entry($key) => $node->entry($value)]
20 12
            : [$node->entry($key)];
21
22 12
        $nodes = $down ? $node->children() : [$node->parent()];
23
24 12
        foreach($nodes as $node)
25
        {
26
            // If node entry is empty, which means there is no parent, we bail out
27 12
            if(!$node) break;
28
29 12
            $values = $value
30 6
                ? array_replace($values, $this->__invoke($node, $key, $value, $down)) // Respect the passed key values
31 12
                : array_merge($values, $this->__invoke($node, $key, $value, $down)); // No key values specified so just append
32
        }
33
34 12
        return $values;
35
    }
36
}
37