Passed
Push — master ( bbf76f...35a0db )
by Ivan
01:53
created

BasicNavigation::cleanAncestors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Everlution\Navigation\Navigation;
6
7
use Everlution\Navigation\Matcher\Matcher;
8
use Everlution\Navigation\RootNavigationItem;
9
use Everlution\Navigation\NavigationItem;
10
11
/**
12
 * Class BasicNavigation.
13
 * @author Ivan Barlog <[email protected]>
14
 */
15
class BasicNavigation implements Navigation
16
{
17
    /** @var NavigationItem[] */
18
    private $ancestors = [];
19
    /** @var NavigationItem */
20
    private $current = null;
21
22
    public function __construct(RootNavigationItem $root, Matcher $matcher)
23
    {
24
        $this->process($root, $matcher);
25
    }
26
27
    /**
28
     * @param NavigationItem $item
29
     * @return bool
30
     */
31
    public function isAncestor(NavigationItem $item): bool
32
    {
33
        return in_array($item, $this->ancestors);
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getBreadcrumbs(): array
40
    {
41
        return $this->ancestors;
42
    }
43
44
    /**
45
     * @return NavigationItem
46
     * @throws CurrentItemNotMatchedException
47
     */
48
    public function getCurrent(): NavigationItem
49
    {
50
        if (!$this->current instanceof NavigationItem) {
51
            throw new CurrentItemNotMatchedException();
52
        }
53
54
        return $this->current;
55
    }
56
57
    /**
58
     * @param NavigationItem $item
59
     * @param Matcher $matcher
60
     * @param int $depth
61
     * @return NavigationItem|null
62
     */
63
    private function process(NavigationItem &$item, Matcher &$matcher, int $depth = 0)
64
    {
65
        $this->ancestors[$depth] = $item;
66
67
        if ($matcher->isCurrent($item)) {
68
            $this->current = $item;
69
            $this->cleanAncestors();
70
71
            return $item;
72
        }
73
74
        $childDepth = ++$depth;
75
        /** @var NavigationItem $child */
76
        foreach ($item->getChildren() as $child) {
77
            $result = $this->process($child, $matcher, $childDepth);
78
79
            if ($result instanceof NavigationItem) {
80
                return $result;
81
            }
82
        }
83
84
        unset($this->ancestors[$depth]);
85
86
        return null;
87
    }
88
89
    private function cleanAncestors()
90
    {
91
        array_shift($this->ancestors);
92
        array_pop($this->ancestors);
93
        $this->ancestors = array_values($this->ancestors);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($this->ancestors) of type array<integer,?> is incompatible with the declared type array<integer,object<Eve...gation\NavigationItem>> of property $ancestors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94
    }
95
}
96