| Conditions | 18 |
| Paths | 14 |
| Total Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 148 | public function pointInPolygon($point, $pointOnBoundary = true, $pointOnVertex = true) { |
||
| 149 | $vertices = $this->getPoints(); |
||
| 150 | |||
| 151 | // Check if the point sits exactly on a vertex |
||
| 152 | if ($this->pointOnVertex($point, $vertices)) { |
||
| 153 | return $pointOnVertex ? TRUE : FALSE; |
||
| 154 | } |
||
| 155 | |||
| 156 | // Check if the point is inside the polygon or on the boundary |
||
| 157 | $intersections = 0; |
||
| 158 | $vertices_count = count($vertices); |
||
| 159 | |||
| 160 | for ($i=1; $i < $vertices_count; $i++) { |
||
| 161 | $vertex1 = $vertices[$i-1]; |
||
| 162 | $vertex2 = $vertices[$i]; |
||
| 163 | if ($vertex1->y() == $vertex2->y() |
||
| 164 | && $vertex1->y() == $point->y() |
||
| 165 | && $point->x() > min($vertex1->x(), $vertex2->x()) |
||
| 166 | && $point->x() < max($vertex1->x(), $vertex2->x())) { |
||
| 167 | // Check if point is on an horizontal polygon boundary |
||
| 168 | return $pointOnBoundary ? TRUE : FALSE; |
||
| 169 | } |
||
| 170 | if ($point->y() > min($vertex1->y(), $vertex2->y()) |
||
| 171 | && $point->y() <= max($vertex1->y(), $vertex2->y()) |
||
| 172 | && $point->x() <= max($vertex1->x(), $vertex2->x()) |
||
| 173 | && $vertex1->y() != $vertex2->y()) { |
||
| 174 | $xinters = |
||
| 175 | ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) |
||
| 176 | / ($vertex2->y() - $vertex1->y()) |
||
| 177 | + $vertex1->x(); |
||
| 178 | if ($xinters == $point->x()) { |
||
| 179 | // Check if point is on the polygon boundary (other than horizontal) |
||
| 180 | return $pointOnBoundary ? TRUE : FALSE; |
||
| 181 | } |
||
| 182 | if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) { |
||
| 183 | $intersections++; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | // If the number of edges we passed through is even, then it's in the polygon. |
||
| 188 | if ($intersections % 2 != 0) { |
||
| 189 | return TRUE; |
||
| 190 | } |
||
| 191 | else { |
||
| 192 | return FALSE; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 211 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.