Passed
Push — main ( 1da062...26c21a )
by Will
02:48
created

platforms   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 393
Duplicated Lines 0 %

Test Coverage

Coverage 95.76%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 26
eloc 296
c 2
b 0
f 0
dl 0
loc 393
ccs 361
cts 377
cp 0.9576
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F get() 0 386 26
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
/**
6
 * @phpstan-import-type MatchConfig from config
7
 */
8
class platforms {
9
10
	/**
11
	 * Generates a configuration array for matching platforms
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 40
	public static function get() : array {
16 1
		$fn = [
17 1
			'platformspace' => function (string $value) : array {
18
				$parts = \explode(' ', $value, 2);
19
				return [
20
					'platform' => $parts[0],
21
					'platformversion' => $parts[1] ?? null
22
				];
23 1
			},
24 1
			'platformlinux' => function (string $value) : array {
25 10
				if (!\str_starts_with($value, 'Red Hat/') && ($platform = \mb_strstr($value, ' ', true)) !== false) {
26
					$value = $platform;
27
				}
28 10
				$parts = \explode('/', $value, 2);
29 10
				if (!isset($parts[1])) {
30 8
					$parts = \explode('-', $value, 2);
31
				}
32 10
				if (!isset($parts[1])) {
33 6
					$parts = \explode(' ', $value, 2);
34
				}
35 10
				return [
36 10
					'type' => 'human',
37 10
					'category' => 'desktop',
38 10
					'kernel' => 'Linux',
39 10
					'platform' => $parts[0] === 'Web0S' ? 'WebOS' : $parts[0],
40 10
					'platformversion' => isset($parts[1]) && \strspn($parts[1], '0123456789.-_') > 0 ? $parts[1] : null
41 10
				];
42 1
			},
43 1
			'platformwindows' => function (string $value) : array {
44 40
				$mapping = [
45 40
					'5.0' => '2000',
46 40
					'5.1' => 'XP',
47 40
					'5.2' => 'XP',
48 40
					'6.0' => 'Vista',
49 40
					'6.1' => '7',
50 40
					'6.2' => '8',
51 40
					'6.3' => '8.1',
52 40
					'10.0' => '10'
53 40
				];
54 40
				$version = null;
55 40
				foreach (['Windows NT ', 'Windows '] AS $item) {
56 40
					if (($pos = \mb_stripos($value, $item)) !== false) {
57 40
						$version = \explode(' ', \mb_substr($value, $pos + \mb_strlen($item)))[0];
58 40
						break;
59
					}
60
				}
61 40
				return [
62 40
					'type' => 'human',
63 40
					'category' => 'desktop',
64 40
					'kernel' => 'Windows NT',
65 40
					'platform' => 'Windows',
66 40
					'platformversion' => $mapping[$version] ?? $version
67 40
				];
68 1
			},
69 1
			'android' => function (string $value, int $i, array $tokens) : array {
70 30
				$os = \explode(' ', $value, 3);
71 30
				$device = empty($tokens[++$i]) || \strlen($tokens[$i]) <= 2 ? [] : devices::getDevice($tokens[$i]);
72 30
				return \array_merge($device, [
73 30
					'type' => 'human',
74 30
					'platform' => $os[0],
75 30
					'platformversion' => $os[1] ?? null
76 30
				]);
77 1
			}
78 1
		];
79
80 1
		return [
81
82
			// platforms
83 1
			'Windows NT ' => [
84 1
				'match' => 'any',
85 1
				'categories' => $fn['platformwindows']
86 1
			],
87 1
			'Windows Phone' => [
88 1
				'match' => 'start',
89 1
				'categories' => function (string $value) : array {
90 3
					$version = \mb_substr($value, 14);
91 3
					return [
92 3
						'type' => 'human',
93 3
						'category' => 'mobile',
94 3
						'platform' => 'Windows Phone',
95 3
						'platformversion' => $version,
96 3
						'kernel' => \intval($version) >= 8 ? 'Windows NT' : 'Windows CE'
97 3
					];
98 1
				}
99 1
			],
100 1
			'Win98' => [
101 1
				'match' => 'start',
102 1
				'categories' => [
103 1
					'type' => 'human',
104 1
					'category' => 'desktop',
105 1
					'architecture' => 'x86',
106 1
					'bits' => 32,
107 1
					'kernel' => 'MS-DOS',
108 1
					'platform' => 'Windows',
109 1
					'platformversion' => '98'
110 1
				]
111 1
			],
112 1
			'WinNT' => [
113 1
				'match' => 'start',
114 1
				'categories' => fn (string $value) : array => [
115 1
					'type' => 'human',
116 1
					'category' => 'desktop',
117 1
					'architecture' => 'x86',
118 1
					'bits' => 32,
119 1
					'kernel' => 'Windows NT',
120 1
					'platform' => 'Windows',
121 1
					'platformversion' => \mb_substr($value, 5)
122 1
				]
123 1
			],
124 1
			'Windows' => [
125 1
				'match' => 'any',
126 1
				'categories' => $fn['platformwindows']
127 1
			],
128 1
			'Mac OS X' => [
129 1
				'match' => 'any',
130 1
				'categories' => function (string $value) : array {
131 23
					$version = \str_replace('_', '.', \mb_substr($value, \mb_stripos($value, 'Mac OS X') + 9));
132 23
					$register = $version && \intval(\explode('.', $version)[1] ?? 0) >= 6 ? 64 : null;
133 23
					return [
134 23
						'type' => 'human',
135 23
						'category' => 'desktop',
136 23
						'kernel' => 'Linux',
137 23
						'platform' => 'Mac OS X',
138 23
						'platformversion' => $version === '' ? null : $version,
139 23
						'bits' => $register
140 23
					];
141 1
				}
142 1
			],
143 1
			'AppleTV' => [
144 1
				'match' => 'exact',
145 1
				'categories' => [
146 1
					'type' => 'human',
147 1
					'category' => 'tv',
148 1
					'device' => 'AppleTV',
149 1
					'platform' => 'tvOS',
150 1
					'bits' => 64
151 1
				]
152 1
			],
153 1
			'iOS/' => [
154 1
				'match' => 'start',
155 1
				'categories' => fn (string $value) : array => [
156 1
					'type' => 'human',
157 1
					'category' => 'mobile',
158 1
					'platform' => 'iOS',
159 1
					'platformversion' => \mb_substr($value, 4)
160 1
				]
161 1
			],
162 1
			'CrOS' => [
163 1
				'match' => 'start',
164 1
				'categories' => function (string $value) : array {
165 4
					$parts = \explode(' ', $value);
166 4
					return [
167 4
						'type' => 'human',
168 4
						'category' => 'desktop',
169 4
						'platform' => 'Chrome OS',
170 4
						'platformversion' => $parts[2] ?? null
171 4
					];
172 1
				}
173 1
			],
174 1
			'Kindle/' => [
175 1
				'match' => 'start',
176 1
				'categories' => fn (string $value) : array => [
177 1
					'type' => 'human',
178 1
					'category' => 'ebook',
179 1
					'platform' => 'Kindle',
180 1
					'platformversion' => \mb_substr($value, 7)
181 1
				]
182 1
			],
183 1
			'Tizen' => [
184 1
				'match' => 'start',
185 1
				'categories' => function (string $value) : array {
186 1
					$parts = \explode(' ', $value, 2);
187 1
					return [
188 1
						'type' => 'human',
189 1
						'category' => 'desktop',
190 1
						'kernel' => 'Linux',
191 1
						'platform' => $parts[0],
192 1
						'platformversion' => $parts[1] ?? null
193 1
					];
194 1
				}
195 1
			],
196 1
			'Ubuntu' => [
197 1
				'match' => 'start',
198 1
				'categories' => $fn['platformlinux']
199 1
			],
200 1
			'Kubuntu' => [
201 1
				'match' => 'start',
202 1
				'categories' => $fn['platformlinux']
203 1
			],
204 1
			'Mint' => [
205 1
				'match' => 'start',
206 1
				'categories' => $fn['platformlinux']
207 1
			],
208 1
			'SUSE' => [
209 1
				'match' => 'start',
210 1
				'categories' => $fn['platformlinux']
211 1
			],
212 1
			'Red Hat/' => [
213 1
				'match' => 'start',
214 1
				'categories' => $fn['platformlinux']
215 1
			],
216 1
			'Debian' => [
217 1
				'match' => 'start',
218 1
				'categories' => $fn['platformlinux']
219 1
			],
220 1
			'Darwin' => [
221 1
				'match' => 'start',
222 1
				'categories' => $fn['platformlinux']
223 1
			],
224 1
			'Fedora' => [
225 1
				'match' => 'start',
226 1
				'categories' => $fn['platformlinux']
227 1
			],
228 1
			'CentOS' => [
229 1
				'match' => 'start',
230 1
				'categories' => $fn['platformlinux']
231 1
			],
232 1
			'Rocky' => [
233 1
				'match' => 'start',
234 1
				'categories' => $fn['platformlinux']
235 1
			],
236 1
			'ArchLinux' => [
237 1
				'match' => 'exact',
238 1
				'categories' => fn () : array => [
239
					'type' => 'human',
240
					'category' => 'desktop',
241
					'kernel' => 'Linux',
242
					'platform' => 'Arch',
243
				]
244 1
			],
245 1
			'Arch' => [
246 1
				'match' => 'exact',
247 1
				'categories' => fn (string $value) : array => [
248
					'type' => 'human',
249
					'category' => 'desktop',
250
					'kernel' => 'Linux',
251
					'platform' => $value,
252
				]
253 1
			],
254 1
			'Web0S' => [
255 1
				'match' => 'exact',
256 1
				'categories' => $fn['platformlinux']
257 1
			],
258 1
			'webOSTV' => [
259 1
				'match' => 'exact',
260 1
				'categories' => [
261 1
					'type' => 'human',
262 1
					'category' => 'tv',
263 1
					'kernel' => 'Linux',
264 1
					'platform' => 'WebOS'
265 1
				]
266 1
			],
267 1
			'WEBOS' => [
268 1
				'match' => 'start',
269 1
				'categories' => fn (string $value) : array => [
270 1
					'type' => 'human',
271 1
					'category' => 'tv',
272 1
					'kernel' => 'Linux',
273 1
					'platform' => 'WebOS',
274 1
					'platformversion' => \mb_substr($value, 5)
275 1
				]
276 1
			],
277 1
			'SunOS' => [
278 1
				'match' => 'start',
279 1
				'categories' => [
280 1
					'type' => 'human',
281 1
					'category' => 'desktop',
282 1
					'kernel' => 'unix',
283 1
					'platform' => 'Solaris',
284 1
				]
285 1
			],
286 1
			'AmigaOS' => [
287 1
				'match' => 'any',
288 1
				'categories' => function (string $value) : array {
289 3
					if (($pos = \mb_stripos($value, 'AmigaOS')) !== false) {
290 3
						$value = \mb_substr($value, $pos);
291
					}
292 3
					$parts = \explode(' ', $value, 2);
293 3
					return [
294 3
						'type' => 'human',
295 3
						'category' => 'desktop',
296 3
						'kernel' => 'Exec',
297 3
						'platform' => $parts[0],
298 3
						'platformversion' => isset($parts[1]) && \strspn($parts[1], '0123456789.-_') === \strlen($parts[1]) ? $parts[1] : null
299 3
					];
300 1
				}
301 1
			],
302 1
			'Fuchsia' => [
303 1
				'match' => 'exact',
304 1
				'categories' => function (string $value, int $i, array $tokens) : array {
305 1
					$os = \explode(' ', $tokens[++$i], 2);
306 1
					return [
307 1
						'type' => 'human',
308 1
						'category' => 'mobile',
309 1
						'kernel' => 'Zircon',
310 1
						'platform' => $value,
311 1
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
312 1
					];
313 1
				}
314 1
			],
315 1
			'Maemo' => [
316 1
				'match' => 'exact',
317 1
				'categories' => function (string $value, int $i, array $tokens) : array {
318 1
					$os = \explode(' ', $tokens[++$i], 2);
319 1
					return [
320 1
						'type' => 'human',
321 1
						'category' => 'mobile',
322 1
						'kernel' => 'Linux',
323 1
						'platform' => $value,
324 1
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
325 1
					];
326 1
				}
327 1
			],
328 1
			'J2ME/MIDP' => [
329 1
				'match' => 'exact',
330 1
				'categories' => fn (string $value) : array => [
331 2
					'type' => 'human',
332 2
					'category' => 'mobile',
333 2
					'kernel' => 'Java VM',
334 2
					'platform' => $value
335 2
				]
336 1
			],
337 1
			'Haiku' => [
338 1
				'match' => 'any',
339 1
				'categories' => function (string $value) : array {
340 3
					$parts = \explode('/', $value, 2);
341 3
					return [
342 3
						'type' => 'human',
343 3
						'category' => 'desktop',
344 3
						'kernel' => 'Haiku',
345 3
						'platform' => 'Haiku',
346 3
						'platformversion' => $parts[1] ?? null
347 3
					];
348 1
				}
349 1
			],
350 1
			'BeOS' => [
351 1
				'match' => 'start',
352 1
				'categories' => function (string $value) : array {
353 1
					$parts = \explode('/', $value, 2);
354 1
					return [
355 1
						'type' => 'human',
356 1
						'category' => 'desktop',
357 1
						'kernel' => 'BeOS',
358 1
						'platform' => 'BeOS',
359 1
						'platformversion' => $parts[1] ?? null
360 1
					];
361 1
				}
362 1
			],
363 1
			'Android' => [
364 1
				'match' => 'exact',
365 1
				'categories' => $fn['android']
366 1
			],
367 1
			'Android ' => [
368 1
				'match' => 'start',
369 1
				'categories' => $fn['android']
370 1
			],
371 1
			'Linux' => [
372 1
				'match' => 'any',
373 1
				'categories' => [
374 1
					'kernel' => 'Linux',
375 1
					'platform' => 'Linux'
376 1
				]
377 1
			],
378 1
			'X11' => [
379 1
				'match' => 'exact',
380 1
				'categories' => function (string $value, int $i, array $tokens) : array {
381 28
					$os = \explode(' ', $tokens[++$i] ?? '', 2);
382 28
					return [
383 28
						'category' => 'desktop',
384 28
						'kernel' => 'Linux',
385 28
						'platform' => $os[0] ?: 'X11',
386 28
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
387 28
					];
388 1
				}
389 1
			],
390 1
			'OS/2' => [
391 1
				'match' => 'exact',
392 1
				'categories' => [
393 1
					'category' => 'desktop',
394 1
					'platform' => 'OS/2'
395 1
				]
396 1
			],
397 1
			'Version/' => [
398 1
				'match' => 'start',
399 1
				'categories' => fn (string $value) : array => [
400 35
					'platformversion' => \mb_substr($value, 8)
401 35
				]
402 1
			]
403 1
		];
404
	}
405
}