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::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Letournel\PathFinder\Algorithms\ShortestDistance;
4
5
use Letournel\PathFinder\AlgorithmShortestDistance;
6
use Letournel\PathFinder\Core\Node;
7
use Letournel\PathFinder\Core\NodeGraph;
8
use Letournel\PathFinder\Core\NodeGrid;
9
use Letournel\PathFinder\Core\NodePriorityQueueMin;
10
use Letournel\PathFinder\Distance;
11
12
class Dijkstra implements AlgorithmShortestDistance
13
{
14
    /*
15
     * For more info see
16
     * http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
17
     */
18
    
19
    private
20
        $distance,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $distance.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
21
        $distanceGraph,
22
        $grid;
23
    
24
    public function __construct(Distance $distance)
25
    {
26
        $this->distance = $distance;
27
    }
28
    
29
    public function setGrid(NodeGrid $grid)
30
    {
31
        $this->grid = $grid;
32
        $this->distanceGraph = new NodeGraph($grid->buildWalkableNodesList());
33
        $this->distanceGraph->setSymetricMode(false);
34
    }
35
    
36 View Code Duplication
    public function computeLength(Node $source, Node $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        if(! $this->grid instanceof NodeGrid)
39
        {
40
            throw new \RuntimeException('Invalid Grid');
41
        }
42
        if(! $this->distanceGraph instanceof NodeGraph)
43
        {
44
            throw new \RuntimeException('Invalid Graph');
45
        }
46
        
47
        $edges = $this->distanceGraph->getEdgesFrom($source);
48
        if(empty($edges))
49
        {
50
            $this->computeDistanceGraph(array($source));
51
        }
52
        
53
        return $this->distanceGraph->getEdgeBetween($source, $target);
54
    }
55
    
56
    private function computeDistanceGraph(array $nodesList)
57
    {
58
        foreach($nodesList as $source)
59
        {
60
            $priorityQueue = new NodePriorityQueueMin();
61
            $priorityQueue->insert($source, 0);
62
            
63
            while(! $priorityQueue->isEmpty())
64
            {
65
                $node = $priorityQueue->extract();
66
                
67
                $neighbors = $this->grid->getWalkableNeighbors($node);
68
                foreach($neighbors as $neighbor)
69
                {
70
                    $alternativeDistance = $this->distanceGraph->getEdgeBetween($source, $node) + $this->distance->compute($node, $neighbor);
71
                    if(! $this->distanceGraph->existsEdgeBetween($source, $neighbor) || $alternativeDistance < $this->distanceGraph->getEdgeBetween($source, $neighbor))
72
                    {
73
                        $priorityQueue->insert($neighbor, $alternativeDistance);
74
                        $this->distanceGraph->createEdgeBetween($source, $neighbor, $alternativeDistance);
75
                    }
76
                }
77
            }
78
        }
79
    }
80
}
81