functions.php ➔ tree_remove_bellow()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
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
function time_ago_day($timestamp, $locales) {
64
    $spawn = new DateTime($timestamp);
65
    $now = new DateTime();
66
67
    $days = $now->diff($spawn)->format("%a");
68
    if ($days == 0) {
69
        return $locales->TODAY;
70
    } elseif ($days == 1) {
71
        return $locales->YESTERDAY;
72
    } else {
73
        return sprintf($locales->TIME_AGO, $days.' '.$locales->DAYS);
74
    }
75
76
    return $days;
0 ignored issues
show
Unused Code introduced by
return $days; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
77
}
78
79
//#######################################################################
80
// Percent calculator
81
// @param $val		=> int (mandatory)
82
// @param $val_total	=> int (mandatory)
83
//
84
// Return pourcent from total
85
//#######################################################################
86
87
function percent($val, $val_total)
88
{
89
    $count1 = $val_total / $val;
90
    $count2 = $count1 * 100;
91
92
    $count = number_format($count2, 0);
93
94
    return $count;
95
}
96
97
//#######################################################################
98
// File version (unix timestamp)
99
// @param $url		=> string (mandatory)
100
//
101
// Return $url with last_modified unix timestamp before suffix
102
//#######################################################################
103
104
function auto_ver($url)
105
{
106
    if (is_file(SYS_PATH.'/'.$url)) {
107
        $path = pathinfo($url);
108
        $ver = '.'.filemtime(SYS_PATH.'/'.$url).'.';
109
        echo $path['dirname'].'/'.preg_replace('/\.(css|js|json)$/', $ver.'$1', $path['basename']);
110
    } else {
111
        echo $url;
112
    }
113
}
114
115
//#######################################################################
116
// File age in secs
117
// @param $filepath     => string (mandatory)
118
//
119
// Return file age of file in secs, PHP_INT_MAX if file doesn't exist
120
//#######################################################################
121
122
function file_update_ago($filepath)
123
{
124
    if (is_file($filepath)) {
125
        $filemtime = filemtime($filepath);
126
        $now = time();
127
        $diff = $now - $filemtime;
128
129
        return $diff;
130
    }
131
    // file doesn't exist yet!
132
    return PHP_INT_MAX;
133
}
134
135
//#######################################################################
136
// Only keep data after $timestamp in $array (compared to 'timestamp' key)
137
// @param $array     => array (mandatory)
138
// @param $timestamp => int (mandatory)
139
//
140
// Return trimmed array
141
//#######################################################################
142
143
function trim_stats_json($array, $timestamp)
144
{
145
    foreach ($array as $key => $value) {
146
        if ($value['timestamp'] < $timestamp) {
147
            unset($array[$key]);
148
        }
149
    }
150
151
    return $array;
152
}
153
154
//#######################################################################
155
// gym level from prestige value
156
// @param $prestige => int (mandatory)
157
//
158
// Return gym level
159
//#######################################################################
160
161
function gym_level($prestige)
162
{
163
    if (0 == $prestige) {
164
        $gym_level = 0;
165
    } elseif ($prestige < 2000) {
166
        $gym_level = 1;
167
    } elseif ($prestige < 4000) {
168
        $gym_level = 2;
169
    } elseif ($prestige < 8000) {
170
        $gym_level = 3;
171
    } elseif ($prestige < 12000) {
172
        $gym_level = 4;
173
    } elseif ($prestige < 16000) {
174
        $gym_level = 5;
175
    } elseif ($prestige < 20000) {
176
        $gym_level = 6;
177
    } elseif ($prestige < 30000) {
178
        $gym_level = 7;
179
    } elseif ($prestige < 40000) {
180
        $gym_level = 8;
181
    } elseif ($prestige < 50000) {
182
        $gym_level = 9;
183
    } else {
184
        $gym_level = 10;
185
    }
186
187
    return $gym_level;
188
}
189
190
//#######################################################################
191
// depth of array
192
// @param $arr     => array (mandatory)
193
//
194
// Retruns max depth of array
195
//#######################################################################
196
function get_depth($arr)
197
{
198
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
199
    $depth = 0;
200
    foreach ($it as $v) {
201
        $it->getDepth() > $depth && $depth = $it->getDepth();
202
    }
203
204
    return $depth;
205
}
206
207
//#######################################################################
208
// tree for at depth
209
// @param $trees     => array (mandatory)
210
// @param $depth => int (mandatory)
211
// @param $max_pokemon => int (mandatory)
212
// @param $currentDepth => int (optional)
213
//
214
// Return all pokemon with data at a certain tree depth
215
//#######################################################################
216
function get_tree_at_depth($trees, $depth, $max_pokemon, $currentDepth = 0)
217
{
218
    if ($depth == $currentDepth) { // Found depth
219
        return tree_remove_bellow($trees, $max_pokemon);
220
    } else { // Go deeper
221
        $arr = array();
222
        foreach ($trees as $temp) { // Go into all trees
223
            $tree = $temp->evolutions;
224
            $results = tree_remove_bellow(get_tree_at_depth($tree, $depth, $max_pokemon, $currentDepth + 1), $max_pokemon);
225
            $arr = tree_check_array($results, $arr, 1 == $depth - $currentDepth);
226
        }
227
228
        return $arr;
229
    }
230
}
231
232
//#######################################################################
233
// used in get_tree_at_depth
234
//#######################################################################
235
function tree_check_array($array_check, $array_add, $correct_arrow)
236
{
237
    $count = count($array_check);
238
    $i = 0;
239
    if (!is_null($array_check)) { // check if exists
240
        foreach ($array_check as $res) { // Check if above, equal or bellow center
241
            if (1 != $count && $correct_arrow) { // only add arrow once
242
                $num = $i / ($count - 1);
243
                if ($num < 0.5) {
244
                    $res->array_sufix = '_up';
245
                } elseif ($num > 0.5) {
246
                    $res->array_sufix = '_down';
247
                } else {
248
                    $res->array_sufix = '';
249
                }
250
            } elseif (!isset($res->array_sufix)) {
251
                $res->array_sufix = '';
252
            }
253
            $array_add[] = $res;
254
            ++$i;
255
        }
256
    }
257
258
    return $array_add;
259
}
260
261
//#######################################################################
262
// used in get_tree_at_depth
263
//#######################################################################
264
function tree_remove_bellow($tree, $max_pokemon)
265
{
266
    if (is_null($tree)) {
267
        return null;
268
    }
269
    $arr = array();
270
    foreach ($tree as $item) { // Check if above, equal or bellow center
271
        if ($item->id <= $max_pokemon) {
272
            $arr[] = $item;
273
        }
274
    }
275
276
    return $arr;
277
}
278
279
//#######################################################################
280
// generation
281
//#######################################################################
282
function generation($id)
283
{
284
    switch ($id) {
285
        case $id >= 1 && $id <= 151:
286
            return [1, 'Kanto'];
287
        case $id >= 152 && $id <= 251:
288
            return [2, 'Johto'];
289
        case $id >= 252 && $id <= 386:
290
            return [3, 'Hoenn'];
291
        case $id >= 387 && $id <= 493:
292
            return [4, 'Sinnoh'];
293
        case $id >= 494 && $id <= 649:
294
            return [5, 'Teselia'];
295
        case $id >= 650 && $id <= 721:
296
            return [6, 'Kalos'];
297
        case $id >= 722 && $id <= 802:
298
            return [7, 'Alola'];
299
    }
300
}
301
302
//#######################################################################
303
// check if point is inside porygon
304
//#######################################################################
305
function pointIsInsidePolygon($lat, $lng, $geos, $bounds)
306
{
307
    if ($lat >= $bounds['minlat'] && $lat <= $bounds['maxlat'] && $lng >= $bounds['minlon'] && $lng <= $bounds['maxlon']) {
308
        $intersections = 0;
309
        $geos_count = count($geos);
310
311
        for ($i = 1; $i < $geos_count; ++$i) {
312
            $geo1 = $geos[$i - 1];
313
            $geo2 = $geos[$i];
314
            if ($geo1['lng'] == $lng && $geo1['lat'] == $lat) { // On one of the coords
315
                return true;
316
            }
317
            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
318
                return true;
319
            }
320
            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']) {
321
                $xinters = ($lng - $geo1['lng']) * ($geo2['lat'] - $geo1['lat']) / ($geo2['lng'] - $geo1['lng']) + $geo1['lat'];
322
                if ($xinters == $lat) { // Check if point is on the polygon boundary (other than horizontal)
323
                    return true;
324
                }
325
                if ($geo1['lat'] == $geo2['lat'] || $lat <= $xinters) {
326
                    ++$intersections;
327
                }
328
            }
329
        }
330
        // If the number of edges we passed through is odd, then it's in the polygon.
331
        return 0 != $intersections % 2;
332
    } else {
333
        return false; // outside bounds
334
    }
335
}
336
337
//#######################################################################
338
// check if $boundsIn is inside (or equal to) $boundsOut
339
//#######################################################################
340
function polyIsInsidePolygon($geoIn, $boundsIn, $geoOut, $boundsOut)
341
{
342
    if ($boundsIn['minlat'] >= $boundsOut['minlat'] && $boundsIn['maxlat'] <= $boundsOut['maxlat'] && $boundsIn['minlon'] >= $boundsOut['minlon'] && $boundsIn['maxlon'] <= $boundsOut['maxlon']) {
343
        $insideCount = 0;
344
        foreach ($geoIn as $coord) {
345
            if (pointIsInsidePolygon($coord['lat'], $coord['lng'], $geoOut, $boundsOut)) {
346
                ++$insideCount;
347
            }
348
        }
349
350
        return $insideCount / count($geoIn) >= 0.95;
351
    } else {
352
        return false; // bounds outside
353
    }
354
}
355
356
//#######################################################################
357
// compine outer ways into porygon
358
//#######################################################################
359
function combineOuter($outers)
360
{
361
    $polygons = array();
362
    $index = 0;
363
    $count = 0;
364
    $maxCount = count($outers);
365
    while (0 != count($outers) && $count <= $maxCount) {
366
        ++$count;
367
        foreach ($outers as $key => $outer) {
368
            if (!isset($polygons[$index])) {
369
                $polygons[$index] = $outer;
370
                unset($outers[$key]);
371
            } else {
372
                $firstEle = $outer[0];
373
                $lastEle = $outer[count($outer) - 1];
374
                $firstElePoly = $polygons[$index][0];
375
                $lastElePoly = $polygons[$index][count($polygons[$index]) - 1];
376
                if ($firstEle == $lastElePoly) {
377
                    $polygons[$index] = array_merge($polygons[$index], $outer);
378
                    unset($outers[$key]);
379 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...
380
                    $polygons[$index] = array_merge($polygons[$index], array_reverse($outer));
381
                    unset($outers[$key]);
382
                } elseif ($firstEle == $firstElePoly) {
383
                    $polygons[$index] = array_merge(array_reverse($outer), $polygons[$index]);
384
                    unset($outers[$key]);
385 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...
386
                    $polygons[$index] = array_merge($outer, $polygons[$index]);
387
                    unset($outers[$key]);
388
                }
389
            }
390
391
            $firstElePoly = $polygons[$index][0];
392
            $lastElePoly = $polygons[$index][count($polygons[$index]) - 1];
393
            if ($firstElePoly == $lastElePoly) {
394
                ++$index;
395
            }
396
        }
397
    }
398
399
    return $polygons;
400
}
401
402
//#######################################################################
403
// HTML output for Menu and Submenu
404
//#######################################################################
405
function printMenuitems($menu, $level, $locales)
406
{
407
    if (isset($menu->locale)) {
408
        $locale = $menu->locale;
409
        $text = $locales->$locale;
410
    } elseif (isset($menu->text)) {
411
        $text = $menu->text;
412
    } else {
413
        $text = '';
414
    }
415
416
    switch ($menu->type) {
417
        case 'group':
418
            ?>
419
			
420
			<li>
421
			<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...
422
			<ul class="dropdown">
423
			
424
			<?php
425
            foreach ($menu->members as $childmenu) {
426
                printMenuitems($childmenu, $level + 1, $locales);
427
            }
428
            ?>
429
			
430
			</ul>
431
			</li>
432
433
			<?php
434
            break;
435
436
        case 'link':
437
            ?>
438
439
			<li>
440
				<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...
441
			</li>
442
443
			<?php
444
            break;
445
446
        case 'link_external':
447
            ?>
448
449
			<li>
450
				<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...
451
			</li>
452
453
			<?php
454
            break;
455
456
        case 'html':
457
            ?>
458
459
			<li> <?= $menu->value; ?> </li>
460
461
			<?php
462
            break;
463
    }
464
}
465
466
?>
467