Passed
Push — main ( 0a2d45...0009be )
by Will
02:48
created

browsers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 362
Duplicated Lines 0 %

Test Coverage

Coverage 98.58%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 271
c 2
b 0
f 0
dl 0
loc 362
ccs 347
cts 352
cp 0.9858
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B get() 0 355 6
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
/**
6
 * @phpstan-import-type MatchConfig from config
7
 */
8
class browsers {
9
10
	/**
11
	 * Generates a configuration array for matching browsers
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 68
	public static function get() : array {
16 1
		$fn = [
17 1
			'browserslash' => function (string $value) : array {
18 68
				if (($browser = \mb_strrchr($value, ' ')) !== false) {
19 1
					$value = \ltrim($browser);
20
				}
21 68
				$parts = \explode('/', $value); // split more incase there are more slashes
22 68
				$map = [
23 68
					'opr' => 'Opera',
24 68
					'crios' => 'Chrome',
25 68
					'edg' => 'Edge',
26 68
					'edgios' => 'Edge',
27 68
					'webpositive' => 'WebPositive',
28 68
					'nintendobrowser' => 'NintendoBrowser',
29 68
					'samsungbrowser' => 'SamsungBrowser',
30 68
					'up.browser' => 'UP.Browser',
31 68
					'ucbrowser' => 'UCBrowser',
32 68
					'netfront' => 'NetFront',
33 68
					'seamonkey' => 'SeaMonkey',
34 68
					'icecat' => 'IceCat',
35 68
					'iceape' => 'IceApe',
36 68
					'iceweasel' => 'IceWeasel',
37 68
					'bonecho' => 'BonEcho',
38 68
					'palemoon' => 'PaleMoon',
39 68
					'k-meleon' => 'K-Meleon',
40 68
					'samsungbrowser' => 'Samsung Browser'
41 68
				];
42 68
				$data = ['type' => 'human'];
43 68
				$browser = \mb_strtolower(\array_shift($parts));
44 68
				$data['browser'] = $map[$browser] ?? \mb_convert_case($browser, MB_CASE_TITLE);
45 68
				$data['browserversion'] = null;
46 68
				foreach ($parts AS $item) {
47 68
					if (\strpbrk($item, '1234567890') !== false) {
48 68
						$data['browserversion'] = $item;
49 68
						break;
50
					}
51
				}
52 68
				return $data;
53 1
			},
54 1
			'presto' => function (string $value) : array {
55 4
				$parts = \explode('/', $value, 2);
56 4
				return [
57 4
					'type' => 'human',
58 4
					'browser' => $parts[0],
59 4
					'browserversion' => $parts[1] ?? null,
60 4
					'engine' => 'Presto',
61 4
					'engineversion' => $parts[1] ?? null
62 4
				];
63 1
			},
64 1
			'chromium' => function (string $value) : array {
65 53
				$parts = \explode('/', $value, 3);
66 53
				return [
67 53
					'type' => 'human',
68 53
					'browser' => \mb_convert_case($parts[0], MB_CASE_TITLE),
69 53
					'engine' => 'Blink',
70 53
					'browserversion' => $parts[1] ?? null,
71 53
					'engineversion' => isset($parts[1]) && \strspn($parts[1], '1234567890.') === \strlen($parts[1]) ? $parts[1] : null
72 53
				];
73 1
			}
74 1
		];
75 25
		return [
76 25
			'HeadlessChrome/' =>  [
77 25
				'match' => 'start',
78 25
				'categories' => fn (string $value) : array => [
79
					'type' => 'robot',
80
					'category' => 'crawler',
81
					'browser' => 'HeadlessChrome',
82
					'browserversion' => \mb_substr($value, 15)
83
				]
84 25
			],
85 25
			'IEMobile/' => [
86 25
				'match' => 'start',
87 25
				'categories' => $fn['browserslash']
88 25
			],
89 25
			'Opera Mini/' => [
90 25
				'match' => 'start',
91 25
				'categories' => $fn['presto']
92 25
			],
93 25
			'Opera/' => [
94 25
				'match' => 'start',
95 25
				'categories' => $fn['presto']
96 25
			],
97 25
			'OPR/' =>  [
98 25
				'match' => 'start',
99 25
				'categories' => $fn['browserslash']
100 25
			],
101 25
			'CriOS/' => [
102 25
				'match' => 'start',
103 25
				'categories' => $fn['browserslash']
104 25
			],
105 25
			'Brave/' =>  [
106 25
				'match' => 'start',
107 25
				'categories' => $fn['browserslash']
108 25
			],
109 25
			'Vivaldi/' =>  [
110 25
				'match' => 'start',
111 25
				'categories' => $fn['browserslash']
112 25
			],
113 25
			'Maxthon/' =>  [
114 25
				'match' => 'start',
115 25
				'categories' => $fn['browserslash']
116 25
			],
117 25
			'Maxthon ' =>  [
118 25
				'match' => 'start',
119 25
				'categories' => fn (string $value) : array => [
120 1
					'browser' => 'Maxthon',
121 1
					'browserversion' => \mb_substr($value, 8)
122 1
				]
123 25
			],
124 25
			'Konqueror/' =>  [
125 25
				'match' => 'start',
126 25
				'categories' => $fn['browserslash']
127 25
			],
128 25
			'K-Meleon/' => [
129 25
				'match' => 'start',
130 25
				'categories' => $fn['browserslash']
131 25
			],
132 25
			'UCBrowser/' => [
133 25
				'match' => 'start',
134 25
				'categories' => $fn['browserslash']
135 25
			],
136 25
			'Waterfox/' =>  [
137 25
				'match' => 'start',
138 25
				'categories' => $fn['browserslash']
139 25
			],
140 25
			'PaleMoon/' => [
141 25
				'match' => 'start',
142 25
				'categories' => $fn['browserslash']
143 25
			],
144 25
			'IceWeasel/' => [
145 25
				'match' => 'start',
146 25
				'categories' => $fn['browserslash']
147 25
			],
148 25
			'IceCat/' => [
149 25
				'match' => 'start',
150 25
				'categories' => $fn['browserslash']
151 25
			],
152 25
			'IceApe/' => [
153 25
				'match' => 'start',
154 25
				'categories' => $fn['browserslash']
155 25
			],
156 25
			'Basilisk/' => [
157 25
				'match' => 'start',
158 25
				'categories' => $fn['browserslash']
159 25
			],
160 25
			'SeaMonkey/' => [
161 25
				'match' => 'start',
162 25
				'categories' => $fn['browserslash']
163 25
			],
164 25
			'UP.Browser/' => [
165 25
				'match' => 'start',
166 25
				'categories' => $fn['browserslash']
167 25
			],
168 25
			'Netscape/' => [
169 25
				'match' => 'start',
170 25
				'categories' => $fn['browserslash']
171 25
			],
172 25
			'Thunderbird/' => [
173 25
				'match' => 'start',
174 25
				'categories' => $fn['browserslash']
175 25
			],
176 25
			'Galeon/' => [
177 25
				'match' => 'start',
178 25
				'categories' => $fn['browserslash']
179 25
			],
180 25
			'WebPositive/' => [
181 25
				'match' => 'start',
182 25
				'categories' => $fn['browserslash']
183 25
			],
184 25
			'K-Ninja/' => [
185 25
				'match' => 'start',
186 25
				'categories' => $fn['browserslash']
187 25
			],
188 25
			'SamsungBrowser/' => [
189 25
				'match' => 'start',
190 25
				'categories' => $fn['browserslash']
191 25
			],
192 25
			'NintendoBrowser/' => [
193 25
				'match' => 'start',
194 25
				'categories' => $fn['browserslash']
195 25
			],
196 25
			'Epiphany/' => [
197 25
				'match' => 'start',
198 25
				'categories' => $fn['browserslash']
199 25
			],
200 25
			'Silk/' => [
201 25
				'match' => 'start',
202 25
				'categories' => $fn['browserslash']
203 25
			],
204 25
			'NetFront/' => [
205 25
				'match' => 'start',
206 25
				'categories' => $fn['browserslash']
207 25
			],
208 25
			'Dooble/' => [
209 25
				'match' => 'start',
210 25
				'categories' => $fn['browserslash']
211 25
			],
212 25
			'Falkon/' => [
213 25
				'match' => 'start',
214 25
				'categories' => $fn['browserslash']
215 25
			],
216 25
			'Namoroka/' => [
217 25
				'match' => 'start',
218 25
				'categories' => $fn['browserslash']
219 25
			],
220 25
			'Lynx/' => [
221 25
				'match' => 'start',
222 25
				'categories' => fn (string $value) : array => [
223 1
					'browser' => 'Lynx',
224 1
					'browserversion' => \explode('/', $value, 2)[1] ?? null,
225 1
					'engine' => 'Libwww',
226 1
					'type' => 'human',
227 1
					'category' => 'desktop'
228 1
				]
229 25
			],
230 25
			'Midori' => [
231 25
				'match' => 'start',
232 25
				'categories' => function (string $value) : array {
233 1
					$parts = \explode('/', $value, 2);
234 1
					return [
235 1
						'type' => 'human',
236 1
						'browser' => 'Midori',
237 1
						'browserversion' => $parts[1] ?? \explode(' ', $value, 2)[1] ?? null
238 1
					];
239 25
				}
240 25
			],
241 25
			'PrivacyBrowser/' => [
242 25
				'match' => 'start',
243 25
				'categories' => $fn['browserslash']
244 25
			],
245 25
			'Fennec/' =>  [
246 25
				'match' => 'start',
247 25
				'categories' => fn (string $value) : array => [
248 2
					'type' => 'human',
249 2
					'browser' => 'Fennec',
250 2
					'engine' => 'Gecko',
251 2
					'browserversion' => \mb_substr($value, 7),
252 2
					'engineversion' => \mb_substr($value, 7)
253 2
				]
254 25
			],
255 25
			'Firefox/' =>  [
256 25
				'match' => 'start',
257 25
				'categories' => function (string $value) use ($fn) : array {
258 23
					$data = $fn['browserslash']($value);
259 23
					return \array_merge($data, [
260 23
						'engine' => 'Gecko',
261 23
						'engineversion' => $data['browserversion'] ?? null
262 23
					]);
263 25
				}
264 25
			],
265 25
			' Firefox/' =>  [
266 25
				'match' => 'any',
267 25
				'categories' => function (string $value) use ($fn) : array {
268 1
					$data = $fn['browserslash']($value);
269 1
					return \array_merge($data, [
270 1
						'engine' => 'Gecko',
271 1
						'engineversion' => $data['browserversion'] ?? null
272 1
					]);
273 25
				}
274 25
			],
275 25
			'Firefox' =>  [
276 25
				'match' => 'exact',
277 25
				'categories' => [
278 25
					'type' => 'human',
279 25
					'engine' => 'Gecko',
280 25
					'browser' => 'Firefox'
281 25
				]
282 25
			],
283 25
			'Minimo/' =>  [
284 25
				'match' => 'start',
285 25
				'categories' => function (string $value) use ($fn) : array {
286 1
					$data = $fn['browserslash']($value);
287 1
					return \array_merge($data, [
288 1
						'engine' => 'Gecko'
289 1
					]);
290 25
				}
291 25
			],
292 25
			'BonEcho/' =>  [
293 25
				'match' => 'start',
294 25
				'categories' => function (string $value) use ($fn) : array {
295 1
					$data = $fn['browserslash']($value);
296 1
					return \array_merge($data, [
297 1
						'engine' => 'Gecko'
298 1
					]);
299 25
				}
300 25
			],
301 25
			'Links/' =>  [
302 25
				'match' => 'start',
303 25
				'categories' => $fn['browserslash']
304 25
			],
305 25
			'Links' =>  [
306 25
				'match' => 'exact',
307 25
				'categories' => fn (string $value, int $i, array $tokens) => [
308 1
					'type' => 'human',
309 1
					'browser' => $value,
310 1
					'browserversion' => $tokens[$i + 1]
311 1
				]
312 25
			],
313 25
			'Elinks/' =>  [
314 25
				'match' => 'start',
315 25
				'categories' => $fn['browserslash']
316 25
			],
317 25
			'ELinks' =>  [
318 25
				'match' => 'exact',
319 25
				'categories' => fn (string $value, int $i, array $tokens) => [
320 1
					'type' => 'human',
321 1
					'browser' => $value,
322 1
					'browserversion' => $tokens[$i + 1]
323 1
				]
324 25
			],
325 25
			'Edg/' =>  [
326 25
				'match' => 'start',
327 25
				'categories' => $fn['browserslash']
328 25
			],
329 25
			'Edge/' =>  [
330 25
				'match' => 'start',
331 25
				'categories' => $fn['browserslash']
332 25
			],
333 25
			'EdgiOS/' =>  [
334 25
				'match' => 'start',
335 25
				'categories' => $fn['browserslash']
336 25
			],
337 25
			'MSIE ' =>  [
338 25
				'match' => 'start',
339 25
				'categories' => fn (string $value) : array => [
340 7
					'type' => 'human',
341 7
					'browser' => 'Internet Explorer',
342 7
					'browserversion' => \mb_substr($value, 5),
343 7
					'engine' => 'Trident'
344 7
				]
345 25
			],
346 25
			'Cronet/' => [
347 25
				'match' => 'start',
348 25
				'categories' => $fn['chromium']
349 25
			],
350 25
			'Chromium/' => [
351 25
				'match' => 'start',
352 25
				'categories' => $fn['chromium']
353 25
			],
354 25
			'Chrome/' => [
355 25
				'match' => 'start',
356 25
				'categories' => $fn['chromium']
357 25
			],
358 25
			'Safari/' =>  [
359 25
				'match' => 'start',
360 25
				'categories' => $fn['browserslash']
361 25
			],
362 25
			'Mozilla/' => [
363 25
				'match' => 'start',
364 25
				'categories' => $fn['browserslash']
365 25
			],
366 25
			'rv:' => [
367 25
				'match' => 'start',
368 25
				'categories' => fn (string $value) : array => [
369 33
					'browserversion' => \mb_substr($value, 3)
370 33
				]
371 25
			]
372 25
		];
373
	}
374
}