Polygon   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 253
Duplicated Lines 10.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 6
dl 27
loc 253
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getPoints() 0 4 1
A getLats() 0 11 2
A getLngs() 0 11 2
A getNumberOfPoints() 0 4 1
A format() 0 4 1
A getSegments() 3 19 3
A addPoint() 0 4 1
A addPoints() 0 6 2
A containsGeometry() 0 10 3
A contains() 0 21 4
A getPerimeter() 14 14 3
A getArea() 0 31 3
A getReverse() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Location;
6
7
use Location\Distance\DistanceInterface;
8
use Location\Formatter\Polygon\FormatterInterface;
9
10
/**
11
 * Polygon Implementation
12
 *
13
 * @author Paul Vidal <[email protected]>
14
 * @author Marcus Jaschen <[email protected]>
15
 */
16
class Polygon 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
     * @return Coordinate[]
47
     */
48
    public function getPoints(): array
49
    {
50
        return $this->points;
51
    }
52
53
    /**
54
     * Return all polygon point's latitudes.
55
     *
56
     * @return float[]
57
     */
58
    public function getLats(): array
59
    {
60
        $lats = [];
61
62
        foreach ($this->points as $point) {
63
            /** @var Coordinate $point */
64
            $lats[] = $point->getLat();
65
        }
66
67
        return $lats;
68
    }
69
70
    /**
71
     * Return all polygon point's longitudes.
72
     *
73
     * @return float[]
74
     */
75
    public function getLngs(): array
76
    {
77
        $lngs = [];
78
79
        foreach ($this->points as $point) {
80
            /** @var Coordinate $point */
81
            $lngs[] = $point->getLng();
82
        }
83
84
        return $lngs;
85
    }
86
87
    /**
88
     * @return int
89
     */
90
    public function getNumberOfPoints(): int
91
    {
92
        return count($this->points);
93
    }
94
95
    /**
96
     * @param FormatterInterface $formatter
97
     *
98
     * @return string
99
     */
100
    public function format(FormatterInterface $formatter): string
101
    {
102
        return $formatter->format($this);
103
    }
104
105
    /**
106
     * @return Line[]
107
     */
108
    public function getSegments(): array
109
    {
110
        $length = count($this->points);
111
        $segments = [];
112
113
        if ($length <= 1) {
114
            return $segments;
115
        }
116
117 View Code Duplication
        for ($i = 1; $i < $length; $i++) {
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...
118
            $segments[] = new Line($this->points[$i - 1], $this->points[$i]);
119
        }
120
121
        // to close the polygon we have to add the final segment between
122
        // the last point and the first point
123
        $segments[] = new Line($this->points[$i - 1], $this->points[0]);
124
125
        return $segments;
126
    }
127
128
    /**
129
     * Determine if given geometry is contained inside the polygon. This is
130
     * assumed to be true, if each point of the geometry is inside the polygon.
131
     *
132
     * Edge cases:
133
     *
134
     * - it's not detected when a line between two points is outside the polygon
135
     * - @see contains() for more restrictions
136
     *
137
     * @param GeometryInterface $geometry
138
     *
139
     * @return boolean
140
     */
141
    public function containsGeometry(GeometryInterface $geometry): bool
142
    {
143
        $geometryInPolygon = true;
144
145
        foreach ($geometry->getPoints() as $point) {
146
            $geometryInPolygon = $geometryInPolygon && $this->contains($point);
147
        }
148
149
        return $geometryInPolygon;
150
    }
151
152
    /**
153
     * Determine if given point is contained inside the polygon. Uses the PNPOLY
154
     * algorithm by W. Randolph Franklin. Therfore some edge cases may not give the
155
     * expected results, e. g. if the point resides on the polygon boundary.
156
     *
157
     * @see https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html
158
     *
159
     * For special cases this calculation leads to wrong results:
160
     *
161
     * - if the polygons spans over the longitude boundaries at 180/-180 degrees
162
     *
163
     * @param Coordinate $point
164
     *
165
     * @return bool
166
     */
167
    public function contains(Coordinate $point): bool
168
    {
169
        $numberOfPoints = $this->getNumberOfPoints();
170
        $polygonLats    = $this->getLats();
171
        $polygonLngs    = $this->getLngs();
172
173
        $polygonContainsPoint = false;
174
175
        for ($node = 0, $altNode = ($numberOfPoints - 1); $node < $numberOfPoints; $altNode = $node++) {
176
            $condition = ($polygonLngs[$node] > $point->getLng()) !== ($polygonLngs[$altNode] > $point->getLng())
177
                && ($point->getLat() < ($polygonLats[$altNode] - $polygonLats[$node])
178
                    * ($point->getLng() - $polygonLngs[$node])
179
                    / ($polygonLngs[$altNode] - $polygonLngs[$node]) + $polygonLats[$node]);
180
181
            if ($condition) {
182
                $polygonContainsPoint = ! $polygonContainsPoint;
183
            }
184
        }
185
186
        return $polygonContainsPoint;
187
    }
188
189
    /**
190
     * Calculates the polygon perimeter.
191
     *
192
     * @param DistanceInterface $calculator instance of distance calculation class
193
     *
194
     * @return float
195
     */
196 View Code Duplication
    public function getPerimeter(DistanceInterface $calculator): float
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...
197
    {
198
        $perimeter = 0.0;
199
200
        if (count($this->points) < 2) {
201
            return $perimeter;
202
        }
203
204
        foreach ($this->getSegments() as $segment) {
205
            $perimeter += $segment->getLength($calculator);
206
        }
207
208
        return $perimeter;
209
    }
210
211
    /**
212
     * Calculates the polygon area.
213
     *
214
     * This algorithm gives inaccurate results as it ignores
215
     * ellipsoid parameters other than to arithmetic mean radius.
216
     * The error should be < 1 % for small areas.
217
     *
218
     * @return float
219
     */
220
    public function getArea(): float
221
    {
222
        $area = 0;
223
224
        if ($this->getNumberOfPoints() <= 2) {
225
            return $area;
226
        }
227
228
        $referencePoint = $this->points[0];
229
        $radius         = $referencePoint->getEllipsoid()->getArithmeticMeanRadius();
230
        $segments       = $this->getSegments();
231
232
        foreach ($segments as $segment) {
233
            /** @var Coordinate $point1 */
234
            $point1 = $segment->getPoint1();
235
            /** @var Coordinate $point2 */
236
            $point2 = $segment->getPoint2();
237
238
            $x1 = deg2rad($point1->getLng() - $referencePoint->getLng()) * cos(deg2rad($point1->getLat()));
239
            $y1 = deg2rad($point1->getLat() - $referencePoint->getLat());
240
241
            $x2 = deg2rad($point2->getLng() - $referencePoint->getLng()) * cos(deg2rad($point2->getLat()));
242
            $y2 = deg2rad($point2->getLat() - $referencePoint->getLat());
243
244
            $area += ($x2 * $y1 - $x1 * $y2);
245
        }
246
247
        $area *= 0.5 * $radius ** 2;
248
249
        return (float)abs($area);
250
    }
251
252
    /**
253
     * Create a new polygon with reversed order of points, i. e. reversed
254
     * polygon direction.
255
     *
256
     * @return Polygon
257
     */
258 View Code Duplication
    public function getReverse(): Polygon
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...
259
    {
260
        $reversed = new self();
261
262
        foreach (array_reverse($this->points) as $point) {
263
            $reversed->addPoint($point);
264
        }
265
266
        return $reversed;
267
    }
268
}
269