Completed
Push — master ( 4f7719...080a41 )
by Marcus
06:01
created

src/Location/Polyline.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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