Completed
Push — develop-1.0 ( 7f9527...fdc9db )
by Marcus
02:09
created

Polyline::getReverse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/**
3
 * Polyline Implementation
4
 *
5
 * PHP version 5
6
 *
7
 * @category  Location
8
 * @author    Marcus Jaschen <[email protected]>
9
 * @license   https://opensource.org/licenses/GPL-3.0 GPL
10
 * @link      https://github.com/mjaschen/phpgeo
11
 */
12
13
namespace Location;
14
15
use Location\Distance\DistanceInterface;
16
use Location\Formatter\Polyline\FormatterInterface;
17
18
/**
19
 * Polyline Implementation
20
 *
21
 * @category Location
22
 * @author   Marcus Jaschen <[email protected]>
23
 * @license  https://opensource.org/licenses/GPL-3.0 GPL
24
 * @link     https://github.com/mjaschen/phpgeo
25
 */
26
class Polyline
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $points = [];
32
33
    /**
34
     * @param Coordinate $point
35
     */
36
    public function addPoint(Coordinate $point)
37
    {
38
        $this->points[] = $point;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function getPoints()
45
    {
46
        return $this->points;
47
    }
48
49
    /**
50
     * @return int
51
     */
52
    public function getNumberOfPoints()
53
    {
54
        return count($this->points);
55
    }
56
57
    /**
58
     * @param FormatterInterface $formatter
59
     *
60
     * @return mixed
61
     */
62
    public function format(FormatterInterface $formatter)
63
    {
64
        return $formatter->format($this);
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getSegments()
71
    {
72
        $segments = [];
73
74
        if (count($this->points) <= 1) {
75
            return $segments;
76
        }
77
78
        $previousPoint = reset($this->points);
79
80 View Code Duplication
        while ($point = next($this->points)) {
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...
81
            $segments[]    = new Line($previousPoint, $point);
82
            $previousPoint = $point;
83
        }
84
85
        return $segments;
86
    }
87
88
    /**
89
     * Calculates the length of the polyline.
90
     *
91
     * @param DistanceInterface $calculator instance of distance calculation class
92
     *
93
     * @return float
94
     */
95 View Code Duplication
    public function getLength(DistanceInterface $calculator)
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...
96
    {
97
        $distance = 0.0;
98
99
        if (count($this->points) <= 1) {
100
            return $distance;
101
        }
102
103
        foreach ($this->getSegments() as $segment) {
104
            $distance += $segment->getLength($calculator);
105
        }
106
107
        return $distance;
108
    }
109
110
    /**
111
     * Create a new polyline with reversed order of points, i. e. reversed
112
     * polyline direction.
113
     *
114
     * @return Polyline
115
     */
116
    public function getReverse()
117
    {
118
        $reversed = new static();
119
120
        foreach (array_reverse($this->points) as $point) {
121
            $reversed->addPoint($point);
122
        }
123
124
        return $reversed;
125
    }
126
}
127