Passed
Push — main ( fa30e1...151935 )
by Will
02:55
created

browsers::get()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 201
Code Lines 168

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 196
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 168
c 2
b 0
f 0
dl 0
loc 201
ccs 196
cts 196
cp 1
rs 6.7555
cc 8
nc 1
nop 0
crap 8

How to fix   Long Method   

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 props from config
7
 */
8
class browsers {
9
10
	/**
11
	 * Generates a configuration array for matching browsers
12
	 * 
13
	 * @return props An array with keys representing the string to match, and a value of an array containing parsing and output settings
14
	 */
15 54
	public static function get() : array {
16 1
		$fn = [
17 1
			'browserslash' => function (string $value) : array {
18 54
				if (($browser = \mb_strrchr($value, ' ')) !== false) {
19 1
					$value = \ltrim($browser);
20
				}
21 54
				$parts = \explode('/', $value); // split more incase there are more slashes
22 54
				$map = [
23 54
					'opr' => 'Opera',
24 54
					'crios' => 'Chrome',
25 54
					'edg' => 'Edge',
26 54
					'edgios' => 'Edge',
27 54
					'edga' => 'Edge',
28 54
					'webpositive' => 'WebPositive',
29 54
					'nintendobrowser' => 'NintendoBrowser',
30 54
					'up.browser' => 'UP.Browser',
31 54
					'ucbrowser' => 'UCBrowser',
32 54
					'netfront' => 'NetFront',
33 54
					'seamonkey' => 'SeaMonkey',
34 54
					'icecat' => 'IceCat',
35 54
					'iceape' => 'IceApe',
36 54
					'iceweasel' => 'IceWeasel',
37 54
					'bonecho' => 'BonEcho',
38 54
					'palemoon' => 'PaleMoon',
39 54
					'k-meleon' => 'K-Meleon',
40 54
					'samsungbrowser' => 'Samsung Browser'
41 54
				];
42 54
				$data = ['type' => 'human'];
43 54
				$browser = \mb_strtolower(\array_shift($parts));
44 54
				$data['browser'] = $map[$browser] ?? \mb_convert_case($browser, MB_CASE_TITLE);
45 54
				foreach ($parts AS $item) {
46 54
					if (\strpbrk($item, '1234567890') !== false) {
47 54
						$data['browserversion'] = $item;
48 54
						break;
49
					}
50
				}
51 54
				return $data;
52 1
			},
53 1
			'presto' => function (string $value) : array {
54 4
				$parts = \explode('/', $value, 2);
55 4
				return [
56 4
					'type' => 'human',
57 4
					'browser' => $parts[0],
58 4
					'browserversion' => $parts[1] ?? null,
59 4
					'engine' => 'Presto',
60 4
					'engineversion' => $parts[1] ?? null
61 4
				];
62 1
			},
63 1
			'chromium' => function (string $value) : array {
64 53
				$parts = \explode('/', $value, 3);
65 53
				return [
66 53
					'type' => 'human',
67 53
					'browser' => \mb_convert_case($parts[0], MB_CASE_TITLE),
68 53
					'engine' => 'Blink',
69 53
					'browserversion' => $parts[1] ?? null,
70 53
					'engineversion' => isset($parts[1]) && \strspn($parts[1], '1234567890.') === \strlen($parts[1]) ? $parts[1] : null
71 53
				];
72 1
			},
73 1
			'safari' => function (string $value, int $i, array $tokens) : array {
74 32
				$version = null;
75 32
				foreach ($tokens AS $item) {
76 32
					if (\mb_stripos($item, 'Version/') === 0) {
77 20
						$version = \mb_substr($item, 8);
78
					}
79
				}
80 32
				$parts = \explode('/', $value, 2);
81 32
				return [
82 32
					'type' => 'human',
83 32
					'browser' => 'Safari',
84 32
					'browserversion' => $version ?? $parts[1] ?? null,
85 32
					'engine' => 'WebKit',
86 32
					'engineversion' => $parts[1] ?? null
87 32
				];
88 1
			},
89 1
		];
90 25
		return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('HeadlessCh...on(...) { /* ... */ })) returns the type array<string,hexydec\agentzero\props> which is incompatible with the documented return type hexydec\agentzero\props.
Loading history...
91 25
			'HeadlessChrome/' => new props('start', fn (string $value) : array => [
92 1
				'type' => 'robot',
93 1
				'category' => 'crawler',
94 1
				'browser' => 'HeadlessChrome',
95 1
				'browserversion' => \mb_substr($value, 15)
96 1
			]),
97 25
			'IEMobile/' => new props('start', $fn['browserslash']),
98 25
			'Opera Mini/' => new props('start', $fn['presto']),
99 25
			'Opera/' => new props('start', $fn['presto']),
100 25
			'OPR/' => new props('start', $fn['browserslash']),
101 25
			'CriOS/' => new props('start', $fn['browserslash']),
102 25
			'Brave/' => new props('start', $fn['browserslash']),
103 25
			'Vivaldi/' => new props('start', $fn['browserslash']),
104 25
			'Maxthon/' => new props('start', $fn['browserslash']),
105 25
			'Maxthon ' => new props('start', fn (string $value) : array => [
106 2
				'browser' => 'Maxthon',
107 2
				'browserversion' => \mb_substr($value, 8)
108 2
			]),
109 25
			'Konqueror/' => new props('start', $fn['browserslash']),
110 25
			'K-Meleon/' => new props('start', $fn['browserslash']),
111 25
			'UCBrowser/' => new props('start', $fn['browserslash']),
112 25
			'Waterfox/' => new props('start', $fn['browserslash']),
113 25
			'PaleMoon/' => new props('start', $fn['browserslash']),
114 25
			'IceWeasel/' => new props('start', $fn['browserslash']),
115 25
			'IceCat/' => new props('start', $fn['browserslash']),
116 25
			'IceApe/' => new props('start', $fn['browserslash']),
117 25
			'Basilisk/' => new props('start', $fn['browserslash']),
118 25
			'SeaMonkey/' => new props('start', $fn['browserslash']),
119 25
			'UP.Browser/' => new props('start', $fn['browserslash']),
120 25
			'Netscape/' => new props('start', $fn['browserslash']),
121 25
			'Thunderbird/' => new props('start', $fn['browserslash']),
122 25
			'Galeon/' => new props('start', $fn['browserslash']),
123 25
			'WebPositive/' => new props('start', $fn['browserslash']),
124 25
			'K-Ninja/' => new props('start', $fn['browserslash']),
125 25
			'SamsungBrowser/' => new props('start', $fn['browserslash']),
126 25
			'NintendoBrowser/' => new props('start', $fn['browserslash']),
127 25
			'Epiphany/' => new props('start', $fn['browserslash']),
128 25
			'Silk/' => new props('start', $fn['browserslash']),
129 25
			'NetFront/' => new props('start', $fn['browserslash']),
130 25
			'Dooble/' => new props('start', $fn['browserslash']),
131 25
			'Falkon/' => new props('start', $fn['browserslash']),
132 25
			'Namoroka/' => new props('start', $fn['browserslash']),
133 25
			'Lynx/' => new props('start', fn (string $value) : array => [
134 2
				'browser' => 'Lynx',
135 2
				'browserversion' => \explode('/', $value, 2)[1] ?? null,
136 2
				'engine' => 'Libwww',
137 2
				'type' => 'human',
138 2
				'category' => 'desktop'
139 2
			]),
140 25
			'Midori' => new props('start', function (string $value) : array {
141 1
				$parts = \explode('/', $value, 2);
142 1
				return [
143 1
					'type' => 'human',
144 1
					'browser' => 'Midori',
145 1
					'browserversion' => $parts[1] ?? \explode(' ', $value, 2)[1] ?? null
146 1
				];
147 25
			}),
148 25
			'PrivacyBrowser/' => new props('start', $fn['browserslash']),
149 25
			'Fennec/' => new props('start', fn (string $value) : array => [
150 3
				'type' => 'human',
151 3
				'browser' => 'Fennec',
152 3
				'engine' => 'Gecko',
153 3
				'browserversion' => \mb_substr($value, 7),
154 3
				'engineversion' => \mb_substr($value, 7)
155 3
			]),
156 25
			'Firefox/' => new props('start', function (string $value) use ($fn) : array {
157 23
				$data = $fn['browserslash']($value);
158 23
				return \array_merge($data, [
159 23
					'engine' => 'Gecko',
160 23
					'engineversion' => $data['browserversion'] ?? null
161 23
				]);
162 25
			}),
163 25
			' Firefox/' => new props('any', function (string $value) use ($fn) : array {
164 1
				$data = $fn['browserslash']($value);
165 1
				return \array_merge($data, [
166 1
					'engine' => 'Gecko',
167 1
					'engineversion' => $data['browserversion'] ?? null
168 1
				]);
169 25
			}),
170 25
			'Firefox' => new props('exact', [
171 25
				'type' => 'human',
172 25
				'engine' => 'Gecko',
173 25
				'browser' => 'Firefox'
174 25
			]),
175 25
			'Minimo/' => new props('start', function (string $value) use ($fn) : array {
176 1
				$data = $fn['browserslash']($value);
177 1
				return \array_merge($data, [
178 1
					'engine' => 'Gecko'
179 1
				]);
180 25
			}),
181 25
			'BonEcho/' => new props('start', function (string $value) use ($fn) : array {
182 1
				$data = $fn['browserslash']($value);
183 1
				return \array_merge($data, [
184 1
					'engine' => 'Gecko'
185 1
				]);
186 25
			}),
187 25
			'Links/' => new props('start', $fn['browserslash']),
188 25
			'Links' => new props('exact', fn (string $value, int $i, array $tokens) => [
189 2
				'type' => 'human',
190 2
				'browser' => $value,
191 2
				'browserversion' => $tokens[$i + 1]
192 2
			]),
193 25
			'Elinks/' => new props('start', $fn['browserslash']),
194 25
			'ELinks' => new props('exact', fn (string $value, int $i, array $tokens) => [
195 2
				'type' => 'human',
196 2
				'browser' => $value,
197 2
				'browserversion' => $tokens[$i + 1]
198 2
			]),
199 25
			'Edg/' => new props('start', $fn['browserslash']),
200 25
			'EdgA/' => new props('start', $fn['browserslash']),
201 25
			'Edge/' => new props('start', $fn['browserslash']),
202 25
			'EdgiOS/' => new props('start', $fn['browserslash']),
203 25
			'MSIE ' => new props('start', fn (string $value) : array => [
204 8
				'type' => 'human',
205 8
				'browser' => 'Internet Explorer',
206 8
				'browserversion' => \mb_substr($value, 5),
207 8
				'engine' => 'Trident'
208 8
			]),
209 25
			'Cronet/' => new props('start', $fn['chromium']),
210 25
			'Chromium/' => new props('start', $fn['chromium']),
211 25
			'Chrome/' => new props('start', $fn['chromium']),
212 25
			'Safari/' => new props('start', $fn['safari']),
213 25
			'Mozilla/' => new props('start', $fn['browserslash']),
214 25
			'rv:' => new props('start', fn (string $value) : array => [
215 34
				'browserversion' => \mb_substr($value, 3)
216 34
			])
217 25
		];
218
	}
219
}