Passed
Push — main ( e55150...6ce6d7 )
by Will
02:49
created

other   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 29
c 1
b 0
f 0
dl 0
loc 50
ccs 28
cts 31
cp 0.9032
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B get() 0 43 7
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
/**
6
 * @phpstan-import-type props from config
7
 */
8
class other {
9
10
	/**
11
	 * Generates a configuration array for matching other
12
	 * 
13
	 * @return props An array with keys representing the string to match, and a value of an array containing parsing and output settings
14
	 */
15 2
	public static function get() : array {
16 1
		return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('{' => new ...on(...) { /* ... */ })) returns the type array<string,hexydec\agentzero\props> which is incompatible with the documented return type hexydec\agentzero\props.
Loading history...
17 1
			'{' => new props('any', function (string $value) : ?array {
18 2
				if (($start = \mb_strpos($value, '{')) === false) {
19
20 2
				} elseif (($end = \mb_strpos($value, '}', $start)) !== false) {
21 1
					$json = \mb_substr($value, $start, $end - $start + 1);
22
23
					// decode JSON
24 1
					if (($data = \json_decode($json, true)) !== null) {
25 1
						$cat = [];
26 1
						$data = \array_change_key_case((array) $data);
27
28
						// special case for chrome - browser
29 1
						if ($data['app'] === 'com.google.chrome.ios') {
30
							$cat['browser'] = 'Chrome';
31
							$cat['browserversion'] = $data['appversion'] ?? null;
32
							unset($data['app'], $data['appversion']);
33
						}
34
35
						// map values
36 1
						$map = [
37 1
							'os' => 'platform',
38 1
							'osversion' => 'platformversion',
39 1
							'isdarktheme' => 'darkmode'
40 1
						];
41 1
						$fields = ['os', 'osversion', 'platform', 'platformversion', 'app', 'appversion', 'isdarktheme'];
42 1
						$values = [
43 1
							'com.google.GoogleMobile' => 'Google',
44 1
							'com.google.android.gms' => 'Google',
45 1
							'com.google.Gmail' => 'Gmail',
46 1
							'com.google.photos' => 'Google Photos',
47 1
							'com.google.ios.youtube' => 'YouTube'
48 1
						];
49 1
						foreach ($fields AS $item) {
50 1
							if (isset($data[$item])) {
51 1
								$cat[$map[$item] ?? $item] = $values[$data[$item]] ?? $data[$item];
52
							}
53
						}
54 1
						return $cat;
55
					}
56
				}
57 1
				return null;
58 1
			})
59 1
		];
60
	}
61
}