Polygon::isPointInPolygon()   B
last analyzed

Complexity

Conditions 10
Paths 36

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
nc 36
nop 1
dl 0
loc 33
ccs 21
cts 21
cp 1
crap 10
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace NoaaCapAlerts\Model\Polygon;
4
5
class Polygon
6
{
7
    private array $points;
8
9
    function __construct(array $points = null)
10
    {
11
        if ($points === null) {
12
            $points = [];
13 5
        }
14
15 5
        $this->points = $points;
16 3
    }
17
18
    /**
19 5
     * @throws \Exception
20 5
     */
21
    public function isPointInPolygon(Point $targetPoint): bool
22 2
    {
23
        $targetLatitude = $targetPoint->getLatitude();
24 2
        $longitudeHits = [];
25 2
        $lastPoint = $this->points[array_key_last($this->points)];
26 2
        foreach ($this->points as $point) {
27 2
            if ($this->doLinesCrossLatitude($lastPoint, $point, $targetLatitude)) {
28 2
                $longitudeHits[] = $this->findInterceptLongitude($lastPoint, $point, $targetLatitude);
29 2
            }
30
            $lastPoint = $point;
31 2
        }
32
33
        $longitudeHits = array_unique($longitudeHits);
34 2
        sort($longitudeHits);
35 2
36
        $hitsBefore = 0;
37 2
        $hitsAfter = 0;
38 2
39
        foreach ($longitudeHits as $hit) {
40 2
            if ($hit <= $targetPoint->getLongitude()) {
41 2
                $hitsBefore++;
42 2
            } elseif ($hit >= $targetPoint->getLongitude()) {
43 1
                $hitsAfter++;
44 1
            }
45
        }
46
47
        if (($hitsBefore % 2 == 1) && ($hitsAfter % 2 == 1)) {
48 2
            return true;
49 1
        } elseif (($hitsBefore % 2 == 0) && ($hitsAfter % 2 == 0)) {
50 1
            return false;
51 1
        }
52
53
        throw new \Exception("Something weird happened.  Fixme");
54
    }
55
56
    private function doLinesCrossLatitude(Point $point1, Point $point2, float $latitude): bool
57 2
    {
58
        if ($point1->getLatitude() >= $latitude && $point2->getLatitude() <= $latitude) {
59 2
            return true;
60 2
        } elseif ($point2->getLatitude() >= $latitude && $point1->getLatitude() <= $latitude) {
61 2
            return true;
62 2
        } else {
63
            return false;
64 2
        }
65
    }
66
67
    private function findInterceptLongitude(Point $point1, Point $point2, float $latitude): float
68 2
    {
69
        if ($point2->getLongitude() - $point1->getLongitude() == 0) {
70 2
            return $point2->getLongitude();
71 2
        }
72
73
        $slope = ($point2->getLatitude() - $point1->getLatitude()) / ($point2->getLongitude() - $point1->getLongitude());
74
        $b = $point1->getLatitude() - ($slope * $point1->getLongitude());
75
76
        return ($latitude + $b) / $slope;
77
    }
78
79
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
    {
81
        $pointStrings = [];
82
        foreach ($this->points as $point) {
83
            $pointStrings[] = '[' . $point->getX() . ', ' . $point->getY() . ']';
84
        }
85
86
        return "Points: " . implode(', ', $pointStrings);
87
    }
88
}