These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Location; |
||
5 | |||
6 | use Location\Distance\DistanceInterface; |
||
7 | use Location\Formatter\Polyline\FormatterInterface; |
||
8 | |||
9 | /** |
||
10 | * Polyline Implementation |
||
11 | * |
||
12 | * @author Marcus Jaschen <[email protected]> |
||
13 | */ |
||
14 | class Polyline implements GeometryInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var Coordinate[] |
||
18 | */ |
||
19 | protected $points = []; |
||
20 | |||
21 | /** |
||
22 | * @param Coordinate $point |
||
23 | * |
||
24 | * @return void |
||
25 | */ |
||
26 | public function addPoint(Coordinate $point) |
||
27 | { |
||
28 | $this->points[] = $point; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return Coordinate[] |
||
33 | */ |
||
34 | public function getPoints(): array |
||
35 | { |
||
36 | return $this->points; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return int |
||
41 | */ |
||
42 | public function getNumberOfPoints(): int |
||
43 | { |
||
44 | return count($this->points); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param FormatterInterface $formatter |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | public function format(FormatterInterface $formatter) |
||
53 | { |
||
54 | return $formatter->format($this); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return Line[] |
||
59 | */ |
||
60 | public function getSegments(): array |
||
61 | { |
||
62 | $length = count($this->points); |
||
63 | $segments = []; |
||
64 | |||
65 | if ($length <= 1) { |
||
66 | return $segments; |
||
67 | } |
||
68 | |||
69 | View Code Duplication | for ($i = 1; $i < $length; $i++) { |
|
0 ignored issues
–
show
|
|||
70 | $segments[] = new Line($this->points[$i - 1], $this->points[$i]); |
||
71 | } |
||
72 | |||
73 | return $segments; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Calculates the length of the polyline. |
||
78 | * |
||
79 | * @param DistanceInterface $calculator instance of distance calculation class |
||
80 | * |
||
81 | * @return float |
||
82 | */ |
||
83 | 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...
|
|||
84 | { |
||
85 | $distance = 0.0; |
||
86 | |||
87 | if (count($this->points) <= 1) { |
||
88 | return $distance; |
||
89 | } |
||
90 | |||
91 | foreach ($this->getSegments() as $segment) { |
||
92 | $distance += $segment->getLength($calculator); |
||
93 | } |
||
94 | |||
95 | return $distance; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Create a new polyline with reversed order of points, i. e. reversed |
||
100 | * polyline direction. |
||
101 | * |
||
102 | * @return Polyline |
||
103 | */ |
||
104 | 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...
|
|||
105 | { |
||
106 | $reversed = new static(); |
||
107 | |||
108 | foreach (array_reverse($this->points) as $point) { |
||
109 | $reversed->addPoint($point); |
||
110 | } |
||
111 | |||
112 | return $reversed; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return Bounds |
||
117 | */ |
||
118 | public function getBounds(): Bounds |
||
119 | { |
||
120 | $latMin = 90.0; |
||
121 | $latMax = -90.0; |
||
122 | $lngMin = 180.0; |
||
123 | $lngMax = -180.0; |
||
124 | |||
125 | foreach ($this->points as $point) { |
||
126 | $latMin = min($point->getLat(), $latMin); |
||
127 | $lngMin = min($point->getLng(), $lngMin); |
||
128 | $latMax = max($point->getLat(), $latMax); |
||
129 | $lngMax = max($point->getLng(), $lngMax); |
||
130 | } |
||
131 | |||
132 | return new Bounds( |
||
133 | new Coordinate($latMax, $lngMin), |
||
134 | new Coordinate($latMin, $lngMax) |
||
135 | ); |
||
136 | } |
||
137 | } |
||
138 |
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.