Ancestors::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 2
nop 2
crap 4
1
<?php
2
3
namespace Vine\Queries;
4
5
use Vine\Node;
6
use Vine\NodeCollection;
7
8
class Ancestors
9
{
10
    /**
11
     * @param Node $node
12
     * @param null $depth
13
     * @return NodeCollection
14
     */
15 15
    public function __invoke(Node $node, $depth = null): NodeCollection
16
    {
17 15
        $ancestors = new NodeCollection;
18 15
        $currentDepth = 0;
19
20 15
        while($parent = $node->parent())
21
        {
22 15
            if(!is_null($depth) && $currentDepth >= $depth) break;
23 15
            $currentDepth++;
24
25 15
            $node = $parent;
26 15
            $ancestors->add($parent);
27
        }
28
29 15
        return new NodeCollection(...array_reverse($ancestors->all()));
30
    }
31
}
32