Failed Conditions
Pull Request — master (#371)
by
unknown
02:05
created

functions.php ➔ pointIsInsidePolygon()   D

Complexity

Conditions 20
Paths 12

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
nc 12
nop 4
dl 0
loc 35
rs 4.1666
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
        if (0 != $intersections % 2) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return 0 != $intersections % 2;.
Loading history...
316
            return true;
317
        } else {
318
            return false;
319
        }
320
    } else {
321
        return false; // outside bounds
322
    }
323
}
324
325
//#######################################################################
326
// check if $boundsIn is inside (or equal to) $boundsOut
327
//#######################################################################
328
function polyIsInsidePolygon($geoIn, $boundsIn, $geoOut, $boundsOut)
329
{
330
    if ($boundsIn['minlat'] >= $boundsOut['minlat'] && $boundsIn['maxlat'] <= $boundsOut['maxlat'] && $boundsIn['minlon'] >= $boundsOut['minlon'] && $boundsIn['maxlon'] <= $boundsOut['maxlon']) {
331
        $insideCount = 0;
332
        foreach ($geoIn as $coord) {
333
            if (pointIsInsidePolygon($coord['lat'], $coord['lng'], $geoOut, $boundsOut)) {
334
                ++$insideCount;
335
            }
336
        }
337
338
        return $insideCount / count($geoIn) >= 0.95;
339
    } else {
340
        return false; // bounds outside
341
    }
342
}
343
344
//#######################################################################
345
// compine outer ways into porygon
346
//#######################################################################
347
function combineOuter($outers)
348
{
349
    $polygons = array();
350
    $index = 0;
351
    $count = 0;
352
    $maxCount = count($outers);
353
    while (0 != count($outers) && $count <= $maxCount) {
354
        ++$count;
355
        foreach ($outers as $key => $outer) {
356
            if (!isset($polygons[$index])) {
357
                $polygons[$index] = $outer;
358
                unset($outers[$key]);
359
            } else {
360
                $firstEle = $outer[0];
361
                $lastEle = $outer[count($outer) - 1];
362
                $firstElePoly = $polygons[$index][0];
363
                $lastElePoly = $polygons[$index][count($polygons[$index]) - 1];
364
                if ($firstEle == $lastElePoly) {
365
                    $polygons[$index] = array_merge($polygons[$index], $outer);
366
                    unset($outers[$key]);
367 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...
368
                    $polygons[$index] = array_merge($polygons[$index], array_reverse($outer));
369
                    unset($outers[$key]);
370
                } elseif ($firstEle == $firstElePoly) {
371
                    $polygons[$index] = array_merge(array_reverse($outer), $polygons[$index]);
372
                    unset($outers[$key]);
373 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...
374
                    $polygons[$index] = array_merge($outer, $polygons[$index]);
375
                    unset($outers[$key]);
376
                }
377
            }
378
379
            $firstElePoly = $polygons[$index][0];
380
            $lastElePoly = $polygons[$index][count($polygons[$index]) - 1];
381
            if ($firstElePoly == $lastElePoly) {
382
                ++$index;
383
            }
384
        }
385
    }
386
387
    return $polygons;
388
}
389
390
//#######################################################################
391
// HTML output for Menu and Submenu
392
//#######################################################################
393
function printMenuitems($menu, $level, $locales)
394
{
395
    if (isset($menu->locale)) {
396
        $locale = $menu->locale;
397
        $text = $locales->$locale;
398
    } elseif (isset($menu->text)) {
399
        $text = $menu->text;
400
    } else {
401
        $text = '';
402
    }
403
404
    switch ($menu->type) {
405
        case 'group':
406
            ?>
407
			
408
			<li>
409
			<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...
410
			<ul class="dropdown">
411
			
412
			<?php
413
            foreach ($menu->members as $childmenu) {
414
                printMenuitems($childmenu, $level + 1, $locales);
415
            }
416
            ?>
417
			
418
			</ul>
419
			</li>
420
421
			<?php
422
            break;
423
424
        case 'link':
425
            ?>
426
427
			<li>
428
				<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...
429
			</li>
430
431
			<?php
432
            break;
433
434
        case 'link_external':
435
            ?>
436
437
			<li>
438
				<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...
439
			</li>
440
441
			<?php
442
            break;
443
444
        case 'html':
445
            ?>
446
447
			<li> <?= $menu->value; ?> </li>
448
449
			<?php
450
            break;
451
    }
452
}
453
454
?>
455