Passed
Push — main ( 0a2d45...0009be )
by Will
02:48
created

platforms::getPlatform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 13
ccs 13
cts 13
cp 1
rs 9.9
cc 1
nc 1
nop 1
crap 1
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 51
	public static function get() : array {
16 1
		$fn = [
17 1
			'platformlinux' => function (string $value) : array {
18 10
				if (!\str_starts_with($value, 'Red Hat/') && ($platform = \mb_strstr($value, ' ', true)) !== false) {
19 1
					$value = $platform;
20
				}
21 10
				$parts = \explode('/', $value, 2);
22 10
				if (!isset($parts[1])) {
23 8
					$parts = \explode('-', $value, 2);
24
				}
25 10
				if (!isset($parts[1])) {
26 6
					$parts = \explode(' ', $value, 2);
27
				}
28 10
				return [
29 10
					'type' => 'human',
30 10
					'category' => 'desktop',
31 10
					'kernel' => 'Linux',
32 10
					'platform' => self::getPlatform($parts[0]),
33 10
					'platformversion' => $parts[1] ?? null
34 10
				];
35 1
			},
36 1
			'platformwindows' => function (string $value) : array {
37 41
				$mapping = [
38 41
					'5.0' => '2000',
39 41
					'5.1' => 'XP',
40 41
					'5.2' => 'XP',
41 41
					'6.0' => 'Vista',
42 41
					'6.1' => '7',
43 41
					'6.2' => '8',
44 41
					'6.3' => '8.1',
45 41
					'10.0' => '10'
46 41
				];
47 41
				$version = null;
48 41
				foreach (['Windows NT ', 'Windows '] AS $item) {
49 41
					if (($pos = \mb_stripos($value, $item)) !== false) {
50 41
						$version = \explode(' ', \mb_substr($value, $pos + \mb_strlen($item)))[0];
51 41
						break;
52
					}
53
				}
54 41
				return [
55 41
					'type' => 'human',
56 41
					'category' => 'desktop',
57 41
					'kernel' => 'Windows NT',
58 41
					'platform' => 'Windows',
59 41
					'platformversion' => $mapping[$version] ?? $version
60 41
				];
61 1
			},
62 1
			'android' => function (string $value, int $i, array $tokens) : array {
63 31
				$os = \explode(' ', $value, 3);
64
65
				// skip language
66 31
				if (!empty($tokens[++$i]) && \strlen($tokens[$i]) === 5 && \mb_strpos($tokens[$i], '_') === 2) {
67
					$i++;
68
				}
69 31
				$device = empty($tokens[$i]) || \strlen($tokens[$i]) <= 2 ? [] : devices::getDevice($tokens[$i]);
70 31
				return \array_merge($device, [
71 31
					'type' => 'human',
72 31
					'platform' => 'Android',
73 31
					'platformversion' => $os[1] ?? null
74 31
				]);
75 1
			}
76 1
		];
77
78 1
		return [
79
80
			// platforms
81 1
			'Windows NT ' => [
82 1
				'match' => 'any',
83 1
				'categories' => $fn['platformwindows']
84 1
			],
85 1
			'Windows Phone' => [
86 1
				'match' => 'start',
87 1
				'categories' => function (string $value) : array {
88 3
					$version = \mb_substr($value, 14);
89 3
					return [
90 3
						'type' => 'human',
91 3
						'category' => 'mobile',
92 3
						'platform' => 'Windows Phone',
93 3
						'platformversion' => $version,
94 3
						'kernel' => \intval($version) >= 8 ? 'Windows NT' : 'Windows CE'
95 3
					];
96 1
				}
97 1
			],
98 1
			'Win98' => [
99 1
				'match' => 'start',
100 1
				'categories' => [
101 1
					'type' => 'human',
102 1
					'category' => 'desktop',
103 1
					'architecture' => 'x86',
104 1
					'bits' => 32,
105 1
					'kernel' => 'MS-DOS',
106 1
					'platform' => 'Windows',
107 1
					'platformversion' => '98'
108 1
				]
109 1
			],
110 1
			'Win32' => [
111 1
				'match' => 'exact',
112 1
				'categories' => [
113 1
					'type' => 'human',
114 1
					'category' => 'desktop',
115 1
					'architecture' => 'x86',
116 1
					'bits' => 32,
117 1
					'platform' => 'Windows'
118 1
				]
119 1
			],
120 1
			'Win64' => [
121 1
				'match' => 'exact',
122 1
				'categories' => [
123 1
					'type' => 'human',
124 1
					'category' => 'desktop',
125 1
					'bits' => 64,
126 1
					'platform' => 'Windows'
127 1
				]
128 1
			],
129 1
			'WinNT' => [
130 1
				'match' => 'start',
131 1
				'categories' => fn (string $value) : array => [
132 1
					'type' => 'human',
133 1
					'category' => 'desktop',
134 1
					'architecture' => 'x86',
135 1
					'bits' => 32,
136 1
					'kernel' => 'Windows NT',
137 1
					'platform' => 'Windows',
138 1
					'platformversion' => \mb_substr($value, 5)
139 1
				]
140 1
			],
141 1
			'Windows' => [
142 1
				'match' => 'any',
143 1
				'categories' => $fn['platformwindows']
144 1
			],
145 1
			'Mac OS X' => [
146 1
				'match' => 'any',
147 1
				'categories' => function (string $value) : array {
148 25
					$version = \str_replace('_', '.', \mb_substr($value, \mb_stripos($value, 'Mac OS X') + 9));
149 25
					$register = $version && \intval(\explode('.', $version)[1] ?? 0) >= 6 ? 64 : null;
150 25
					return [
151 25
						'type' => 'human',
152 25
						'category' => 'desktop',
153 25
						'kernel' => 'Linux',
154 25
						'platform' => 'Mac OS X',
155 25
						'platformversion' => $version === '' ? null : $version,
156 25
						'bits' => $register
157 25
					];
158 1
				}
159 1
			],
160 1
			'AppleTV' => [
161 1
				'match' => 'exact',
162 1
				'categories' => [
163 1
					'type' => 'human',
164 1
					'category' => 'tv',
165 1
					'device' => 'AppleTV',
166 1
					'platform' => 'tvOS',
167 1
					'bits' => 64
168 1
				]
169 1
			],
170 1
			'iOS/' => [
171 1
				'match' => 'start',
172 1
				'categories' => fn (string $value) : array => [
173 1
					'type' => 'human',
174 1
					'category' => 'mobile',
175 1
					'platform' => 'iOS',
176 1
					'platformversion' => \mb_substr($value, 4)
177 1
				]
178 1
			],
179 1
			'CrOS' => [
180 1
				'match' => 'start',
181 1
				'categories' => function (string $value) : array {
182 4
					$parts = \explode(' ', $value);
183 4
					return [
184 4
						'type' => 'human',
185 4
						'category' => 'desktop',
186 4
						'platform' => 'Chrome OS',
187 4
						'platformversion' => $parts[2] ?? null
188 4
					];
189 1
				}
190 1
			],
191 1
			'Kindle/' => [
192 1
				'match' => 'start',
193 1
				'categories' => fn (string $value) : array => [
194 1
					'type' => 'human',
195 1
					'category' => 'ebook',
196 1
					'platform' => 'Kindle',
197 1
					'platformversion' => \mb_substr($value, 7)
198 1
				]
199 1
			],
200 1
			'Tizen' => [
201 1
				'match' => 'start',
202 1
				'categories' => function (string $value) : array {
203 1
					$parts = \explode(' ', $value, 2);
204 1
					return [
205 1
						'type' => 'human',
206 1
						'category' => 'desktop',
207 1
						'kernel' => 'Linux',
208 1
						'platform' => 'Tizen',
209 1
						'platformversion' => $parts[1] ?? null
210 1
					];
211 1
				}
212 1
			],
213 1
			'Ubuntu' => [
214 1
				'match' => 'start',
215 1
				'categories' => $fn['platformlinux']
216 1
			],
217 1
			'Kubuntu' => [
218 1
				'match' => 'start',
219 1
				'categories' => $fn['platformlinux']
220 1
			],
221 1
			'Mint' => [
222 1
				'match' => 'start',
223 1
				'categories' => $fn['platformlinux']
224 1
			],
225 1
			'SUSE' => [
226 1
				'match' => 'start',
227 1
				'categories' => $fn['platformlinux']
228 1
			],
229 1
			'Red Hat/' => [
230 1
				'match' => 'start',
231 1
				'categories' => $fn['platformlinux']
232 1
			],
233 1
			'Debian' => [
234 1
				'match' => 'start',
235 1
				'categories' => $fn['platformlinux']
236 1
			],
237 1
			'Darwin' => [
238 1
				'match' => 'start',
239 1
				'categories' => $fn['platformlinux']
240 1
			],
241 1
			'Fedora' => [
242 1
				'match' => 'start',
243 1
				'categories' => $fn['platformlinux']
244 1
			],
245 1
			'CentOS' => [
246 1
				'match' => 'start',
247 1
				'categories' => $fn['platformlinux']
248 1
			],
249 1
			'Rocky' => [
250 1
				'match' => 'start',
251 1
				'categories' => $fn['platformlinux']
252 1
			],
253 1
			'Alma' => [
254 1
				'match' => 'start',
255 1
				'categories' => $fn['platformlinux']
256 1
			],
257 1
			'Gentoo' => [
258 1
				'match' => 'start',
259 1
				'categories' => $fn['platformlinux']
260 1
			],
261 1
			'Slackware' => [
262 1
				'match' => 'start',
263 1
				'categories' => $fn['platformlinux']
264 1
			],
265 1
			'ArchLinux' => [
266 1
				'match' => 'exact',
267 1
				'categories' => fn () : array => [
268
					'type' => 'human',
269
					'category' => 'desktop',
270
					'kernel' => 'Linux',
271
					'platform' => 'Arch',
272
				]
273 1
			],
274 1
			'Arch' => [
275 1
				'match' => 'exact',
276 1
				'categories' => fn (string $value) : array => [
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

276
				'categories' => fn (/** @scrutinizer ignore-unused */ string $value) : 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...
277
					'type' => 'human',
278
					'category' => 'desktop',
279
					'kernel' => 'Linux',
280
					'platform' => 'Arch',
281
				]
282 1
			],
283 1
			'Web0S' => [
284 1
				'match' => 'exact',
285 1
				'categories' => $fn['platformlinux']
286 1
			],
287 1
			'webOSTV' => [
288 1
				'match' => 'exact',
289 1
				'categories' => [
290 1
					'type' => 'human',
291 1
					'category' => 'tv',
292 1
					'kernel' => 'Linux',
293 1
					'platform' => 'WebOS'
294 1
				]
295 1
			],
296 1
			'WEBOS' => [
297 1
				'match' => 'start',
298 1
				'categories' => fn (string $value) : array => [
299 1
					'type' => 'human',
300 1
					'category' => 'tv',
301 1
					'kernel' => 'Linux',
302 1
					'platform' => 'WebOS',
303 1
					'platformversion' => \mb_substr($value, 5)
304 1
				]
305 1
			],
306 1
			'SunOS' => [
307 1
				'match' => 'start',
308 1
				'categories' => [
309 1
					'type' => 'human',
310 1
					'category' => 'desktop',
311 1
					'kernel' => 'unix',
312 1
					'platform' => 'Solaris',
313 1
				]
314 1
			],
315 1
			'AmigaOS' => [
316 1
				'match' => 'any',
317 1
				'categories' => function (string $value) : array {
318 3
					if (($pos = \mb_stripos($value, 'AmigaOS')) !== false) {
319 3
						$value = \mb_substr($value, $pos);
320
					}
321 3
					$parts = \explode(' ', $value, 2);
322 3
					return [
323 3
						'type' => 'human',
324 3
						'category' => 'desktop',
325 3
						'kernel' => 'Exec',
326 3
						'platform' => 'AmigaOS',
327 3
						'platformversion' => isset($parts[1]) && \strspn($parts[1], '0123456789.-_') === \strlen($parts[1]) ? $parts[1] : null
328 3
					];
329 1
				}
330 1
			],
331 1
			'Fuchsia' => [
332 1
				'match' => 'exact',
333 1
				'categories' => function (string $value, int $i, array $tokens) : array {
334 1
					$os = \explode(' ', $tokens[++$i], 2);
335 1
					return [
336 1
						'type' => 'human',
337 1
						'category' => 'mobile',
338 1
						'kernel' => 'Zircon',
339 1
						'platform' => 'Fuchsia',
340 1
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
341 1
					];
342 1
				}
343 1
			],
344 1
			'Maemo' => [
345 1
				'match' => 'exact',
346 1
				'categories' => function (string $value, int $i, array $tokens) : array {
347 1
					$os = \explode(' ', $tokens[++$i], 2);
348 1
					return [
349 1
						'type' => 'human',
350 1
						'category' => 'mobile',
351 1
						'kernel' => 'Linux',
352 1
						'platform' => 'Maemo',
353 1
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
354 1
					];
355 1
				}
356 1
			],
357 1
			'J2ME/MIDP' => [
358 1
				'match' => 'exact',
359 1
				'categories' => fn (string $value) : array => [
360 2
					'type' => 'human',
361 2
					'category' => 'mobile',
362 2
					'kernel' => 'Java VM',
363 2
					'platform' => $value
364 2
				]
365 1
			],
366 1
			'Haiku' => [
367 1
				'match' => 'any',
368 1
				'categories' => function (string $value) : array {
369 3
					$parts = \explode('/', $value, 2);
370 3
					return [
371 3
						'type' => 'human',
372 3
						'category' => 'desktop',
373 3
						'kernel' => 'Haiku',
374 3
						'platform' => 'Haiku',
375 3
						'platformversion' => $parts[1] ?? null
376 3
					];
377 1
				}
378 1
			],
379 1
			'BeOS' => [
380 1
				'match' => 'start',
381 1
				'categories' => function (string $value) : array {
382 1
					$parts = \explode('/', $value, 2);
383 1
					return [
384 1
						'type' => 'human',
385 1
						'category' => 'desktop',
386 1
						'kernel' => 'BeOS',
387 1
						'platform' => 'BeOS',
388 1
						'platformversion' => $parts[1] ?? null
389 1
					];
390 1
				}
391 1
			],
392 1
			'Android' => [
393 1
				'match' => 'exact',
394 1
				'categories' => $fn['android']
395 1
			],
396 1
			'Android ' => [
397 1
				'match' => 'start',
398 1
				'categories' => $fn['android']
399 1
			],
400 1
			'Linux' => [
401 1
				'match' => 'any',
402 1
				'categories' => function (string $value, int $i, array $tokens) : array {
403 51
					return [
404 51
						'kernel' => 'Linux',
405 51
						'platform' => 'Linux',
406 51
						'platformversion' => \str_contains($tokens[$i + 1] ?? '', '.') && \strspn($tokens[$i + 1], '0123456789.') >= 3 ? \explode(' ', $tokens[$i + 1])[0] : null
407 51
					];
408 1
				}
409 1
			],
410 1
			'X11' => [
411 1
				'match' => 'exact',
412 1
				'categories' => function (string $value, int $i, array $tokens) : array {
413 28
					$os = \explode(' ', $tokens[++$i] ?? '', 2);
414 28
					return [
415 28
						'category' => 'desktop',
416 28
						'kernel' => 'Linux',
417 28
						'platform' => $os[0] ?: 'X11',
418 28
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
419 28
					];
420 1
				}
421 1
			],
422 1
			'OS/2' => [
423 1
				'match' => 'exact',
424 1
				'categories' => [
425 1
					'category' => 'desktop',
426 1
					'platform' => 'OS/2'
427 1
				]
428 1
			],
429 1
			'Version/' => [
430 1
				'match' => 'start',
431 1
				'categories' => fn (string $value) : array => [
432 37
					'platformversion' => \mb_substr($value, 8)
433 37
				]
434 1
			]
435 1
		];
436
	}
437
438 15
	public static function getPlatform(string $value) : string {
439 15
		$map = [
440 15
			'webos' => 'WebOS',
441 15
			'web0s' => 'WebOS',
442 15
			'j2me/midp' => 'J2ME/MIDP',
443 15
			'centos' => 'CentOS',
444 15
			'suse' => 'SUSE',
445 15
			'freebsd' => 'FreeBSD',
446 15
			'openbsd' => 'OpenBSD',
447 15
			'netbsd' => 'NetBSD'
448 15
		];
449 15
		$value = \mb_strtolower($value);
450 15
		return $map[$value] ?? \mb_convert_case($value, MB_CASE_TITLE);
451
	}
452
}