Passed
Push — main ( 5784d3...50dae2 )
by Will
12:43 queued 09:23
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 41
	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
					$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
			'WinNT' => [
111 1
				'match' => 'start',
112 1
				'categories' => fn (string $value) : array => [
113 1
					'type' => 'human',
114 1
					'category' => 'desktop',
115 1
					'architecture' => 'x86',
116 1
					'bits' => 32,
117 1
					'kernel' => 'Windows NT',
118 1
					'platform' => 'Windows',
119 1
					'platformversion' => \mb_substr($value, 5)
120 1
				]
121 1
			],
122 1
			'Windows' => [
123 1
				'match' => 'any',
124 1
				'categories' => $fn['platformwindows']
125 1
			],
126 1
			'Mac OS X' => [
127 1
				'match' => 'any',
128 1
				'categories' => function (string $value) : array {
129 25
					$version = \str_replace('_', '.', \mb_substr($value, \mb_stripos($value, 'Mac OS X') + 9));
130 25
					$register = $version && \intval(\explode('.', $version)[1] ?? 0) >= 6 ? 64 : null;
131 25
					return [
132 25
						'type' => 'human',
133 25
						'category' => 'desktop',
134 25
						'kernel' => 'Linux',
135 25
						'platform' => 'Mac OS X',
136 25
						'platformversion' => $version === '' ? null : $version,
137 25
						'bits' => $register
138 25
					];
139 1
				}
140 1
			],
141 1
			'AppleTV' => [
142 1
				'match' => 'exact',
143 1
				'categories' => [
144 1
					'type' => 'human',
145 1
					'category' => 'tv',
146 1
					'device' => 'AppleTV',
147 1
					'platform' => 'tvOS',
148 1
					'bits' => 64
149 1
				]
150 1
			],
151 1
			'iOS/' => [
152 1
				'match' => 'start',
153 1
				'categories' => fn (string $value) : array => [
154 1
					'type' => 'human',
155 1
					'category' => 'mobile',
156 1
					'platform' => 'iOS',
157 1
					'platformversion' => \mb_substr($value, 4)
158 1
				]
159 1
			],
160 1
			'CrOS' => [
161 1
				'match' => 'start',
162 1
				'categories' => function (string $value) : array {
163 4
					$parts = \explode(' ', $value);
164 4
					return [
165 4
						'type' => 'human',
166 4
						'category' => 'desktop',
167 4
						'platform' => 'Chrome OS',
168 4
						'platformversion' => $parts[2] ?? null
169 4
					];
170 1
				}
171 1
			],
172 1
			'Kindle/' => [
173 1
				'match' => 'start',
174 1
				'categories' => fn (string $value) : array => [
175 1
					'type' => 'human',
176 1
					'category' => 'ebook',
177 1
					'platform' => 'Kindle',
178 1
					'platformversion' => \mb_substr($value, 7)
179 1
				]
180 1
			],
181 1
			'Tizen' => [
182 1
				'match' => 'start',
183 1
				'categories' => function (string $value) : array {
184 1
					$parts = \explode(' ', $value, 2);
185 1
					return [
186 1
						'type' => 'human',
187 1
						'category' => 'desktop',
188 1
						'kernel' => 'Linux',
189 1
						'platform' => 'Tizen',
190 1
						'platformversion' => $parts[1] ?? null
191 1
					];
192 1
				}
193 1
			],
194 1
			'Ubuntu' => [
195 1
				'match' => 'start',
196 1
				'categories' => $fn['platformlinux']
197 1
			],
198 1
			'Kubuntu' => [
199 1
				'match' => 'start',
200 1
				'categories' => $fn['platformlinux']
201 1
			],
202 1
			'Mint' => [
203 1
				'match' => 'start',
204 1
				'categories' => $fn['platformlinux']
205 1
			],
206 1
			'SUSE' => [
207 1
				'match' => 'start',
208 1
				'categories' => $fn['platformlinux']
209 1
			],
210 1
			'Red Hat/' => [
211 1
				'match' => 'start',
212 1
				'categories' => $fn['platformlinux']
213 1
			],
214 1
			'Debian' => [
215 1
				'match' => 'start',
216 1
				'categories' => $fn['platformlinux']
217 1
			],
218 1
			'Darwin' => [
219 1
				'match' => 'start',
220 1
				'categories' => $fn['platformlinux']
221 1
			],
222 1
			'Fedora' => [
223 1
				'match' => 'start',
224 1
				'categories' => $fn['platformlinux']
225 1
			],
226 1
			'CentOS' => [
227 1
				'match' => 'start',
228 1
				'categories' => $fn['platformlinux']
229 1
			],
230 1
			'Rocky' => [
231 1
				'match' => 'start',
232 1
				'categories' => $fn['platformlinux']
233 1
			],
234 1
			'ArchLinux' => [
235 1
				'match' => 'exact',
236 1
				'categories' => fn () : array => [
237
					'type' => 'human',
238
					'category' => 'desktop',
239
					'kernel' => 'Linux',
240
					'platform' => 'Arch',
241
				]
242 1
			],
243 1
			'Arch' => [
244 1
				'match' => 'exact',
245 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

245
				'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...
246
					'type' => 'human',
247
					'category' => 'desktop',
248
					'kernel' => 'Linux',
249
					'platform' => 'Arch',
250
				]
251 1
			],
252 1
			'Web0S' => [
253 1
				'match' => 'exact',
254 1
				'categories' => $fn['platformlinux']
255 1
			],
256 1
			'webOSTV' => [
257 1
				'match' => 'exact',
258 1
				'categories' => [
259 1
					'type' => 'human',
260 1
					'category' => 'tv',
261 1
					'kernel' => 'Linux',
262 1
					'platform' => 'WebOS'
263 1
				]
264 1
			],
265 1
			'WEBOS' => [
266 1
				'match' => 'start',
267 1
				'categories' => fn (string $value) : array => [
268 1
					'type' => 'human',
269 1
					'category' => 'tv',
270 1
					'kernel' => 'Linux',
271 1
					'platform' => 'WebOS',
272 1
					'platformversion' => \mb_substr($value, 5)
273 1
				]
274 1
			],
275 1
			'SunOS' => [
276 1
				'match' => 'start',
277 1
				'categories' => [
278 1
					'type' => 'human',
279 1
					'category' => 'desktop',
280 1
					'kernel' => 'unix',
281 1
					'platform' => 'Solaris',
282 1
				]
283 1
			],
284 1
			'AmigaOS' => [
285 1
				'match' => 'any',
286 1
				'categories' => function (string $value) : array {
287 3
					if (($pos = \mb_stripos($value, 'AmigaOS')) !== false) {
288 3
						$value = \mb_substr($value, $pos);
289
					}
290 3
					$parts = \explode(' ', $value, 2);
291 3
					return [
292 3
						'type' => 'human',
293 3
						'category' => 'desktop',
294 3
						'kernel' => 'Exec',
295 3
						'platform' => 'AmigaOS',
296 3
						'platformversion' => isset($parts[1]) && \strspn($parts[1], '0123456789.-_') === \strlen($parts[1]) ? $parts[1] : null
297 3
					];
298 1
				}
299 1
			],
300 1
			'Fuchsia' => [
301 1
				'match' => 'exact',
302 1
				'categories' => function (string $value, int $i, array $tokens) : array {
303 1
					$os = \explode(' ', $tokens[++$i], 2);
304 1
					return [
305 1
						'type' => 'human',
306 1
						'category' => 'mobile',
307 1
						'kernel' => 'Zircon',
308 1
						'platform' => 'Fuchsia',
309 1
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
310 1
					];
311 1
				}
312 1
			],
313 1
			'Maemo' => [
314 1
				'match' => 'exact',
315 1
				'categories' => function (string $value, int $i, array $tokens) : array {
316 1
					$os = \explode(' ', $tokens[++$i], 2);
317 1
					return [
318 1
						'type' => 'human',
319 1
						'category' => 'mobile',
320 1
						'kernel' => 'Linux',
321 1
						'platform' => 'Maemo',
322 1
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
323 1
					];
324 1
				}
325 1
			],
326 1
			'J2ME/MIDP' => [
327 1
				'match' => 'exact',
328 1
				'categories' => fn (string $value) : array => [
329 2
					'type' => 'human',
330 2
					'category' => 'mobile',
331 2
					'kernel' => 'Java VM',
332 2
					'platform' => $value
333 2
				]
334 1
			],
335 1
			'Haiku' => [
336 1
				'match' => 'any',
337 1
				'categories' => function (string $value) : array {
338 3
					$parts = \explode('/', $value, 2);
339 3
					return [
340 3
						'type' => 'human',
341 3
						'category' => 'desktop',
342 3
						'kernel' => 'Haiku',
343 3
						'platform' => 'Haiku',
344 3
						'platformversion' => $parts[1] ?? null
345 3
					];
346 1
				}
347 1
			],
348 1
			'BeOS' => [
349 1
				'match' => 'start',
350 1
				'categories' => function (string $value) : array {
351 1
					$parts = \explode('/', $value, 2);
352 1
					return [
353 1
						'type' => 'human',
354 1
						'category' => 'desktop',
355 1
						'kernel' => 'BeOS',
356 1
						'platform' => 'BeOS',
357 1
						'platformversion' => $parts[1] ?? null
358 1
					];
359 1
				}
360 1
			],
361 1
			'Android' => [
362 1
				'match' => 'exact',
363 1
				'categories' => $fn['android']
364 1
			],
365 1
			'Android ' => [
366 1
				'match' => 'start',
367 1
				'categories' => $fn['android']
368 1
			],
369 1
			'Linux' => [
370 1
				'match' => 'any',
371 1
				'categories' => [
372 1
					'kernel' => 'Linux',
373 1
					'platform' => 'Linux'
374 1
				]
375 1
			],
376 1
			'X11' => [
377 1
				'match' => 'exact',
378 1
				'categories' => function (string $value, int $i, array $tokens) : array {
379 28
					$os = \explode(' ', $tokens[++$i] ?? '', 2);
380 28
					return [
381 28
						'category' => 'desktop',
382 28
						'kernel' => 'Linux',
383 28
						'platform' => $os[0] ?: 'X11',
384 28
						'platformversion' => isset($os[1]) && \strspn($os[1], '0123456789.-_', \strlen($os[0])) === \strlen($os[1]) ? $os[1] : null
385 28
					];
386 1
				}
387 1
			],
388 1
			'OS/2' => [
389 1
				'match' => 'exact',
390 1
				'categories' => [
391 1
					'category' => 'desktop',
392 1
					'platform' => 'OS/2'
393 1
				]
394 1
			],
395 1
			'Version/' => [
396 1
				'match' => 'start',
397 1
				'categories' => fn (string $value) : array => [
398 37
					'platformversion' => \mb_substr($value, 8)
399 37
				]
400 1
			]
401 1
		];
402
	}
403
404 10
	protected static function getPlatform(string $value) : string {
405 10
		$map = [
406 10
			'webos' => 'WebOS',
407 10
			'web0s' => 'WebOS',
408 10
			'j2me/midp' => 'J2ME/MIDP',
409 10
			'centos' => 'CentOS',
410 10
			'suse' => 'SUSE',
411 10
			'freebsd' => 'FreeBSD',
412 10
			'openbsd' => 'OpenBSD',
413 10
			'netbsd' => 'NetBSD'
414 10
		];
415 10
		$value = \mb_strtolower($value);
416 10
		return $map[$value] ?? \mb_convert_case($value, MB_CASE_TITLE);
417
	}
418
}