Issues (16)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Polygon.php (3 issues)

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
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
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
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
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