Test Failed
Branch master (529f7c)
by Tom
07:56 queued 05:50
created

Polygon::isPointInPolygon()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
nc 36
nop 1
dl 0
loc 33
ccs 0
cts 22
cp 0
crap 110
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 $points;
8
9
    /**
10
     * Polygon constructor.
11
     * @param array $points
12
     */
13 3
    function __construct(array $points = null)
14
    {
15 3
        if ($points === null) {
16 3
            $points = [];
17
        }
18
19 3
        $this->points = $points;
20 3
    }
21
22
    public function isPointInPolygon(Point $targetPoint): bool
23
    {
24
        $targetLatitude = $targetPoint->getLatitude();
25
        $longitudeHits = [];
26
        $lastPoint = $this->points[array_key_last($this->points)];
27
        foreach ($this->points as $point) {
28
            if ($this->doLinesCrossLatitude($lastPoint, $point, $targetLatitude)) {
29
                $longitudeHits[] = $this->findInterceptLongitude($lastPoint, $point, $targetLatitude);
30
            }
31
            $lastPoint = $point;
32
        }
33
34
        $longitudeHits = array_unique($longitudeHits);
35
        sort($longitudeHits);
36
37
        $hitsBefore = 0;
38
        $hitsAfter = 0;
39
40
        foreach ($longitudeHits as $hit) {
41
            if ($hit <= $targetPoint->getLongitude()) {
42
                $hitsBefore++;
43
            } elseif ($hit >= $targetPoint->getLongitude()) {
44
                $hitsAfter++;
45
            }
46
        }
47
48
        if (($hitsBefore % 2 == 1) && ($hitsAfter % 2 == 1)) {
49
            return true;
50
        } elseif (($hitsBefore % 2 == 0) && ($hitsAfter % 2 == 0)) {
51
            return false;
52
        }
53
54
        throw new \Exception("Something weird happened.  Fixme");
55
    }
56
57
    private function doLinesCrossLatitude(Point $point1, Point $point2, float $latitude): bool
58
    {
59
        if ($point1->getLatitude() >= $latitude && $point2->getLatitude() <= $latitude) {
60
            return true;
61
        } elseif ($point2->getLatitude() >= $latitude && $point1->getLatitude() <= $latitude) {
62
            return true;
63
        } else {
64
            return false;
65
        }
66
    }
67
68
    private function findInterceptLongitude(Point $point1, Point $point2, float $latitude): float
69
    {
70
        if ($point2->getLongitude() - $point1->getLongitude() == 0) {
71
            return $point2->getLongitude();
72
        }
73
74
        $slope = ($point2->getLatitude() - $point1->getLatitude()) / ($point2->getLongitude() - $point1->getLongitude());
75
        $b = $point1->getLatitude() - ($slope * $point1->getLongitude());
76
77
        return ($latitude + $b) / $slope;
78
    }
79
80
    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...
81
    {
82
        $pointStrings = [];
83
        foreach ($this->points as $point) {
84
            $pointStrings[] = '[' . $point->getX() . ', ' . $point->getY() . ']';
85
        }
86
87
        return "Points: " . implode(', ', $pointStrings);
88
    }
89
}