Passed
Push — main ( 1902e7...09cef8 )
by Will
02:55
created

browsers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Test Coverage

Coverage 97.97%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 230
c 2
b 0
f 0
dl 0
loc 305
ccs 289
cts 295
cp 0.9797
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B get() 0 298 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
					$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
					'yabrowser' => 'Yandex',
26 68
					'edg' => 'Edge',
27 68
					'edgios' => 'Edge',
28 68
					'webpositive' => 'WebPositive',
29 68
					'oculusbrowser' => 'OculusBrowser',
30 68
					'nintendobrowser' => 'NintendoBrowser',
31 68
					'samsungbrowser' => 'SamsungBrowser',
32 68
					'up.browser' => 'UP.Browser',
33 68
					'ucbrowser' => 'UCBrowser',
34 68
					'netfront' => 'NetFront',
35 68
					'seamonkey' => 'SeaMonkey',
36 68
					'icecat' => 'IceCat',
37 68
					'palemoon' => 'PaleMoon',
38 68
					'k-meleon' => 'K-Meleon',
39 68
					'samsungbrowser' => 'Samsung Browser'
40 68
				];
41 68
				$data = ['type' => 'human'];
42 68
				$browser = \mb_strtolower(\array_shift($parts));
43 68
				$data['browser'] = $map[$browser] ?? \mb_convert_case($browser, MB_CASE_TITLE);
44 68
				$data['browserversion'] = null;
45 68
				foreach ($parts AS $item) {
46 68
					if (\strpbrk($item, '1234567890') !== false) {
47 68
						$data['browserversion'] = $item;
48 68
						break;
49
					}
50
				}
51 68
				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
		];
74 24
		return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('HeadlessCh...on(...) { /* ... */ })) returns the type array<string,array<string,callable|string>> which is incompatible with the documented return type hexydec\agentzero\MatchConfig.
Loading history...
75 24
			'HeadlessChrome/' =>  [
76 24
				'match' => 'start',
77 24
				'categories' => fn (string $value) : array => [
78
					'type' => 'robot',
79
					'category' => 'crawler',
80
					'browser' => 'HeadlessChrome',
81
					'browserversion' => \mb_substr($value, 15)
82
				]
83 24
			],
84 24
			'IEMobile/' => [
85 24
				'match' => 'start',
86 24
				'categories' => $fn['browserslash']
87 24
			],
88 24
			'Opera Mini/' => [
89 24
				'match' => 'start',
90 24
				'categories' => $fn['presto']
91 24
			],
92 24
			'Opera/' => [
93 24
				'match' => 'start',
94 24
				'categories' => $fn['presto']
95 24
			],
96 24
			'OPR/' =>  [
97 24
				'match' => 'start',
98 24
				'categories' => $fn['browserslash']
99 24
			],
100 24
			'CriOS/' => [
101 24
				'match' => 'start',
102 24
				'categories' => $fn['browserslash']
103 24
			],
104 24
			'Brave/' =>  [
105 24
				'match' => 'start',
106 24
				'categories' => $fn['browserslash']
107 24
			],
108 24
			'Vivaldi/' =>  [
109 24
				'match' => 'start',
110 24
				'categories' => $fn['browserslash']
111 24
			],
112 24
			'Maxthon/' =>  [
113 24
				'match' => 'start',
114 24
				'categories' => $fn['browserslash']
115 24
			],
116 24
			'Maxthon ' =>  [
117 24
				'match' => 'start',
118 24
				'categories' => fn (string $value) : array => [
119 1
					'browser' => 'Maxthon',
120 1
					'browserversion' => \mb_substr($value, 8)
121 1
				]
122 24
			],
123 24
			'Konqueror/' =>  [
124 24
				'match' => 'start',
125 24
				'categories' => $fn['browserslash']
126 24
			],
127 24
			'K-Meleon/' => [
128 24
				'match' => 'start',
129 24
				'categories' => $fn['browserslash']
130 24
			],
131 24
			'UCBrowser/' => [
132 24
				'match' => 'start',
133 24
				'categories' => $fn['browserslash']
134 24
			],
135 24
			'Waterfox/' =>  [
136 24
				'match' => 'start',
137 24
				'categories' => $fn['browserslash']
138 24
			],
139 24
			'PaleMoon/' => [
140 24
				'match' => 'start',
141 24
				'categories' => $fn['browserslash']
142 24
			],
143 24
			'Iceweasel/' => [
144 24
				'match' => 'start',
145 24
				'categories' => $fn['browserslash']
146 24
			],
147 24
			'IceCat/' => [
148 24
				'match' => 'start',
149 24
				'categories' => $fn['browserslash']
150 24
			],
151 24
			'Basilisk/' => [
152 24
				'match' => 'start',
153 24
				'categories' => $fn['browserslash']
154 24
			],
155 24
			'SeaMonkey/' => [
156 24
				'match' => 'start',
157 24
				'categories' => $fn['browserslash']
158 24
			],
159 24
			'YaBrowser/' => [
160 24
				'match' => 'start',
161 24
				'categories' => $fn['browserslash']
162 24
			],
163 24
			'UP.Browser/' => [
164 24
				'match' => 'start',
165 24
				'categories' => $fn['browserslash']
166 24
			],
167 24
			'Netscape/' => [
168 24
				'match' => 'start',
169 24
				'categories' => $fn['browserslash']
170 24
			],
171 24
			'Thunderbird/' => [
172 24
				'match' => 'start',
173 24
				'categories' => $fn['browserslash']
174 24
			],
175 24
			'Galeon/' => [
176 24
				'match' => 'start',
177 24
				'categories' => $fn['browserslash']
178 24
			],
179 24
			'WebPositive/' => [
180 24
				'match' => 'start',
181 24
				'categories' => $fn['browserslash']
182 24
			],
183 24
			'K-Ninja/' => [
184 24
				'match' => 'start',
185 24
				'categories' => $fn['browserslash']
186 24
			],
187 24
			'OculusBrowser/' => [
188 24
				'match' => 'start',
189 24
				'categories' => $fn['browserslash']
190 24
			],
191 24
			'SamsungBrowser/' => [
192 24
				'match' => 'start',
193 24
				'categories' => $fn['browserslash']
194 24
			],
195 24
			'NintendoBrowser/' => [
196 24
				'match' => 'start',
197 24
				'categories' => $fn['browserslash']
198 24
			],
199 24
			'Epiphany/' => [
200 24
				'match' => 'start',
201 24
				'categories' => $fn['browserslash']
202 24
			],
203 24
			'Silk/' => [
204 24
				'match' => 'start',
205 24
				'categories' => $fn['browserslash']
206 24
			],
207 24
			'NetFront/' => [
208 24
				'match' => 'start',
209 24
				'categories' => $fn['browserslash']
210 24
			],
211 24
			'Dooble/' => [
212 24
				'match' => 'start',
213 24
				'categories' => $fn['browserslash']
214 24
			],
215 24
			'Falkon/' => [
216 24
				'match' => 'start',
217 24
				'categories' => $fn['browserslash']
218 24
			],
219 24
			'Namoroka/' => [
220 24
				'match' => 'start',
221 24
				'categories' => $fn['browserslash']
222 24
			],
223 24
			'Lynx/' => [
224 24
				'match' => 'start',
225 24
				'categories' => fn (string $value) : array => [
226 1
					'browser' => 'Lynx',
227 1
					'browserversion' => \explode('/', $value, 2)[1] ?? null,
228 1
					'engine' => 'Libwww',
229 1
					'type' => 'human',
230 1
					'category' => 'desktop'
231 1
				]
232 24
			],
233 24
			'Midori' => [
234 24
				'match' => 'start',
235 24
				'categories' => function (string $value) : array {
236 1
					$parts = \explode('/', $value, 2);
237 1
					return [
238 1
						'type' => 'human',
239 1
						'browser' => 'Midori',
240 1
						'browserversion' => $parts[1] ?? \explode(' ', $value, 2)[1] ?? null
241 1
					];
242 24
				}
243 24
			],
244 24
			'PrivacyBrowser/' => [
245 24
				'match' => 'start',
246 24
				'categories' => $fn['browserslash']
247 24
			],
248 24
			'Fennec/' =>  [
249 24
				'match' => 'start',
250 24
				'categories' => fn (string $value) : array => [
251 2
					'type' => 'human',
252 2
					'browser' => 'Fennec',
253 2
					'engine' => 'Gecko',
254 2
					'browserversion' => \mb_substr($value, 7),
255 2
					'engineversion' => \mb_substr($value, 7)
256 2
				]
257 24
			],
258 24
			'Firefox/' =>  [
259 24
				'match' => 'start',
260 24
				'categories' => function (string $value) use ($fn) : array {
261 23
					$data = $fn['browserslash']($value);
262 23
					return \array_merge($data, [
263 23
						'engine' => 'Gecko',
264 23
						'engineversion' => $data['browserversion'] ?? null
265 23
					]);
266 24
				}
267 24
			],
268 24
			'Edg/' =>  [
269 24
				'match' => 'start',
270 24
				'categories' => $fn['browserslash']
271 24
			],
272 24
			'Edge/' =>  [
273 24
				'match' => 'start',
274 24
				'categories' => $fn['browserslash']
275 24
			],
276 24
			'EdgiOS/' =>  [
277 24
				'match' => 'start',
278 24
				'categories' => $fn['browserslash']
279 24
			],
280 24
			'MSIE ' =>  [
281 24
				'match' => 'start',
282 24
				'categories' => fn (string $value) : array => [
283 7
					'type' => 'human',
284 7
					'browser' => 'Internet Explorer',
285 7
					'browserversion' => \mb_substr($value, 5),
286 7
					'engine' => 'Trident'
287 7
				]
288 24
			],
289 24
			'Cronet/' => [
290 24
				'match' => 'start',
291 24
				'categories' => $fn['chromium']
292 24
			],
293 24
			'Chromium/' => [
294 24
				'match' => 'start',
295 24
				'categories' => $fn['chromium']
296 24
			],
297 24
			'Chrome/' => [
298 24
				'match' => 'start',
299 24
				'categories' => $fn['chromium']
300 24
			],
301 24
			'Safari/' =>  [
302 24
				'match' => 'start',
303 24
				'categories' => $fn['browserslash']
304 24
			],
305 24
			'Mozilla/' => [
306 24
				'match' => 'start',
307 24
				'categories' => $fn['browserslash']
308 24
			],
309 24
			'rv:' => [
310 24
				'match' => 'start',
311 24
				'categories' => fn (string $value) : array => [
312 31
					'browserversion' => \mb_substr($value, 3)
313 31
				]
314 24
			]
315 24
		];
316
	}
317
}