Passed
Push — main ( 4c4711...48372d )
by Will
13:04
created

browsers::get()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 297
Code Lines 222

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 282
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 222
c 2
b 0
f 0
dl 0
loc 297
ccs 282
cts 288
cp 0.9792
rs 8
cc 3
nc 1
nop 0
crap 3

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