Passed
Push — main ( a92fbe...d29be2 )
by N.
05:41 queued 01:24
created

t()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Finns ett värde inom ett intervall inklusivt?
7
 */
8
function in(int|float $värde, int|float $min, int|float $max): bool {
9 5
	return $värde >= $min && $värde <= $max;
10
}
11
12
/**
13
 * Max med kontroll för tom vektor.
14
 * @param int[]|float[] $vektor
15
 */
16
function ne_max(array $vektor): int|float {
17 5
	return $vektor === [] ? 0 : max($vektor);
18
}
19
20
/**
21
 * Min med kontroll för tom vektor.
22
 * @param int[]|float[] $vektor
23
 */
24
function ne_min(array $vektor): int|float {
25 5
	return $vektor === [] ? 0 : min($vektor);
26
}
27
28
/**
29
 * Plocka ut och kontrollera variabler från requeststräng.
30
 * @return array<int, int>
31
 */
32
function extrahera(): array {
33 1
	$ret = [];
34 1
	$opts = isset($_REQUEST['i']) ? $_REQUEST : (array) getopt("i:j:k:l:");
35
36 1
	foreach ($opts as $opt) {
37 1
		$ret[] = is_string($opt) ? intval($opt) : 0;
38
	}
39 1
	return $ret;
40
}
41
42
/**
43
 * a1·b1 + a2·b2 + …
44
 * @param array<int|float> $vektor1
45
 * @param array<int|float> $vektor2
46
 */
47
function vektorprodukt(array $vektor1, array $vektor2): int|float {
48 3
	return array_sum(
49 3
		array_map(
50 3
			fn (int|float $tal1, int|float $tal2): int|float =>
51 3
			$tal1 * $tal2,
52 3
			$vektor1,
53 3
			$vektor2
54 3
		)
55 3
	);
56
}
57
58
/**
59
 * Sträng med «svenska» decimaler till korrekt flyttalsform.
60
 */
61
function flyttal(string $str): float {
62 1
	return floatval(str_replace(',', '.', $str));
63
}
64
65
/**
66
 * Rendera tal som procentvärde.
67
 */
68
function procenttal(float $värde): int {
69 5
	return intval(100 * round($värde, 2));
70
}
71
72
/**
73
 * Tabulera indrag i HTML.
74
 */
75
function t(int $n, string $text): string {
76 2
	return str_repeat("\t", $n) . $text . PHP_EOL;
77
}
78
79
/**
80
 * Extrahera spelstopp från JSON-data.
81
 * @return string[]
82
 */
83
function spelstopp(string $utc): array {
84 2
	$datum = '';
85 2
	$spelstopp = '';
86 2
	$dag = '';
87
88 2
	if ($utc != null) {
89 2
		$stopp = explode('T', $utc);
90 2
		$datum = $stopp[0];
91 2
		$format = new IntlDateFormatter(pattern:'EEEE', locale:'sv_SE', dateType:IntlDateFormatter::FULL, timeType:IntlDateFormatter::NONE);
92 2
		$dag = ucfirst((string) $format->format((int) strtotime($datum)));
93 2
		$spelstopp = explode('+', $stopp[1])[0];
94
	}
95 2
	return [$datum, $spelstopp, $dag];
96
}
97
98
/**
99
 * Hämta JSON-objekt från en server.
100
 */
101
function hämta_objekt(string $url): ?object {
102
	// return json_decode((string) file_get_contents(JSONFIL));
103 1
	$json = null;
104 1
	$handle = curl_init($url);
105 1
	if ($handle !== false && $url !== '') {
106
		curl_setopt($handle, CURLOPT_HEADER, false);
107
		curl_setopt($handle, CURLOPT_USERAGENT, USERAGENT);
0 ignored issues
show
Bug introduced by
The constant USERAGENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
108
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
109
		curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
110
		curl_setopt($handle, CURLOPT_URL, $url);
111
		$json = curl_exec($handle);
112
		unset($handle);
113
	}
114
115 1
	if ($json) {
116
		file_put_contents(JSONFIL, $json);
0 ignored issues
show
Bug introduced by
The constant JSONFIL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
117
	}
118
119 1
	$retur = is_string($json) ? json_decode($json) : null;
120 1
	return (!is_object($retur)) ? null : $retur;
121
}
122