Passed
Push — main ( 0009be...03a93c )
by Will
02:49
created

platforms   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 445
Duplicated Lines 0 %

Test Coverage

Coverage 97.63%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 32
eloc 330
c 3
b 0
f 0
dl 0
loc 445
ccs 412
cts 422
cp 0.9763
rs 9.84

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlatform() 0 13 1
F get() 0 420 31
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]) === 2 || (\strlen($tokens[$i]) === 5 && \mb_strpos($tokens[$i], '_') === 2))) {
67 1
					$i++;
68
				}
69
70
				// only where the token length is greater than 2, or it doesn't contain a /, or if it does it is Build/
71 31
				$device = empty($tokens[$i]) || \strlen($tokens[$i]) <= 2 || (\str_contains($tokens[$i], '/') && \mb_stripos($tokens[$i], 'Build/') === false) ? [] : devices::getDevice($tokens[$i]);
72 31
				return \array_merge($device, [
73 31
					'type' => 'human',
74 31
					'platform' => 'Android',
75 31
					'platformversion' => $os[1] ?? null
76 31
				]);
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
			'Win32' => [
113 1
				'match' => 'exact',
114 1
				'categories' => [
115 1
					'type' => 'human',
116 1
					'category' => 'desktop',
117 1
					'architecture' => 'x86',
118 1
					'bits' => 32,
119 1
					'platform' => 'Windows'
120 1
				]
121 1
			],
122 1
			'Win64' => [
123 1
				'match' => 'exact',
124 1
				'categories' => [
125 1
					'type' => 'human',
126 1
					'category' => 'desktop',
127 1
					'bits' => 64,
128 1
					'platform' => 'Windows'
129 1
				]
130 1
			],
131 1
			'WinNT' => [
132 1
				'match' => 'start',
133 1
				'categories' => fn (string $value) : array => [
134 1
					'type' => 'human',
135 1
					'category' => 'desktop',
136 1
					'architecture' => 'x86',
137 1
					'bits' => 32,
138 1
					'kernel' => 'Windows NT',
139 1
					'platform' => 'Windows',
140 1
					'platformversion' => \mb_substr($value, 5)
141 1
				]
142 1
			],
143 1
			'Windows' => [
144 1
				'match' => 'any',
145 1
				'categories' => $fn['platformwindows']
146 1
			],
147 1
			'Mac OS X' => [
148 1
				'match' => 'any',
149 1
				'categories' => function (string $value) : array {
150 25
					$version = \str_replace('_', '.', \mb_substr($value, \mb_stripos($value, 'Mac OS X') + 9));
151 25
					$register = $version && \intval(\explode('.', $version)[1] ?? 0) >= 6 ? 64 : null;
152 25
					return [
153 25
						'type' => 'human',
154 25
						'category' => 'desktop',
155 25
						'kernel' => 'Linux',
156 25
						'platform' => 'Mac OS X',
157 25
						'platformversion' => $version === '' ? null : $version,
158 25
						'bits' => $register
159 25
					];
160 1
				}
161 1
			],
162 1
			'AppleTV' => [
163 1
				'match' => 'exact',
164 1
				'categories' => [
165 1
					'type' => 'human',
166 1
					'category' => 'tv',
167 1
					'device' => 'AppleTV',
168 1
					'platform' => 'tvOS',
169 1
					'bits' => 64
170 1
				]
171 1
			],
172 1
			'iOS/' => [
173 1
				'match' => 'start',
174 1
				'categories' => fn (string $value) : array => [
175 1
					'type' => 'human',
176 1
					'category' => 'mobile',
177 1
					'platform' => 'iOS',
178 1
					'platformversion' => \mb_substr($value, 4)
179 1
				]
180 1
			],
181 1
			'CrOS' => [
182 1
				'match' => 'start',
183 1
				'categories' => function (string $value) : array {
184 4
					$parts = \explode(' ', $value);
185 4
					return [
186 4
						'type' => 'human',
187 4
						'category' => 'desktop',
188 4
						'platform' => 'Chrome OS',
189 4
						'platformversion' => $parts[2] ?? null
190 4
					];
191 1
				}
192 1
			],
193 1
			'Kindle/' => [
194 1
				'match' => 'start',
195 1
				'categories' => fn (string $value) : array => [
196 1
					'type' => 'human',
197 1
					'category' => 'ebook',
198 1
					'platform' => 'Kindle',
199 1
					'platformversion' => \mb_substr($value, 7)
200 1
				]
201 1
			],
202 1
			'Tizen' => [
203 1
				'match' => 'start',
204 1
				'categories' => function (string $value) : array {
205 1
					$parts = \explode(' ', $value, 2);
206 1
					return [
207 1
						'type' => 'human',
208 1
						'category' => 'desktop',
209 1
						'kernel' => 'Linux',
210 1
						'platform' => 'Tizen',
211 1
						'platformversion' => $parts[1] ?? null
212 1
					];
213 1
				}
214 1
			],
215 1
			'Ubuntu' => [
216 1
				'match' => 'start',
217 1
				'categories' => $fn['platformlinux']
218 1
			],
219 1
			'Kubuntu' => [
220 1
				'match' => 'start',
221 1
				'categories' => $fn['platformlinux']
222 1
			],
223 1
			'Mint' => [
224 1
				'match' => 'start',
225 1
				'categories' => $fn['platformlinux']
226 1
			],
227 1
			'SUSE' => [
228 1
				'match' => 'start',
229 1
				'categories' => $fn['platformlinux']
230 1
			],
231 1
			'Red Hat/' => [
232 1
				'match' => 'start',
233 1
				'categories' => $fn['platformlinux']
234 1
			],
235 1
			'Debian' => [
236 1
				'match' => 'start',
237 1
				'categories' => $fn['platformlinux']
238 1
			],
239 1
			'Darwin' => [
240 1
				'match' => 'start',
241 1
				'categories' => $fn['platformlinux']
242 1
			],
243 1
			'Fedora' => [
244 1
				'match' => 'start',
245 1
				'categories' => $fn['platformlinux']
246 1
			],
247 1
			'CentOS' => [
248 1
				'match' => 'start',
249 1
				'categories' => $fn['platformlinux']
250 1
			],
251 1
			'Rocky' => [
252 1
				'match' => 'start',
253 1
				'categories' => $fn['platformlinux']
254 1
			],
255 1
			'Alma' => [
256 1
				'match' => 'start',
257 1
				'categories' => $fn['platformlinux']
258 1
			],
259 1
			'Gentoo' => [
260 1
				'match' => 'start',
261 1
				'categories' => $fn['platformlinux']
262 1
			],
263 1
			'Slackware' => [
264 1
				'match' => 'start',
265 1
				'categories' => $fn['platformlinux']
266 1
			],
267 1
			'ArchLinux' => [
268 1
				'match' => 'exact',
269 1
				'categories' => fn () : array => [
270
					'type' => 'human',
271
					'category' => 'desktop',
272
					'kernel' => 'Linux',
273
					'platform' => 'Arch',
274
				]
275 1
			],
276 1
			'Arch' => [
277 1
				'match' => 'exact',
278 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

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