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

Polygon::isPointInPolygon()   B

Complexity

Conditions 11
Paths 48

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11.0077

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 24
nc 48
nop 1
dl 0
loc 36
ccs 24
cts 25
cp 0.96
crap 11.0077
rs 7.3166
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 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
}