This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Location; |
||
6 | |||
7 | use Location\Distance\DistanceInterface; |
||
8 | use Location\Exception\InvalidGeometryException; |
||
9 | use Location\Formatter\Polyline\FormatterInterface; |
||
10 | |||
11 | /** |
||
12 | * Polyline Implementation |
||
13 | * |
||
14 | * @author Marcus Jaschen <[email protected]> |
||
15 | */ |
||
16 | class Polyline implements GeometryInterface |
||
17 | { |
||
18 | use GetBoundsTrait; |
||
19 | |||
20 | /** |
||
21 | * @var Coordinate[] |
||
22 | */ |
||
23 | protected $points = []; |
||
24 | |||
25 | /** |
||
26 | * @param Coordinate $point |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | public function addPoint(Coordinate $point): void |
||
31 | { |
||
32 | $this->points[] = $point; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param array $points |
||
37 | */ |
||
38 | public function addPoints(array $points): void |
||
39 | { |
||
40 | foreach ($points as $point) { |
||
41 | $this->addPoint($point); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Adds an unique point to the polyline. A maximum allowed distance for |
||
47 | * same point comparison can be provided. Default allowed distance |
||
48 | * deviation is 0.001 meters (1 millimeter). |
||
49 | * |
||
50 | * @param Coordinate $point |
||
51 | * @param float $allowedDistance |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | public function addUniquePoint(Coordinate $point, float $allowedDistance = .001): void |
||
56 | { |
||
57 | if ($this->containsPoint($point, $allowedDistance)) { |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | $this->addPoint($point); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return Coordinate[] |
||
66 | */ |
||
67 | public function getPoints(): array |
||
68 | { |
||
69 | return $this->points; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return int |
||
74 | */ |
||
75 | public function getNumberOfPoints(): int |
||
76 | { |
||
77 | return count($this->points); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param Coordinate $point |
||
82 | * @param float $allowedDistance |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function containsPoint(Coordinate $point, float $allowedDistance = .001): bool |
||
87 | { |
||
88 | foreach ($this->points as $existingPoint) { |
||
89 | if ($existingPoint->hasSameLocation($point, $allowedDistance)) { |
||
90 | return true; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | return false; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param FormatterInterface $formatter |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function format(FormatterInterface $formatter): string |
||
103 | { |
||
104 | return $formatter->format($this); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return Line[] |
||
109 | */ |
||
110 | public function getSegments(): array |
||
111 | { |
||
112 | $length = count($this->points); |
||
113 | $segments = []; |
||
114 | |||
115 | if ($length <= 1) { |
||
116 | return $segments; |
||
117 | } |
||
118 | |||
119 | View Code Duplication | for ($i = 1; $i < $length; $i++) { |
|
0 ignored issues
–
show
|
|||
120 | $segments[] = new Line($this->points[$i - 1], $this->points[$i]); |
||
121 | } |
||
122 | |||
123 | return $segments; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Calculates the length of the polyline. |
||
128 | * |
||
129 | * @param DistanceInterface $calculator instance of distance calculation class |
||
130 | * |
||
131 | * @return float |
||
132 | */ |
||
133 | View Code Duplication | public function getLength(DistanceInterface $calculator): float |
|
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...
|
|||
134 | { |
||
135 | $distance = 0.0; |
||
136 | |||
137 | if (count($this->points) <= 1) { |
||
138 | return $distance; |
||
139 | } |
||
140 | |||
141 | foreach ($this->getSegments() as $segment) { |
||
142 | $distance += $segment->getLength($calculator); |
||
143 | } |
||
144 | |||
145 | return $distance; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Create a new polyline with reversed order of points, i. e. reversed |
||
150 | * polyline direction. |
||
151 | * |
||
152 | * @return Polyline |
||
153 | */ |
||
154 | View Code Duplication | public function getReverse(): Polyline |
|
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...
|
|||
155 | { |
||
156 | $reversed = new self(); |
||
157 | |||
158 | foreach (array_reverse($this->points) as $point) { |
||
159 | $reversed->addPoint($point); |
||
160 | } |
||
161 | |||
162 | return $reversed; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Returns the point which is defined by the avarages of all |
||
167 | * latitude/longitude values. |
||
168 | * |
||
169 | * This currently only works for polylines which don't cross the dateline at |
||
170 | * 180/-180 degrees longitude. |
||
171 | * |
||
172 | * @return Coordinate |
||
173 | * |
||
174 | * @throws InvalidGeometryException when the polyline doesn't contain any points. |
||
175 | */ |
||
176 | public function getAveragePoint(): Coordinate |
||
177 | { |
||
178 | $latitude = 0.0; |
||
179 | $longitude = 0.0; |
||
180 | $numberOfPoints = count($this->points); |
||
181 | |||
182 | if ($this->getNumberOfPoints() === 0) { |
||
183 | throw new InvalidGeometryException('Polyline doesn\'t contain points', 9464188927); |
||
184 | } |
||
185 | |||
186 | foreach ($this->points as $point) { |
||
187 | /* @var $point Coordinate */ |
||
188 | $latitude += $point->getLat(); |
||
189 | $longitude += $point->getLng(); |
||
190 | } |
||
191 | |||
192 | $latitude /= $numberOfPoints; |
||
193 | $longitude /= $numberOfPoints; |
||
194 | |||
195 | return new Coordinate($latitude, $longitude); |
||
196 | } |
||
197 | } |
||
198 |
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.