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.

FloydWarshall::computeDistanceGraph()   C
last analyzed

Complexity

Conditions 11
Paths 12

Size

Total Lines 47
Code Lines 24

Duplication

Lines 26
Ratio 55.32 %

Importance

Changes 0
Metric Value
dl 26
loc 47
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 24
nc 12
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Distance;
10
11
class FloydWarshall implements AlgorithmShortestDistance
12
{
13
    /*
14
     * For more info see
15
     * http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
16
     */
17
    
18
    private
19
        $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...
20
        $distanceGraph,
21
        $grid;
22
    
23
    public function __construct(Distance $distance)
24
    {
25
        $this->distance = $distance;
26
    }
27
    
28
    public function setGrid(NodeGrid $grid)
29
    {
30
        $this->grid = $grid;
31
        $this->distanceGraph = new NodeGraph($grid->buildWalkableNodesList());
32
    }
33
    
34 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...
35
    {
36
        if(! $this->grid instanceof NodeGrid)
37
        {
38
            throw new \RuntimeException('Invalid Grid');
39
        }
40
        if(! $this->distanceGraph instanceof NodeGraph)
41
        {
42
            throw new \RuntimeException('Invalid Graph');
43
        }
44
        
45
        $edges = $this->distanceGraph->getEdgesFrom($source);
46
        if(empty($edges))
47
        {
48
            $this->computeDistanceGraph();
49
        }
50
        
51
        return $this->distanceGraph->getEdgeBetween($source, $target);
52
    }
53
    
54
    private function computeDistanceGraph()
55
    {
56
        $nbNodes = $this->grid->getNodesNb();
57 View Code Duplication
        for($i = 0; $i < $nbNodes; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
        {
59
            $iNode = $this->grid->getNodeNumber($i);
60
            $neighbors = $this->grid->getWalkableNeighbors($iNode);
61
            foreach($neighbors as $neighbor)
62
            {
63
                $alternativeDistance = $this->distance->compute($iNode, $neighbor);
64
                if($alternativeDistance < $this->distanceGraph->getEdgeBetween($iNode, $neighbor))
65
                {
66
                    $this->distanceGraph->createEdgeBetween($iNode, $neighbor, $alternativeDistance);
67
                }
68
            }
69
        }
70
        
71
        for($k = 0; $k < $nbNodes; $k++)
72
        {
73
            $kNode = $this->grid->getNodeNumber($k);
74
            if(! $kNode->isWalkable())
75
            {
76
                continue;
77
            }
78
            for($i = 0; $i < $nbNodes; $i++)
79
            {
80
                $iNode = $this->grid->getNodeNumber($i);
81
                if(! $iNode->isWalkable())
82
                {
83
                    continue;
84
                }
85 View Code Duplication
                for($j = 0; $j < $nbNodes; $j++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86
                {
87
                    $jNode = $this->grid->getNodeNumber($j);
88
                    if(! $jNode->isWalkable())
89
                    {
90
                        continue;
91
                    }
92
                    $alternativeDistance = $this->distanceGraph->getEdgeBetween($iNode, $kNode) + $this->distanceGraph->getEdgeBetween($kNode, $jNode);
93
                    if($alternativeDistance < $this->distanceGraph->getEdgeBetween($iNode, $jNode))
94
                    {
95
                        $this->distanceGraph->createEdgeBetween($iNode, $jNode, $alternativeDistance);
96
                    }
97
                }
98
            }
99
        }
100
    }
101
}
102