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.

NodeGraph::getEdgesTo()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Letournel\PathFinder\Core;
4
5
class NodeGraph
6
{
7
    private
8
        $edges,
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 $edges.

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...
9
        $vertexes,
10
        $symetric;
11
    
12
    public function __construct(array $nodes)
13
    {
14
        $this->edges = array();
15
        $this->vertexes = array();
16
        $this->symetric = true;
17
        foreach($nodes as $node)
18
        {
19
            if($node instanceof Node)
20
            {
21
                $this->vertexes[$node->getId()] = $node;
22
            }
23
        }
24
    }
25
    
26
    public function setSymetricMode($symetric)
27
    {
28
        $this->symetric = $symetric;
29
        
30
        return $this;
31
    }
32
    
33
    public function createEdgeBetween(Node $a, Node $b, $value)
34
    {
35
        $aId = $a->getId();
36
        $bId = $b->getId();
37
        if(! array_key_exists($aId, $this->vertexes) || ! array_key_exists($bId, $this->vertexes))
38
        {
39
            return;
40
        }
41
        if($aId === $bId)
42
        {
43
            return;
44
        }
45
        
46
        if($this->symetric === true)
47
        {
48
            $this->edges[$bId][$aId] = (float) $value;
49
        }
50
        $this->edges[$aId][$bId] = (float) $value;
51
    }
52
    
53
    public function getEdgeBetween(Node $a, Node $b)
54
    {
55
        $aId = $a->getId();
56
        $bId = $b->getId();
57 View Code Duplication
        if(! array_key_exists($aId, $this->vertexes) || ! array_key_exists($bId, $this->vertexes))
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
            return null;
60
        }
61
        if($aId === $bId)
62
        {
63
            return 0;
64
        }
65
        if(! array_key_exists($aId, $this->edges))
66
        {
67
            return INF;
68
        }
69
        if(! array_key_exists($bId, $this->edges[$aId]))
70
        {
71
            return INF;
72
        }
73
        
74
        return $this->edges[$aId][$bId];
75
    }
76
    
77
    public function existsEdgeBetween(Node $a, Node $b)
78
    {
79
        $aId = $a->getId();
80
        $bId = $b->getId();
81 View Code Duplication
        if(! array_key_exists($aId, $this->vertexes) || ! array_key_exists($bId, $this->vertexes))
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...
82
        {
83
            return false;
84
        }
85
        if($aId === $bId)
86
        {
87
            return false;
88
        }
89
        if(! array_key_exists($aId, $this->edges))
90
        {
91
            return false;
92
        }
93
        if(! array_key_exists($bId, $this->edges[$aId]))
94
        {
95
            return false;
96
        }
97
        
98
        return true;
99
    }
100
    
101
    public function containsEdges(Node $a, Node $b)
102
    {
103
        $aId = $a->getId();
104
        $bId = $b->getId();
105
        
106
        return array_key_exists($aId, $this->vertexes) && array_key_exists($bId, $this->vertexes);
107
    }
108
    
109
    public function removeEdgeBetween(Node $a, Node $b)
110
    {
111
        $aId = $a->getId();
112
        $bId = $b->getId();
113
        if(! array_key_exists($aId, $this->vertexes) || ! array_key_exists($bId, $this->vertexes))
114
        {
115
            return;
116
        }
117
        
118
        unset($this->edges[$aId][$bId]);
119
        unset($this->edges[$bId][$aId]);
120
    }
121
    
122
    public function getVertexes()
123
    {
124
        return $this->vertexes;
125
    }
126
    
127
    public function getVertex($id)
128
    {
129
        if(array_key_exists($id, $this->vertexes))
130
        {
131
            return $this->vertexes[$id];
132
        }
133
        
134
        return null;
135
    }
136
    
137 View Code Duplication
    public function getEdgesFrom(Node $vertexFrom)
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...
138
    {
139
        $edges = array();
140
        $vertexFromId = $vertexFrom->getId();
141
        foreach($this->vertexes as $vertexToId => $vertexTo)
142
        {
143
            if(isset($this->edges[$vertexFromId][$vertexToId]))
144
            {
145
                $edges[$vertexToId] = $this->edges[$vertexFromId][$vertexToId];
146
            }
147
        }
148
        
149
        return $edges;
150
    }
151
    
152 View Code Duplication
    public function getEdgesTo(Node $vertexTo)
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...
153
    {
154
        $edges = array();
155
        $vertexToId = $vertexTo->getId();
156
        foreach($this->vertexes as $vertexFromId => $vertexFrom)
157
        {
158
            if(isset($this->edges[$vertexFromId][$vertexToId]))
159
            {
160
                $edges[$vertexToId] = $this->edges[$vertexFromId][$vertexToId];
161
            }
162
        }
163
        
164
        return $edges;
165
    }
166
    
167 View Code Duplication
    public function computeLength(NodePath $route)
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...
168
    {
169
        $length = 0;
170
        $prevNode = null;
171
        foreach($route as $node)
172
        {
173
            if($prevNode === null)
174
            {
175
                $prevNode = $node;
176
                continue;
177
            }
178
            
179
            $length += $this->getEdgeBetween($prevNode, $node);
180
            $prevNode = $node;
181
        }
182
        
183
        return $length;
184
    }
185
}
186