Failed Conditions
Pull Request — master (#363)
by Florian
07:09
created

functions.php ➔ get_depth()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 8
rs 9.4285
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
14
	// Set up our variables.
15
	$minute_in_seconds = 60;
16
	$hour_in_seconds   = $minute_in_seconds * 60;
17
	$day_in_seconds	   = $hour_in_seconds * 24;
18
	$week_in_seconds   = $day_in_seconds * 7;
19
	$month_in_seconds  = $day_in_seconds * 30;
20
	$year_in_seconds   = $day_in_seconds * 365;
21
22
	// current time
23
	$now = time();
24
25
	// Calculate the time difference between the current time reference point and the timestamp we're comparing.
26
	// The difference is defined negative, when in the future.
27
	$time_difference = $now - $timestamp;
28
29
	// Calculate the time ago using the smallest applicable unit.
30
	if ($time_difference < $hour_in_seconds) {
31
		$difference_value = abs(round($time_difference / $minute_in_seconds));
32
		$difference_label = 'MINUTE';
33
	} elseif ($time_difference < $day_in_seconds) {
34
		$difference_value = abs(round($time_difference / $hour_in_seconds));
35
		$difference_label = 'HOUR';
36
	} elseif ($time_difference < $week_in_seconds) {
37
		$difference_value = abs(round($time_difference / $day_in_seconds));
38
		$difference_label = 'DAY';
39
	} elseif ($time_difference < $month_in_seconds) {
40
		$difference_value = abs(round($time_difference / $week_in_seconds));
41
		$difference_label = 'WEEK';
42
	} elseif ($time_difference < $year_in_seconds) {
43
		$difference_value = abs(round($time_difference / $month_in_seconds));
44
		$difference_label = 'MONTH';
45
	} else {
46
		$difference_value = abs(round($time_difference / $year_in_seconds));
47
		$difference_label = 'YEAR';
48
	}
49
50
	// plural
51
	if ($difference_value != 1) {
52
		$difference_label = $difference_label.'S';
53
	}
54
55
	if ($time_difference <= 0) {
56
		// Present
57
		return sprintf($locales->TIME_LEFT, $difference_value.' '.$locales->$difference_label);
58
	} else {
59
		// Past
60
		return sprintf($locales->TIME_AGO, $difference_value.' '.$locales->$difference_label);
61
	}
62
}
63
64
65
########################################################################
66
// Percent calculator
67
// @param $val		=> int (mandatory)
68
// @param $val_total	=> int (mandatory)
69
//
70
// Return pourcent from total
71
########################################################################
72
73
function percent($val, $val_total)
74
{
75
	$count1 = $val_total / $val;
76
	$count2 = $count1 * 100;
77
78
	$count = number_format($count2, 0);
79
80
	return $count;
81
}
82
83
84
########################################################################
85
// File version (unix timestamp)
86
// @param $url		=> string (mandatory)
87
//
88
// Return $url with last_modified unix timestamp before suffix
89
########################################################################
90
91
function auto_ver($url)
92
{
93
	if (is_file(SYS_PATH.'/'.$url)) {
94
		$path = pathinfo($url);
95
		$ver = '.'.filemtime(SYS_PATH.'/'.$url).'.';
96
		echo $path['dirname'].'/'.preg_replace('/\.(css|js|json)$/', $ver."$1", $path['basename']);
97
	} else {
98
		echo $url;
99
	}
100
}
101
102
103
########################################################################
104
// File age in secs
105
// @param $filepath     => string (mandatory)
106
//
107
// Return file age of file in secs, PHP_INT_MAX if file doesn't exist
108
########################################################################
109
110
function file_update_ago($filepath)
111
{
112
	if (is_file($filepath)) {
113
		$filemtime = filemtime($filepath);
114
		$now = time();
115
		$diff = $now - $filemtime;
116
		return $diff;
117
	}
118
	// file doesn't exist yet!
119
	return PHP_INT_MAX;
120
}
121
122
123
########################################################################
124
// Only keep data after $timestamp in $array (compared to 'timestamp' key)
125
// @param $array     => array (mandatory)
126
// @param $timestamp => int (mandatory)
127
//
128
// Return trimmed array
129
########################################################################
130
131
function trim_stats_json($array, $timestamp)
132
{
133
	foreach ($array as $key => $value) {
134
		if ($value['timestamp'] < $timestamp) {
135
			unset($array[$key]);
136
		}
137
	}
138
	return $array;
139
}
140
141
142
########################################################################
143
// gym level from prestige value
144
// @param $prestige => int (mandatory)
145
//
146
// Return gym level
147
########################################################################
148
149
function gym_level($prestige)
150
{
151
	if ($prestige == 0) {
152
		$gym_level = 0;
153
	} elseif ($prestige < 2000) {
154
		$gym_level = 1;
155
	} elseif ($prestige < 4000) {
156
		$gym_level = 2;
157
	} elseif ($prestige < 8000) {
158
		$gym_level = 3;
159
	} elseif ($prestige < 12000) {
160
		$gym_level = 4;
161
	} elseif ($prestige < 16000) {
162
		$gym_level = 5;
163
	} elseif ($prestige < 20000) {
164
		$gym_level = 6;
165
	} elseif ($prestige < 30000) {
166
		$gym_level = 7;
167
	} elseif ($prestige < 40000) {
168
		$gym_level = 8;
169
	} elseif ($prestige < 50000) {
170
		$gym_level = 9;
171
	} else {
172
		$gym_level = 10;
173
	}
174
175
	return $gym_level;
176
}
177
178
########################################################################
179
// depth of array
180
// @param $arr     => array (mandatory)
181
//
182
// Retruns max depth of array
183
########################################################################
184
function get_depth($arr) {
185
	$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
186
	$depth = 0;
187
	foreach ($it as $v) {
188
		$it->getDepth() > $depth && $depth = $it->getDepth();
189
	}
190
	return $depth;
191
}
192
193
########################################################################
194
// tree for at depth
195
// @param $trees     => array (mandatory)
196
// @param $depth => int (mandatory)
197
// @param $max_pokemon => int (mandatory)
198
// @param $currentDepth => int (optional)
199
//
200
// Return all pokemon with data at a certain tree depth
201
########################################################################
202
function get_tree_at_depth($trees, $depth, $max_pokemon, $currentDepth = 0) {
203
	if ($depth == $currentDepth) { // Found depth
204
		return tree_remove_bellow($trees, $max_pokemon);
205
	} else { // Go deeper
206
		$arr = array();
207
		foreach ($trees as $temp) { // Go into all trees
208
			$tree = $temp->evolutions;
209
			$results = tree_remove_bellow(get_tree_at_depth($tree, $depth, $max_pokemon, $currentDepth + 1), $max_pokemon);
210
			$arr = tree_check_array($results, $arr, $depth - $currentDepth == 1);
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
	$count = count($array_check);
221
	$i = 0;
222
	if (!is_null($array_check)) { // check if exists
223
		foreach ($array_check as $res) { // Check if above, equal or bellow center
224
			if ($count != 1 && $correct_arrow) { // only add arrow once
225
				$num = $i / ($count - 1);
226
				if ($num < 0.5) {
227
					$res->array_sufix = "_up";
228
				} elseif ($num > 0.5) {
229
					$res->array_sufix = "_down";
230
				} else {
231
					$res->array_sufix = "";
232
				}
233
			} else if (!isset($res->array_sufix)) {
234
				$res->array_sufix = "";
235
			}
236
			$array_add[] = $res;
237
			$i++;
238
		}
239
	}
240
	return $array_add;
241
}
242
243
########################################################################
244
// used in get_tree_at_depth
245
########################################################################
246
function tree_remove_bellow($tree, $max_pokemon)
247
{
248
	if (is_null($tree)) {
249
		return null;
250
	}
251
	$arr = array();
252
	foreach ($tree as $item) { // Check if above, equal or bellow center
253
		if ($item->id <= $max_pokemon) {
254
			$arr[] = $item;
255
		}
256
	}
257
	return $arr;
258
}
259
260
########################################################################
261
// generation
262
########################################################################
263
function generation($id)
264
{
265
	switch ($id) {
266
		case ($id >= 1 && $id <= 151):
267
			return [1, "Kanto"];
268
		case ($id >= 152 && $id <= 251):
269
			return [2, "Johto"];
270
		case ($id >= 252 && $id <= 386):
271
			return [3, "Hoenn"];
272
		case ($id >= 387 && $id <= 493):
273
			return [4, "Sinnoh"];
274
		case ($id >= 494 && $id <= 649):
275
			return [5, "Teselia"];
276
		case ($id >= 650 && $id <= 721):
277
			return [6, "Kalos"];
278
		case ($id >= 722 && $id <= 802):
279
			return [7, "Alola"];
280
	}
281
}
282
283
########################################################################
284
// check if point is inside porygon
285
########################################################################
286
function pointIsInsidePolygon($lat, $lng, $geos) {
287
	
288
	$intersections = 0;
289
	$geos_count = count($geos);
290
291
	for ($i=1; $i < $geos_count; $i++) {
292
		$geo1 = $geos[$i-1];
293
		$geo2 = $geos[$i];
294
		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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
295
			return "boundary";
296
		}
297
		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']) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
298
			$xinters = ($lng - $geo1['lng']) * ($geo2['lat'] - $geo1['lat']) / ($geo2['lng'] - $geo1['lng']) + $geo1['lat'];
299
			if ($xinters == $lat) { // Check if point is on the polygon boundary (other than horizontal)
300
				return "boundary";
301
			}
302
			if ($geo1['lat'] == $geo2['lat'] || $lat <= $xinters) {
303
				$intersections++;
304
			}
305
		}
306
	}
307
	// If the number of edges we passed through is odd, then it's in the polygon. 
308
	if ($intersections % 2 != 0) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return $intersections % 2 != 0;.
Loading history...
309
		return true;
310
	} else {
311
		return false;
312
	}
313
}
314