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.

Dijkstra::extractPaths()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

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