GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#62)
by Simone
02:21
created

Dijkstra::processQueue()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 22
rs 6.9811
1
<?php
2
3
namespace Mado\QueryBundle\Component\Meta;
4
5
/**
6
 * @since Class available since Release 2.1.0
7
 */
8
class Dijkstra
9
{
10
    private $map;
11
12
    private $distance;
13
14
    private $prev;
15
16
    private $visited;
17
18
    public function setMap($map)
19
    {
20
        foreach ($map as $nodeName => $metaData) {
21
            foreach ($metaData['relations'] as $itemKey => $itemValue) {
22
                $this->map[$nodeName][$itemValue] = 1;
23
            }
24
        }
25
    }
26
27
    private function processQueue(array $excluded)
28
    {
29
        $this->ensureMapIsDefined();
30
31
        $node = array_search(min($this->visited), $this->visited);
32
33
        if (!empty($this->map[$node]) && !in_array($node, $excluded)) {
34
            foreach ($this->map[$node] as $neighbor => $cost) {
35
                if (isset($this->distance[$neighbor])) {
36
                    if ($this->distance[$node] + $cost < $this->distance[$neighbor]) {
37
                        $this->distance[$neighbor] = $this->distance[$node] + $cost;
38
                        $this->prev[$neighbor] = [$node];
39
                        $this->visited[$neighbor] = $this->distance[$neighbor];
40
                    } elseif ($this->distance[$node] + $cost === $this->distance[$neighbor]) {
41
                        $this->prev[$neighbor][] = $node;
42
                        $this->visited[$neighbor] = $this->distance[$neighbor];
43
                    }
44
                }
45
            }
46
        }
47
48
        unset($this->visited[$node]);
49
    }
50
51
    private function extractPaths($target)
52
    {
53
        $paths = [[$target]];
54
        while (current($paths) !== false) {
55
            $key  = key($paths);
56
            $path = current($paths);
57
            next($paths);
58
            if (!empty($this->prev[$path[0]])) {
59
                foreach ($this->prev[$path[0]] as $prev) {
60
                    $copy = $path;
61
                    array_unshift($copy, $prev);
62
                    $paths[] = $copy;
63
                }
64
                unset($paths[$key]);
65
            }
66
        }
67
        return array_values($paths);
68
    }
69
70
    public function shortestPaths($source, $target, array $excluded = array())
71
    {
72
        $this->ensureMapIsDefined();
73
74
        $this->distance = array_fill_keys(array_keys($this->map), INF);
75
        $this->distance[$source] = 0;
76
        $this->prev = array_fill_keys(array_keys($this->map), []);
77
        $this->visited = [$source => 0];
78
79
        while (!empty($this->visited)) {
80
            $this->processQueue($excluded);
81
        }
82
83
        if ($source === $target) {
84
            return [[$source]];
85
        } elseif (empty($this->prev[$target])) {
86
            return [];
87
        }
88
89
        return $this->extractPaths($target);
90
    }
91
92
    public function ensureMapIsDefined()
93
    {
94
        if (!$this->map) {
95
            throw new \RuntimeException(
96
                'Oops! Map is not defined'
97
            );
98
        }
99
    }
100
}
101