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.

TwoOpt::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\Node;
7
use Letournel\PathFinder\Core\NodeGraph;
8
use Letournel\PathFinder\Core\NodePath;
9
10
class TwoOpt implements AlgorithmTravelingSalesman
11
{
12
    /*
13
     * For more info see
14
     * http://en.wikipedia.org/wiki/2-opt
15
     */
16
    
17
    private
18
        $existingRoute,
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 $existingRoute.

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
        $graph;
20
    
21
    public function setExistingRoute(NodePath $existingRoute)
22
    {
23
        $this->existingRoute = $existingRoute;
24
    }
25
    
26
    public function setGraph(NodeGraph $graph)
27
    {
28
        $this->graph = $graph;
29
    }
30
    
31
    public function computeRoute()
32
    {
33
        if(! $this->graph instanceof NodeGraph)
34
        {
35
            throw new \RuntimeException('Invalid Graph');
36
        }
37
        if(! $this->existingRoute instanceof NodePath)
38
        {
39
            throw new \RuntimeException('Invalid ExistingRoute');
40
        }
41
        if(count($this->existingRoute) < 2)
42
        {
43
            return $this->existingRoute;
44
        }
45
        
46
        $existingDistance = $this->graph->computeLength($this->existingRoute);
47
        
48
        do
49
        {
50
            $improvementMade = false;
51
            foreach($this->existingRoute->getKeys() as $iKey)
52
            {
53
                foreach($this->existingRoute->getKeys() as $kKey)
54
                {
55
                    if($iKey == $kKey)
56
                    {
57
                        continue;
58
                    }
59
                    $newRoute = $this->twoOptSwap($iKey, $kKey);
60
                    $newDistance = $this->graph->computeLength($newRoute);
61 View Code Duplication
                    if($newDistance < $existingDistance)
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...
62
                    {
63
                        $this->existingRoute = $newRoute;
64
                        $existingDistance = $newDistance;
65
                        $improvementMade = true;
66
                        
67
                        break 2;
68
                    }
69
                }
70
            }
71
        } while($improvementMade);
72
        
73
        return $this->existingRoute;
74
    }
75
    
76
    private function twoOptSwap($iKey, $kKey)
77
    {
78
        $newRouteNodes = array();
79
        foreach($this->existingRoute->getKeys() as $key)
80
        {
81
            if ($key === $iKey)
82
            {
83
                $newRouteNodes[] = $this->existingRoute->getKey($kKey);
84
            }
85
            elseif($key === $kKey)
86
            {
87
                $newRouteNodes[] = $this->existingRoute->getKey($iKey);
88
            }
89
            else
90
            {
91
                $newRouteNodes[] = $this->existingRoute->getKey($key);
92
            }
93
        }
94
        
95
        return new NodePath($newRouteNodes);
96
    }
97
}
98