Passed
Push — main ( bcdb57...ad9cc7 )
by Will
03:05
created

other::get()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8.0877

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 26
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 41
ccs 24
cts 27
cp 0.8889
crap 8.0877
rs 8.4444
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
class other {
6
7
	/**
8
	 * Generates a configuration array for matching other
9
	 * 
10
	 * @return array<string,props> An array with keys representing the string to match, and values a props object defining how to generate the match and which properties to set
11
	 */
12 2
	public static function get() : array {
13 1
		return [
14 1
			'{' => new props('any', function (string $value) : ?array {
15 2
				if (($start = \mb_strpos($value, '{')) === false) {
16
17 2
				} elseif (($end = \mb_strpos($value, '}', $start)) !== false) {
18 1
					$json = \mb_substr($value, $start, $end - $start + 1);
19
20
					// decode JSON
21 1
					if (($data = \json_decode($json, true)) !== null) {
22 1
						$cat = [];
23 1
						$data = \array_change_key_case((array) $data);
24
25
						// special case for chrome - browser
26 1
						if ($data['app'] === 'com.google.chrome.ios') {
27
							$cat['browser'] = 'Chrome';
28
							$cat['browserversion'] = $data['appversion'] ?? null;
29
							unset($data['app'], $data['appversion']);
30
						}
31
32
						// map values
33 1
						$map = [
34 1
							'os' => 'platform',
35 1
							'osversion' => 'platformversion',
36 1
							'isdarktheme' => 'darkmode'
37 1
						];
38 1
						$fields = ['os', 'osversion', 'platform', 'platformversion', 'app', 'appversion', 'isdarktheme'];
39 1
						foreach ($fields AS $item) {
40 1
							if (isset($data[$item])) {
41 1
								if ($item === 'app') {
42 1
									$cat['app'] = apps::getApp($data[$item]);
43 1
									$cat['appname'] = $data[$item];
44
								} else {
45 1
									$cat[$map[$item] ?? $item] = $data[$item];
46
								}
47
							}
48
						}
49 1
						return $cat;
50
					}
51
				}
52 1
				return null;
53 1
			})
54 1
		];
55
	}
56
}