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.

NearestNeighbour::setGraph()   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\TravelingSalesman;
4
5
use Letournel\PathFinder\AlgorithmTravelingSalesman;
6
use Letournel\PathFinder\Core\NodeGraph;
7
use Letournel\PathFinder\Core\NodeMap;
8
use Letournel\PathFinder\Core\NodePath;
9
10
class NearestNeighbour implements AlgorithmTravelingSalesman
11
{
12
    /*
13
     * For more info see
14
     * http://en.wikipedia.org/wiki/Nearest_neighbour_algorithm
15
     */
16
    
17
    private
18
        $graph;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $graph.

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...
19
    
20
    public function setGraph(NodeGraph $graph)
21
    {
22
        $this->graph = $graph;
23
    }
24
    
25
    public function computeRoute()
26
    {
27
        if(! $this->graph instanceof NodeGraph)
28
        {
29
            throw new \RuntimeException('Invalid Graph');
30
        }
31
        
32
        $vertexes = $this->graph->getVertexes();
33
        if(count($vertexes) <= 2)
34
        {
35
            return new NodePath($vertexes);
36
        }
37
        
38
        $vertexFrom = array_shift($vertexes);
39
        $vertexTo = null;
40
        
41
        $pathMap = new NodeMap();
42
        $pathMap->insert($vertexFrom, $vertexTo);
0 ignored issues
show
Documentation introduced by
$vertexTo is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
        while(! empty($vertexes))
44
        {
45
            $minimalVertexTo = null;
46
            $minimalEdge = INF;
47
            $edges = $this->graph->getEdgesFrom($vertexFrom);
48
            foreach($edges as $vertexToId => $edge)
49
            {
50
                $vertexTo = $this->graph->getVertex($vertexToId);
51
                if(! $pathMap->exists($vertexTo) && $edge < $minimalEdge)
52
                {
53
                    $minimalVertexTo = $vertexTo;
54
                    $minimalEdge = $edge;
55
                }
56
            }
57
            
58
            $pathMap->insert($minimalVertexTo, $vertexFrom);
59
            unset($vertexes[$minimalVertexTo->getId()]);
60
            $vertexFrom = $minimalVertexTo;
61
        }
62
        
63
        return new NodePath($pathMap->lookupFrom($vertexFrom));
64
    }
65
}
66