Passed
Push — main ( 5a01f2...ad28d2 )
by Will
03:19
created

hints::parse()   C

Complexity

Conditions 16
Paths 3

Size

Total Lines 82
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 54
CRAP Score 16.3873

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 55
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 82
ccs 54
cts 61
cp 0.8852
crap 16.3873
rs 5.5666

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
class hints {
6
7 100
	public static function parse(string &$ua, array $hints) : \stdClass {
8 100
		$map = [
9 100
			'sec-ch-ua-mobile' => fn (\stdClass $obj, string $value) : string => $obj->category = $value === '?1' ? 'mobile' : 'desktop',
10 100
			'sec-ch-ua-platform' => fn (\stdClass $obj, string $value) : ?string => $obj->platform = \trim($value, '"') ?: null,
11 100
			'sec-ch-ua-platform-version' => function (\stdClass $obj, string $value) : void {
12 1
				$value = \trim($value, '"');
13 1
				if ($obj->platform === 'Windows') {
14 1
					$map = [
15 1
						'8',
16 1
						'10.1507',
17 1
						'10.1511',
18 1
						'10.1607',
19 1
						'10.1703',
20 1
						'10.1709',
21 1
						'10.1803',
22 1
						'10.1809',
23 1
						'10.1903',
24 1
						'10.1909',
25 1
						'10.2004',
26 1
						'10.20H2',
27 1
						'10.21H1',
28 1
						'10.21H2'
29 1
					];
30 1
					$major = \intval($value);
31 1
					if (isset($map[$major])) {
32 1
						$value = $map[$major] ?? '11';
33
					}
34
				}
35 1
				$obj->platformversion = $value ?: null;
36 100
			},
37 100
			'sec-ch-ua-model' => fn (\stdClass $obj, string $value) : ?string => $obj->model = \trim($value, '"') ?: null,
38 100
			'sec-ch-ua-full-version-list' => function (\stdClass $obj, string $value, string &$ua) : void {
39 1
				$brands = [];
40
41
				// process brands string
42 1
				foreach (\explode(',', $value) AS $item) {
43 1
					$parts = \explode('";v="', \trim($item, ' "'));
44
		
45
					// remove GREASE brands
46 1
					if (\strcspn($parts[0], '()-./:;=?_') === \strlen($parts[0])) {
47 1
						$brands[$parts[0]] = $parts[1];
48
					}
49
				}
50
51
				// remove Chromium if Google Chrome present
52 1
				if (isset($brands['Chromium'], $brands['Google Chrome'])) {
53 1
					unset($brands['Chromium']);
54
				}
55
56
				// sort the values in importance order
57 1
				$sort = ['Chromium', 'Google Chrome'];
58 1
				$count = \count($sort);
59 1
				\uksort($brands, function (string $a, string $b) use ($sort, $count) : int {
60
					$aval = $bval = $count;
61
					foreach ($sort AS $key => $item) {
62
						if ($a === $item) {
63
							$aval = $key;
64
						}
65
						if ($b === $item) {
66
							$bval = $key;
67
						}
68
					}
69
					return $aval - $bval;
70 1
				});
71
72
				// add to UA string
73 1
				foreach ($brands AS $key => $item) {
74 1
					$ua = $key.'/'.$item.' '.$ua;
75
				}
76 100
			},
77 100
			'device-memory' => fn (\stdClass $obj, string $value) : int => $obj->ram = \intval(\floatval($value) * 1024),
78 100
			'width' => fn (\stdClass $obj, string $value) : int => $obj->width = \intval($value),
79 100
			'ect' => fn (\stdClass $obj, string $value) : string => $obj->nettype = $value
80 100
		];
81 100
		$obj = new \stdClass();
82 100
		foreach ($hints AS $key => $item) {
83 1
			$key = \strtolower($key);
84 1
			if (isset($map[$key])) {
85 1
				$map[$key]($obj, $item, $ua);
86
			}
87
		}
88 100
		return $obj;
89
	}
90
}