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

platforms::get()   F

Complexity

Conditions 29
Paths 1

Size

Total Lines 391
Code Lines 297

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 362
CRAP Score 29.0761

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 297
c 2
b 0
f 0
dl 0
loc 391
ccs 362
cts 379
cp 0.9551
rs 3.3333
cc 29
nc 1
nop 0
crap 29.0761

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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