Failed Conditions
Push — master ( 328b73...9cdd6e )
by Markus
04:54
created

functions.php ➔ pointIsInsidePolygon()   D

Complexity

Conditions 19
Paths 8

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 8
nop 4
dl 0
loc 31
rs 4.5166
c 0
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
//#######################################################################
4
// Human Time Ago
5
// @param $timestamp	=> timestamp (mandatory)
6
// @param $locales	=> locales (mandatory)
7
//
8
// Return time ago at human format (eg: 2 hours ago)
9
//#######################################################################
10
11
function time_ago($timestamp, $locales)
12
{
13
    // Set up our variables.
14
    $minute_in_seconds = 60;
15
    $hour_in_seconds = $minute_in_seconds * 60;
16
    $day_in_seconds = $hour_in_seconds * 24;
17
    $week_in_seconds = $day_in_seconds * 7;
18
    $month_in_seconds = $day_in_seconds * 30;
19
    $year_in_seconds = $day_in_seconds * 365;
20
21
    // current time
22
    $now = time();
23
24
    // Calculate the time difference between the current time reference point and the timestamp we're comparing.
25
    // The difference is defined negative, when in the future.
26
    $time_difference = $now - $timestamp;
27
28
    // Calculate the time ago using the smallest applicable unit.
29
    if ($time_difference < $hour_in_seconds) {
30
        $difference_value = abs(round($time_difference / $minute_in_seconds));
31
        $difference_label = 'MINUTE';
32
    } elseif ($time_difference < $day_in_seconds) {
33
        $difference_value = abs(round($time_difference / $hour_in_seconds));
34
        $difference_label = 'HOUR';
35
    } elseif ($time_difference < $week_in_seconds) {
36
        $difference_value = abs(round($time_difference / $day_in_seconds));
37
        $difference_label = 'DAY';
38
    } elseif ($time_difference < $month_in_seconds) {
39
        $difference_value = abs(round($time_difference / $week_in_seconds));
40
        $difference_label = 'WEEK';
41
    } elseif ($time_difference < $year_in_seconds) {
42
        $difference_value = abs(round($time_difference / $month_in_seconds));
43
        $difference_label = 'MONTH';
44
    } else {
45
        $difference_value = abs(round($time_difference / $year_in_seconds));
46
        $difference_label = 'YEAR';
47
    }
48
49
    // plural
50
    if (1 != $difference_value) {
51
        $difference_label = $difference_label.'S';
52
    }
53
54
    if ($time_difference <= 0) {
55
        // Present
56
        return sprintf($locales->TIME_LEFT, $difference_value.' '.$locales->$difference_label);
57
    } else {
58
        // Past
59
        return sprintf($locales->TIME_AGO, $difference_value.' '.$locales->$difference_label);
60
    }
61
}
62
63
//#######################################################################
64
// Percent calculator
65
// @param $val		=> int (mandatory)
66
// @param $val_total	=> int (mandatory)
67
//
68
// Return pourcent from total
69
//#######################################################################
70
71
function percent($val, $val_total)
72
{
73
    $count1 = $val_total / $val;
74
    $count2 = $count1 * 100;
75
76
    $count = number_format($count2, 0);
77
78
    return $count;
79
}
80
81
//#######################################################################
82
// File version (unix timestamp)
83
// @param $url		=> string (mandatory)
84
//
85
// Return $url with last_modified unix timestamp before suffix
86
//#######################################################################
87
88
function auto_ver($url)
89
{
90
    if (is_file(SYS_PATH.'/'.$url)) {
91
        $path = pathinfo($url);
92
        $ver = '.'.filemtime(SYS_PATH.'/'.$url).'.';
93
        echo $path['dirname'].'/'.preg_replace('/\.(css|js|json)$/', $ver.'$1', $path['basename']);
94
    } else {
95
        echo $url;
96
    }
97
}
98
99
//#######################################################################
100
// File age in secs
101
// @param $filepath     => string (mandatory)
102
//
103
// Return file age of file in secs, PHP_INT_MAX if file doesn't exist
104
//#######################################################################
105
106
function file_update_ago($filepath)
107
{
108
    if (is_file($filepath)) {
109
        $filemtime = filemtime($filepath);
110
        $now = time();
111
        $diff = $now - $filemtime;
112
113
        return $diff;
114
    }
115
    // file doesn't exist yet!
116
    return PHP_INT_MAX;
117
}
118
119
//#######################################################################
120
// Only keep data after $timestamp in $array (compared to 'timestamp' key)
121
// @param $array     => array (mandatory)
122
// @param $timestamp => int (mandatory)
123
//
124
// Return trimmed array
125
//#######################################################################
126
127
function trim_stats_json($array, $timestamp)
128
{
129
    foreach ($array as $key => $value) {
130
        if ($value['timestamp'] < $timestamp) {
131
            unset($array[$key]);
132
        }
133
    }
134
135
    return $array;
136
}
137
138
//#######################################################################
139
// gym level from prestige value
140
// @param $prestige => int (mandatory)
141
//
142
// Return gym level
143
//#######################################################################
144
145
function gym_level($prestige)
146
{
147
    if (0 == $prestige) {
148
        $gym_level = 0;
149
    } elseif ($prestige < 2000) {
150
        $gym_level = 1;
151
    } elseif ($prestige < 4000) {
152
        $gym_level = 2;
153
    } elseif ($prestige < 8000) {
154
        $gym_level = 3;
155
    } elseif ($prestige < 12000) {
156
        $gym_level = 4;
157
    } elseif ($prestige < 16000) {
158
        $gym_level = 5;
159
    } elseif ($prestige < 20000) {
160
        $gym_level = 6;
161
    } elseif ($prestige < 30000) {
162
        $gym_level = 7;
163
    } elseif ($prestige < 40000) {
164
        $gym_level = 8;
165
    } elseif ($prestige < 50000) {
166
        $gym_level = 9;
167
    } else {
168
        $gym_level = 10;
169
    }
170
171
    return $gym_level;
172
}
173
174
//#######################################################################
175
// depth of array
176
// @param $arr     => array (mandatory)
177
//
178
// Retruns max depth of array
179
//#######################################################################
180
function get_depth($arr)
181
{
182
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
183
    $depth = 0;
184
    foreach ($it as $v) {
185
        $it->getDepth() > $depth && $depth = $it->getDepth();
186
    }
187
188
    return $depth;
189
}
190
191
//#######################################################################
192
// tree for at depth
193
// @param $trees     => array (mandatory)
194
// @param $depth => int (mandatory)
195
// @param $max_pokemon => int (mandatory)
196
// @param $currentDepth => int (optional)
197
//
198
// Return all pokemon with data at a certain tree depth
199
//#######################################################################
200
function get_tree_at_depth($trees, $depth, $max_pokemon, $currentDepth = 0)
201
{
202
    if ($depth == $currentDepth) { // Found depth
203
        return tree_remove_bellow($trees, $max_pokemon);
204
    } else { // Go deeper
205
        $arr = array();
206
        foreach ($trees as $temp) { // Go into all trees
207
            $tree = $temp->evolutions;
208
            $results = tree_remove_bellow(get_tree_at_depth($tree, $depth, $max_pokemon, $currentDepth + 1), $max_pokemon);
209
            $arr = tree_check_array($results, $arr, 1 == $depth - $currentDepth);
210
        }
211
212
        return $arr;
213
    }
214
}
215
216
//#######################################################################
217
// used in get_tree_at_depth
218
//#######################################################################
219
function tree_check_array($array_check, $array_add, $correct_arrow)
220
{
221
    $count = count($array_check);
222
    $i = 0;
223
    if (!is_null($array_check)) { // check if exists
224
        foreach ($array_check as $res) { // Check if above, equal or bellow center
225
            if (1 != $count && $correct_arrow) { // only add arrow once
226
                $num = $i / ($count - 1);
227
                if ($num < 0.5) {
228
                    $res->array_sufix = '_up';
229
                } elseif ($num > 0.5) {
230
                    $res->array_sufix = '_down';
231
                } else {
232
                    $res->array_sufix = '';
233
                }
234
            } elseif (!isset($res->array_sufix)) {
235
                $res->array_sufix = '';
236
            }
237
            $array_add[] = $res;
238
            ++$i;
239
        }
240
    }
241
242
    return $array_add;
243
}
244
245
//#######################################################################
246
// used in get_tree_at_depth
247
//#######################################################################
248
function tree_remove_bellow($tree, $max_pokemon)
249
{
250
    if (is_null($tree)) {
251
        return null;
252
    }
253
    $arr = array();
254
    foreach ($tree as $item) { // Check if above, equal or bellow center
255
        if ($item->id <= $max_pokemon) {
256
            $arr[] = $item;
257
        }
258
    }
259
260
    return $arr;
261
}
262
263
//#######################################################################
264
// generation
265
//#######################################################################
266
function generation($id)
267
{
268
    switch ($id) {
269
        case $id >= 1 && $id <= 151:
270
            return [1, 'Kanto'];
271
        case $id >= 152 && $id <= 251:
272
            return [2, 'Johto'];
273
        case $id >= 252 && $id <= 386:
274
            return [3, 'Hoenn'];
275
        case $id >= 387 && $id <= 493:
276
            return [4, 'Sinnoh'];
277
        case $id >= 494 && $id <= 649:
278
            return [5, 'Teselia'];
279
        case $id >= 650 && $id <= 721:
280
            return [6, 'Kalos'];
281
        case $id >= 722 && $id <= 802:
282
            return [7, 'Alola'];
283
    }
284
}
285
286
//#######################################################################
287
// check if point is inside porygon
288
//#######################################################################
289
function pointIsInsidePolygon($lat, $lng, $geos, $bounds)
290
{
291
    if ($lat >= $bounds['minlat'] && $lat <= $bounds['maxlat'] && $lng >= $bounds['minlon'] && $lng <= $bounds['maxlon']) {
292
        $intersections = 0;
293
        $geos_count = count($geos);
294
295
        for ($i = 1; $i < $geos_count; ++$i) {
296
            $geo1 = $geos[$i - 1];
297
            $geo2 = $geos[$i];
298
            if ($geo1['lng'] == $lng && $geo1['lat'] == $lat) { // On one of the coords
299
                return true;
300
            }
301
            if ($geo1['lng'] == $geo2['lng'] and $geo1['lng'] == $lng and $lat > min($geo1['lat'], $geo2['lat']) and $lat < max($geo1['lat'], $geo2['lat'])) { // Check if point is on an horizontal polygon boundary
302
                return true;
303
            }
304
            if ($lng > min($geo1['lng'], $geo2['lng']) and $lng <= max($geo1['lng'], $geo2['lng']) and $lat <= max($geo1['lat'], $geo2['lat']) and $geo1['lng'] != $geo2['lng']) {
305
                $xinters = ($lng - $geo1['lng']) * ($geo2['lat'] - $geo1['lat']) / ($geo2['lng'] - $geo1['lng']) + $geo1['lat'];
306
                if ($xinters == $lat) { // Check if point is on the polygon boundary (other than horizontal)
307
                    return true;
308
                }
309
                if ($geo1['lat'] == $geo2['lat'] || $lat <= $xinters) {
310
                    ++$intersections;
311
                }
312
            }
313
        }
314
        // If the number of edges we passed through is odd, then it's in the polygon.
315
        return 0 != $intersections % 2;
316
    } else {
317
        return false; // outside bounds
318
    }
319
}
320
321
//#######################################################################
322
// check if $boundsIn is inside (or equal to) $boundsOut
323
//#######################################################################
324
function polyIsInsidePolygon($geoIn, $boundsIn, $geoOut, $boundsOut)
325
{
326
    if ($boundsIn['minlat'] >= $boundsOut['minlat'] && $boundsIn['maxlat'] <= $boundsOut['maxlat'] && $boundsIn['minlon'] >= $boundsOut['minlon'] && $boundsIn['maxlon'] <= $boundsOut['maxlon']) {
327
        $insideCount = 0;
328
        foreach ($geoIn as $coord) {
329
            if (pointIsInsidePolygon($coord['lat'], $coord['lng'], $geoOut, $boundsOut)) {
330
                ++$insideCount;
331
            }
332
        }
333
334
        return $insideCount / count($geoIn) >= 0.95;
335
    } else {
336
        return false; // bounds outside
337
    }
338
}
339
340
//#######################################################################
341
// compine outer ways into porygon
342
//#######################################################################
343
function combineOuter($outers)
344
{
345
    $polygons = array();
346
    $index = 0;
347
    $count = 0;
348
    $maxCount = count($outers);
349
    while (0 != count($outers) && $count <= $maxCount) {
350
        ++$count;
351
        foreach ($outers as $key => $outer) {
352
            if (!isset($polygons[$index])) {
353
                $polygons[$index] = $outer;
354
                unset($outers[$key]);
355
            } else {
356
                $firstEle = $outer[0];
357
                $lastEle = $outer[count($outer) - 1];
358
                $firstElePoly = $polygons[$index][0];
359
                $lastElePoly = $polygons[$index][count($polygons[$index]) - 1];
360
                if ($firstEle == $lastElePoly) {
361
                    $polygons[$index] = array_merge($polygons[$index], $outer);
362
                    unset($outers[$key]);
363 View Code Duplication
                } elseif ($lastEle == $lastElePoly) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
364
                    $polygons[$index] = array_merge($polygons[$index], array_reverse($outer));
365
                    unset($outers[$key]);
366
                } elseif ($firstEle == $firstElePoly) {
367
                    $polygons[$index] = array_merge(array_reverse($outer), $polygons[$index]);
368
                    unset($outers[$key]);
369 View Code Duplication
                } elseif ($lastEle == $firstElePoly) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
370
                    $polygons[$index] = array_merge($outer, $polygons[$index]);
371
                    unset($outers[$key]);
372
                }
373
            }
374
375
            $firstElePoly = $polygons[$index][0];
376
            $lastElePoly = $polygons[$index][count($polygons[$index]) - 1];
377
            if ($firstElePoly == $lastElePoly) {
378
                ++$index;
379
            }
380
        }
381
    }
382
383
    return $polygons;
384
}
385
386
//#######################################################################
387
// HTML output for Menu and Submenu
388
//#######################################################################
389
function printMenuitems($menu, $level, $locales)
390
{
391
    if (isset($menu->locale)) {
392
        $locale = $menu->locale;
393
        $text = $locales->$locale;
394
    } elseif (isset($menu->text)) {
395
        $text = $menu->text;
396
    } else {
397
        $text = '';
398
    }
399
400
    switch ($menu->type) {
401
        case 'group':
402
            ?>
403
			
404
			<li>
405
			<a class="menu-label"><i class="fa <?= $menu->icon; ?>" aria-hidden="true"></i> <?= $text; ?></a>
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
406
			<ul class="dropdown">
407
			
408
			<?php
409
            foreach ($menu->members as $childmenu) {
410
                printMenuitems($childmenu, $level + 1, $locales);
411
            }
412
            ?>
413
			
414
			</ul>
415
			</li>
416
417
			<?php
418
            break;
419
420
        case 'link':
421
            ?>
422
423
			<li>
424
				<a href="<?= $menu->href; ?>" class="menu-label"><i class="fa <?= $menu->icon; ?>" aria-hidden="true"></i> <?= $text; ?></a>
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
425
			</li>
426
427
			<?php
428
            break;
429
430
        case 'link_external':
431
            ?>
432
433
			<li>
434
				<a href="<?= $menu->href; ?>" target="_blank" class="menu-label"><i class="fa <?= $menu->icon; ?>" aria-hidden="true"></i> <?= $menu->text; ?></a>
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
435
			</li>
436
437
			<?php
438
            break;
439
440
        case 'html':
441
            ?>
442
443
			<li> <?= $menu->value; ?> </li>
444
445
			<?php
446
            break;
447
    }
448
}
449
450
?>
451