Pluck::__invoke()   B
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 16
nop 4
crap 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