Passed
Push — main ( 7300c8...1902e7 )
by Will
13:33
created

apps   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Test Coverage

Coverage 99.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 178
c 1
b 0
f 0
dl 0
loc 264
ccs 241
cts 243
cp 0.9918
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D get() 0 257 17
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
/**
6
 * @phpstan-import-type MatchConfig from config
7
 */
8
class apps {
9
10
	/**
11
	 * Generates a configuration array for matching apps
12
	 * 
13
	 * @return MatchConfig An array with keys representing the string to match, and a value of an array containing parsing and output settings
0 ignored issues
show
Bug introduced by
The type hexydec\agentzero\MatchConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
	 */
15 39
	public static function get() : array {
16 1
		$fn = [
17 1
			'appslash' => function (string $value, int $i, array $tokens) : ?array {
0 ignored issues
show
Unused Code introduced by
The parameter $tokens is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

17
			'appslash' => function (string $value, int $i, /** @scrutinizer ignore-unused */ array $tokens) : ?array {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $i is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

17
			'appslash' => function (string $value, /** @scrutinizer ignore-unused */ int $i, array $tokens) : ?array {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18 39
				if (\mb_stripos($value, 'AppleWebKit') !== 0 && !\str_contains($value, '://')) {
19 12
					$parts = \explode('/', $value, 2);
20 12
					return [
21 12
						'app' => $parts[0],
22 12
						'appversion' => $parts[1] ?? null
23 12
					];
24
				}
25 37
				return null;
26 1
			},
27 1
			'langslash' => function (string $value) : array {
28 6
				$parts = \explode('-', \str_replace('_', '-', \explode('/', $value, 2)[1]), 4);
29 6
				$suffix = $parts[2] ?? $parts[1] ?? null;
30 6
				return [
31 6
					'language' => \strtolower($parts[0]).($suffix !== null ? '-'.\strtoupper($suffix) : '')
32 6
				];
33 1
			}
34 1
		];
35 1
		return [
36 1
			'OcIdWebView' => [
37 1
				'match' => 'exact',
38 1
				'categories' => function (string $value, int $i, array $tokens) : array {
39 1
					$data = [
40 1
						'app' => $value
41 1
					];
42 1
					if (!empty($tokens[$i+1]) && ($json = \json_decode($tokens[$i+1], true)) !== null) {
43 1
						$data['appversion'] = $json['appVersion'] ?? null;
44 1
						$data['darkmode'] = isset($json['isDarkTheme']) ? \boolval($json['isDarkTheme']) : null;
45
					}
46 1
					return $data;
47 1
				}
48 1
			],
49 1
			'com.google.android.apps.' => [
50 1
				'match' => 'any',
51 1
				'categories' => $fn['appslash']
52 1
			],
53 1
			'Instagram' => [
54 1
				'match' => 'any',
55 1
				'categories' => function (string $value, int $i, array $tokens) : array {
56 5
					$data = [
57 5
						'app' => 'Instagram',
58 5
						'appversion' => \explode(' ', $value, 3)[1] ?? null
59 5
					];
60 5
					foreach (\array_slice($tokens, $i + 1) AS $item) {
61 5
						if (\str_starts_with($item, 'scale=')) {
62 2
							$data['density'] = \floatval(\mb_substr($item, 6));
63 5
						} elseif (\str_ends_with($item, 'dpi')) {
64 4
							$data['dpi'] = \intval(\mb_substr($item, 0, -3));
65 5
						} elseif (\str_contains($item, 'x') && \strspn($item, '0123456789x') === \strlen($item)) {
66 5
							list($data['width'], $data['height']) = \array_map('intval', \explode('x', $item, 2));
67
						}
68
					}
69 5
					return $data;
70 1
				}
71 1
			],
72 1
			'GSA/' => [
73 1
				'match' => 'any',
74 1
				'categories' => $fn['appslash']
75 1
			],
76 1
			'DuckDuckGo/' => [
77 1
				'match' => 'start',
78 1
				'categories' => $fn['appslash']
79 1
			],
80 1
			'FlipboardProxy/' => [
81 1
				'match' => 'start',
82 1
				'categories' => $fn['appslash']
83 1
			],
84 1
			'AndroidDownloadManager/' => [
85 1
				'match' => 'start',
86 1
				'categories' => $fn['appslash']
87 1
			],
88 1
			'Google-Read-Aloud' => [
89 1
				'match' => 'exact',
90 1
				'categories' => [
91 1
					'type' => 'human',
92 1
					'app' => 'Google-Read-Aloud'
93 1
				]
94 1
			],
95 1
			'Zoom ' => [
96 1
				'match' => 'start',
97 1
				'categories' => fn (string $value) : array => [
98 1
					'app' => 'Zoom',
99 1
					'appversion' => \mb_substr($value, 5)
100 1
				]
101 1
			],
102
103
			// special parser for Facebook app because it is completely different to any other
104 1
			'FBAN/' => [
105 1
				'match' => 'start',
106 1
				'categories' => function (string $value) : array {
107 4
					$map = [
108 4
						'FBAN/MessengerLiteForiOS' => [
109 4
							'type' => 'human',
110 4
							'app' => 'Facebook Messenger Lite',
111 4
							'platform' => 'iOS'
112 4
						],
113 4
						'FBAN/FB4A' => [
114 4
							'type' => 'human',
115 4
							'app' => 'Facebook',
116 4
							'platform' => 'Android'
117 4
						],
118 4
						'FBAN/FBIOS' => [
119 4
							'type' => 'human',
120 4
							'app' => 'Facebook',
121 4
							'platform' => 'iOS'
122 4
						],
123 4
						'FBAN/FB4FireTV' => [
124 4
							'type' => 'human',
125 4
							'category' => 'tv',
126 4
							'app' => 'Facebook',
127 4
							'platform' => 'Android'
128 4
						],
129 4
						'FBAN/MessengerDesktop' => [
130 4
							'type' => 'human',
131 4
							'category' => 'desktop',
132 4
							'app' => 'Facebook Messenger Desktop'
133 4
						]
134 4
					];
135 4
					return $map[$value] ?? [
136 4
						'app' => 'Facebook',
137 4
						'type' => 'human'
138
					];
139 1
				}
140 1
			],
141 1
			'FB_IAB/' => [
142 1
				'match' => 'start',
143 1
				'categories' => [
144 1
					'app' => 'Facebook'
145 1
				]
146 1
			],
147 1
			'FBAV/' => [
148 1
				'match' => 'start',
149 1
				'categories' => fn (string $value) : array => [
150 8
					'appversion' => \mb_substr($value, 5)
151 8
				]
152 1
			],
153 1
			'FBMF/' => [
154 1
				'match' => 'start',
155 1
				'categories' => fn (string $value) : array => [
156 1
					'vendor' => \mb_substr($value, 5)
157 1
				]
158 1
			],
159 1
			'FBDV/' => [
160 1
				'match' => 'start',
161 1
				'categories' => fn (string $value) : array => [
162 2
					'device' => \mb_substr($value, 5)
163 2
				]
164 1
			],
165 1
			'FBMD/' => [
166 1
				'match' => 'start',
167 1
				'categories' => fn (string $value) : array => [
168 2
					'model' => \mb_substr($value, 5)
169 2
				]
170 1
			],
171 1
			'FBLC/' => [
172 1
				'match' => 'start',
173 1
				'categories' => $fn['langslash']
174 1
			],
175 1
			'FBDM/' => [
176 1
				'match' => 'start',
177 1
				'categories' => function (string $value) : array {
178 1
					$data = [];
179 1
					foreach (\explode(',', \trim(\mb_substr($value, 5), '{}')) AS $item) {
180 1
						$parts = \explode('=', $item);
181 1
						if (!empty($parts[1])) {
182 1
							if (\is_numeric($parts[1])) {
183 1
								$parts[1] = \str_contains($parts[1], '.') ? \floatval($parts[1]) : \intval($parts[1]);
184
							}
185 1
							$data[$parts[0]] = $parts[1];
186
						}
187
					}
188 1
					return $data;
189 1
				}
190 1
			],
191 1
			'width=' => [
192 1
				'match' => 'start',
193 1
				'categories' => fn (string $value) : array => [
194 1
					'width' => \intval(\mb_substr($value, 6))
195 1
				]
196 1
			],
197 1
			'height=' => [
198 1
				'match' => 'start',
199 1
				'categories' => fn (string $value) : array => [
200 1
					'height' => \intval(\mb_substr($value, 7))
201 1
				]
202 1
			],
203 1
			'dpi=' => [
204 1
				'match' => 'start',
205 1
				'categories' => fn (string $value) : array => [
206
					'dpi' => \mb_substr($value, 4)
207
				]
208 1
			],
209 1
			'FBSN/' => [
210 1
				'match' => 'start',
211 1
				'categories' => fn (string $value) : array => [
212 2
					'platform' => \mb_substr($value, 5)
213 2
				]
214 1
			],
215 1
			'FBSV' => [
216 1
				'match' => 'start',
217 1
				'categories' => fn (string $value) : array => [
218 2
					'platformversion' => \mb_substr($value, 5)
219 2
				]
220 1
			],
221 1
			'isDarkMode/' => [
222 1
				'match' => 'start',
223 1
				'categories' => function (string $value) : array {
224 1
					$mode = \mb_substr($value, 11);
225 1
					return [
226 1
						'darkmode' => \in_array($mode, ['0', '1'], true) ? \boolval($mode) : null
227 1
					];
228 1
				}
229 1
			],
230 1
			'ByteFullLocale/' => [
231 1
				'match' => 'start',
232 1
				'categories' => $fn['langslash']
233 1
			],
234 1
			'NetType/' => [
235 1
				'match' => 'start',
236 1
				'categories' => fn (string $value) : array => [
237 2
					'nettype' => \mb_convert_case(\mb_substr($value, 8), MB_CASE_UPPER)
238 2
				]
239 1
			],
240
241
			// other
242 1
			'MAUI' => [
243 1
				'match' => 'start',
244 1
				'categories' => fn (string $value) : array => [
245 1
					'type' => 'human',
246 1
					'app' => $value
247 1
				]
248 1
			],
249 1
			'AppName/' => [
250 1
				'match' => 'start',
251 1
				'categories' => fn(string $value) : array => [
252 2
					'app' => \mb_substr($value, 8)
253 2
				]
254 1
			],
255 1
			'AppVersion/' => [
256 1
				'match' => 'any',
257 1
				'categories' => fn(string $value) : array => [
258 1
					'appversion' => \explode('/', $value, 2)[1]
259 1
				]
260 1
			],
261 1
			'app_version/' => [
262 1
				'match' => 'start',
263 1
				'categories' => fn(string $value) : array => [
264 1
					'appversion' => \mb_substr($value, 12)
265 1
				]
266 1
			],
267
				
268
			// generic
269 1
			'App' => [
270 1
				'match' => 'any',
271 1
				'categories' => $fn['appslash']
272 1
			],
273 1
		];
274
	}
275
}