Passed
Push — master ( 287584...820428 )
by Tom
03:21
created

Polygon   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 80.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 85
ccs 38
cts 47
cp 0.8085
rs 10
c 1
b 0
f 0
wmc 22

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A findInterceptLongitude() 0 10 2
B isPointInPolygon() 0 36 11
A doLinesCrossLatitude() 0 7 5
A __toString() 0 8 2
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 5
    function __construct(array $points = null)
14
    {
15 5
        if ($points === null) {
16 3
            $points = [];
17
        }
18
19 5
        $this->points = $points;
20 5
    }
21
22 2
    public function isPointInPolygon(Point $targetPoint): bool {
23 2
        $targetLatitude = $targetPoint->getLatitude();
24 2
        $longitudeHits = [];
25 2
        $lastPoint = null;
26 2
        foreach ($this->points as $point) {
27 2
            if ($lastPoint == null) {
28 2
                $lastPoint = $point;
29 2
                continue;
30
            }
31 2
            if ($this->doLinesCrossLatitude($lastPoint, $point, $targetLatitude)) {
0 ignored issues
show
Bug introduced by
$lastPoint of type null is incompatible with the type NoaaCapAlerts\Model\Polygon\Point expected by parameter $point1 of NoaaCapAlerts\Model\Poly...:doLinesCrossLatitude(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
            if ($this->doLinesCrossLatitude(/** @scrutinizer ignore-type */ $lastPoint, $point, $targetLatitude)) {
Loading history...
32 2
                $longitudeHits[] = $this->findInterceptLongitude($lastPoint, $point, $targetLatitude);
0 ignored issues
show
Bug introduced by
$lastPoint of type null is incompatible with the type NoaaCapAlerts\Model\Polygon\Point expected by parameter $point1 of NoaaCapAlerts\Model\Poly...indInterceptLongitude(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
                $longitudeHits[] = $this->findInterceptLongitude(/** @scrutinizer ignore-type */ $lastPoint, $point, $targetLatitude);
Loading history...
33
            }
34 2
            $lastPoint = $point;
35
        }
36
37 2
        $longitudeHits = array_unique($longitudeHits);
38 2
        sort($longitudeHits);
39
40 2
        $hitsBefore = 0;
41 2
        $hitsAfter = 0;
42
43 2
        foreach($longitudeHits as $hit) {
44 2
            if ($hit <= $targetPoint->getLongitude()) {
45 2
                $hitsBefore++;
46 1
            } elseif ($hit >= $targetPoint->getLongitude()) {
47 2
                $hitsAfter++;
48
            }
49
        }
50
51 2
        if (($hitsBefore % 2 == 1) && ($hitsAfter % 2 == 1)) {
52 1
            return true;
53 1
        } elseif (($hitsBefore % 2 == 0) && ($hitsAfter % 2 == 0)) {
54 1
            return false;
55
        }
56
57
        throw new \Exception("Something weird happened.  Fixme");
58
    }
59
60 2
    private function doLinesCrossLatitude(Point $point1, Point $point2, float $latitude): bool {
61 2
        if ($point1->getLatitude() >= $latitude && $point2->getLatitude() <= $latitude) {
62 2
            return true;
63 2
        } elseif ($point2->getLatitude() >= $latitude && $point1->getLatitude() <= $latitude) {
64 2
            return true;
65
        } else {
66 2
            return false;
67
        }
68
    }
69
70 2
    private function findInterceptLongitude(Point $point1, Point $point2, float $latitude): float
71
    {
72 2
        if ($point2->getLongitude() - $point1->getLongitude() == 0) {
73 2
            return $point2->getLongitude();
74
        }
75
76
        $slope = ($point2->getLatitude() - $point1->getLatitude()) / ($point2->getLongitude() - $point1->getLongitude());
77
        $b = $point1->getLatitude() - ($slope*$point1->getLongitude());
78
79
        return ($latitude + $b) / $slope;
80
    }
81
82
    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...
83
    {
84
        $pointStrings = [];
85
        foreach ($this->points as $point) {
86
            $pointStrings[] = '[' . $point->getX() . ', ' . $point->getY() . ']';
87
        }
88
89
        return "Points: " . implode(', ', $pointStrings);
90
    }
91
}