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

Path::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 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