Passed
Push — main ( c4a646...dc1874 )
by Will
02:03
created

apps   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 128
c 1
b 0
f 0
dl 0
loc 187
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C get() 0 185 12
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
class apps {
6
7
	public static function get() {
8
		$fn = [
9
			'appslash' => function (string $value) : ?array {
10
				if (!\str_starts_with($value, 'AppleWebKit') && !\str_contains($value, '://')) {
11
					$parts = \explode('/', $value, 2);
12
					return [
13
						'app' => $parts[0],
14
						'appversion' => $parts[1] ?? null
15
					];
16
				}
17
				return null;
18
			}
19
		];
20
		return [
21
			'com.google.android.apps.' => [
22
				'match' => 'any',
23
				'categories' => $fn['appslash']
24
			],
25
			'Instagram' => [
26
				'match' => 'any',
27
				'categories' => function (string $value, int $i, array $tokens) : array {
28
					$data = [
29
						'app' => 'Instagram',
30
						'appversion' => \explode(' ', $value, 3)[1] ?? null
31
					];
32
					foreach (\array_slice($tokens, $i + 1) AS $item) {
33
						if (\str_starts_with($item, 'scale=')) {
34
							$data['density'] = \floatval(\mb_substr($item, 6));
35
						} elseif (\str_ends_with($item, 'dpi')) {
36
							$data['dpi'] = \intval(\mb_substr($item, 0, -3));
37
						} elseif (\str_contains($item, 'x') && \strspn($item, '0123456789x') === \strlen($item)) {
38
							list($data['width'], $data['height']) = \array_map('intval', \explode('x', $item, 2));
39
						}
40
					}
41
					return $data;
42
				}
43
			],
44
			'GSA/' => [
45
				'match' => 'any',
46
				'categories' => $fn['appslash']
47
			],
48
			'DuckDuckGo/' => [
49
				'match' => 'start',
50
				'categories' => $fn['appslash']
51
			],
52
			'FlipboardProxy/' => [
53
				'match' => 'start',
54
				'categories' => $fn['appslash']
55
			],
56
			'Google-Read-Aloud' => [
57
				'match' => 'exact',
58
				'categories' => [
59
					'type' => 'human',
60
					'app' => 'Google-Read-Aloud'
61
				]
62
			],
63
64
			// special parser for Facebook app because it is completely different to any other
65
			'FBAN/' => [
66
				'match' => 'start',
67
				'categories' => function (string $value) : array {
68
					$map = [
69
						'FBAN/MessengerLiteForiOS' => [
70
							'type' => 'human',
71
							'app' => 'Facebook Messenger Lite',
72
							'platform' => 'iOS'
73
						],
74
						'FBAN/FB4A' => [
75
							'type' => 'human',
76
							'app' => 'Facebook',
77
							'platform' => 'Android'
78
						],
79
						'FBAN/FBIOS' => [
80
							'type' => 'human',
81
							'app' => 'Facebook',
82
							'platform' => 'iOS'
83
						],
84
						'FBAN/FB4FireTV' => [
85
							'type' => 'human',
86
							'category' => 'tv',
87
							'app' => 'Facebook',
88
							'platform' => 'Android'
89
						],
90
						'FBAN/MessengerDesktop' => [
91
							'type' => 'human',
92
							'category' => 'desktop',
93
							'app' => 'Facebook Messenger Desktop'
94
						]
95
					];
96
					return $map[$value] ?? [
97
						'app' => 'Facebook',
98
						'type' => 'human'
99
					];
100
				}
101
			],
102
			'FB_IAB/' => [
103
				'match' => 'start',
104
				'categories' => [
105
					'app' => 'Facebook'
106
				]
107
			],
108
			'FBAV/' => [
109
				'match' => 'start',
110
				'categories' => fn (string $value) : array => [
111
					'appversion' => \mb_substr($value, 5)
112
				]
113
			],
114
			'FBMF/' => [
115
				'match' => 'start',
116
				'categories' => fn (string $value) : array => [
117
					'vendor' => \mb_substr($value, 5)
118
				]
119
			],
120
			'FBDV/' => [
121
				'match' => 'start',
122
				'categories' => fn (string $value) : array => [
123
					'device' => \mb_substr($value, 5)
124
				]
125
			],
126
			'FBMD/' => [
127
				'match' => 'start',
128
				'categories' => fn (string $value) : array => [
129
					'model' => \mb_substr($value, 5)
130
				]
131
			],
132
			'FBLC/' => [
133
				'match' => 'start',
134
				'categories' => fn (string $value) : array => [
135
					'language' => \str_replace('_', '-', \mb_substr($value, 5))
136
				]
137
			],
138
			'FBDM/' => [
139
				'match' => 'start',
140
				'categories' => function (string $value) : array {
141
					$data = [];
142
					foreach (\explode(',', \trim(\mb_substr($value, 5), '{}')) AS $item) {
143
						$parts = \explode('=', $item);
144
						if (!empty($parts[1])) {
145
							if (\is_numeric($parts[1])) {
146
								$parts[1] = \str_contains($parts[1], '.') ? \floatval($parts[1]) : \intval($parts[1]);
147
							}
148
							$data[$parts[0]] = $parts[1];
149
						}
150
					}
151
					return $data;
152
				}
153
			],
154
			'FBSN/' => [
155
				'match' => 'start',
156
				'categories' => fn (string $value) : array => [
157
					'platform' => \mb_substr($value, 5)
158
				]
159
			],
160
			'FBSV' => [
161
				'match' => 'start',
162
				'categories' => fn (string $value) : array => [
163
					'platformversion' => \mb_substr($value, 5)
164
				]
165
			],
166
167
			// other
168
			'MAUI' => [
169
				'match' => 'start',
170
				'categories' => fn (string $value) : array => [
171
					'type' => 'human',
172
					'app' => $value
173
				]
174
			],
175
			'AppName/' => [
176
				'match' => 'start',
177
				'categories' => fn(string $value) : array => [
178
					'app' => \mb_substr($value, 8)
179
				]
180
			],
181
			'app_version/' => [
182
				'match' => 'start',
183
				'categories' => fn(string $value) : array => [
184
					'appversion' => \mb_substr($value, 12)
185
				]
186
			],
187
				
188
			// generic
189
			'App' => [
190
				'match' => 'any',
191
				'categories' => $fn['appslash']
192
			],
193
		];
194
	}
195
}