Completed
Push — master ( 195fbe...724c19 )
by Ventaquil
02:47
created

Path   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 43
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 8 2
A addNode() 0 14 2
A checkDistance() 0 6 3
A copy() 0 7 1
1
<?php
2
3
namespace PHPAlgorithms\Dijkstra;
4
5
use PHPAlgorithms\Dijkstra\Exceptions\PathException;
6
7
class Path {
8
    private $nodes = array();
9
    private $distance = 0;
10
11
    public function __get($name)
12
    {
13
        if (isset($this->{$name})) {
14
            return $this->{$name};
15
        }
16
17
        return null;
18
    }
19
20
    public function addNode($point, $distance = 0)
21
    {
22
        if ($point instanceof Point) {
23
            $point = $point->id;
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in PHPAlgorithms\Dijkstra\Point.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
24
        }
25
26
        $this->nodes[] = $point;
27
28
        $this->checkDistance($distance);
29
30
        $this->distance += $distance;
31
32
        return $this;
33
    }
34
35
    public function checkDistance($distance)
36
    {
37
        if (!is_numeric($distance) && ($distance <= 0)) {
38
            throw new PathException('Distance must be numeric value greater than 0');
39
        }
40
    }
41
42
    public function copy(self $path)
43
    {
44
        $this->nodes = $path->nodes;
45
        $this->distance = $path->distance;
46
47
        return $this;
48
    }
49
}
50