Passed
Push — main ( 1da062...26c21a )
by Will
02:48
created

browsers::get()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 292
Code Lines 218

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 277
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 218
c 2
b 0
f 0
dl 0
loc 292
ccs 277
cts 283
cp 0.9788
rs 8
cc 2
nc 1
nop 0
crap 2

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 66
	public static function get() : array {
16 1
		$fn = [
17 1
			'browserslash' => function (string $value) : array {
18 66
				if (($browser = \mb_strrchr($value, ' ')) !== false) {
19
					$value = \ltrim($browser);
20
				}
21 66
				$parts = \explode('/', $value, 3); // split more incase there are more slashes
22 66
				$map = [
23 66
					'opr' => 'Opera',
24 66
					'crios' => 'Chrome',
25 66
					'yabrowser' => 'Yandex',
26 66
					'edg' => 'Edge',
27 66
					'edgios' => 'Edge',
28 66
					'webpositive' => 'WebPositive',
29 66
					'oculusbrowser' => 'OculusBrowser',
30 66
					'nintendobrowser' => 'NintendoBrowser',
31 66
					'samsungbrowser' => 'SamsungBrowser',
32 66
					'up.browser' => 'UP.Browser',
33 66
					'ucbrowser' => 'UCBrowser',
34 66
					'netfront' => 'NetFront',
35 66
					'seamonkey' => 'SeaMonkey',
36 66
					'icecat' => 'IceCat',
37 66
					'palemoon' => 'PaleMoon',
38 66
					'k-meleon' => 'K-Meleon',
39 66
					'samsungbrowser' => 'Samsung Browser'
40 66
				];
41 66
				return [
42 66
					'type' => 'human',
43 66
					'browser' => $map[\mb_strtolower($parts[0])] ?? \mb_convert_case($parts[0], MB_CASE_TITLE),
44 66
					'browserversion' => $parts[1] ?? null
45 66
				];
46 1
			},
47 1
			'presto' => function (string $value) : array {
48 4
				$parts = \explode('/', $value, 2);
49 4
				return [
50 4
					'type' => 'human',
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['browserslash']
190 24
			],
191 24
			'NetFront/' => [
192 24
				'match' => 'start',
193 24
				'categories' => $fn['browserslash']
194 24
			],
195 24
			'Dooble/' => [
196 24
				'match' => 'start',
197 24
				'categories' => $fn['browserslash']
198 24
			],
199 24
			'Falkon/' => [
200 24
				'match' => 'start',
201 24
				'categories' => $fn['browserslash']
202 24
			],
203 24
			'Namoroka/' => [
204 24
				'match' => 'start',
205 24
				'categories' => $fn['browserslash']
206 24
			],
207 24
			'Lynx/' => [
208 24
				'match' => 'start',
209 24
				'categories' => fn (string $value) : array => [
210 1
					'browser' => 'Lynx',
211 1
					'browserversion' => \explode('/', $value, 2)[1] ?? null,
212 1
					'engine' => 'Libwww',
213 1
					'type' => 'human',
214 1
					'category' => 'desktop'
215 1
				]
216 24
			],
217 24
			'Midori' => [
218 24
				'match' => 'start',
219 24
				'categories' => function (string $value) : array {
220 1
					$parts = \explode('/', $value, 2);
221 1
					return [
222 1
						'type' => 'human',
223 1
						'browser' => 'Midori',
224 1
						'browserversion' => $parts[1] ?? \explode(' ', $value, 2)[1] ?? null
225 1
					];
226 24
				}
227 24
			],
228 24
			'PrivacyBrowser/' => [
229 24
				'match' => 'start',
230 24
				'categories' => $fn['browserslash']
231 24
			],
232 24
			'Fennec/' =>  [
233 24
				'match' => 'start',
234 24
				'categories' => fn (string $value) : array => [
235 2
					'type' => 'human',
236 2
					'browser' => 'Fennec',
237 2
					'engine' => 'Gecko',
238 2
					'browserversion' => \mb_substr($value, 7),
239 2
					'engineversion' => \mb_substr($value, 7)
240 2
				]
241 24
			],
242 24
			'Firefox/' =>  [
243 24
				'match' => 'start',
244 24
				'categories' => function (string $value) use ($fn) : array {
245 23
					$data = $fn['browserslash']($value);
246 23
					return \array_merge($data, [
247 23
						'engine' => 'Gecko',
248 23
						'engineversion' => $data['browserversion'] ?? null
249 23
					]);
250 24
				}
251 24
			],
252 24
			'Edg/' =>  [
253 24
				'match' => 'start',
254 24
				'categories' => $fn['browserslash']
255 24
			],
256 24
			'Edge/' =>  [
257 24
				'match' => 'start',
258 24
				'categories' => $fn['browserslash']
259 24
			],
260 24
			'EdgiOS/' =>  [
261 24
				'match' => 'start',
262 24
				'categories' => $fn['browserslash']
263 24
			],
264
			// 'BOIE9' => [
265
			// 	'match' => 'exact',
266
			// 	'categories' => [
267
			// 		'browser' => 'Internet Explorer',
268
			// 		'browserversion' => '9',
269
			// 		'engine' => 'Trident'
270
			// 	]
271
			// ],
272 24
			'MSIE ' =>  [
273 24
				'match' => 'start',
274 24
				'categories' => fn (string $value) : array => [
275 7
					'type' => 'human',
276 7
					'browser' => 'Internet Explorer',
277 7
					'browserversion' => \mb_substr($value, 5),
278 7
					'engine' => 'Trident'
279 7
				]
280 24
			],
281 24
			'Chromium/' => [
282 24
				'match' => 'start',
283 24
				'categories' => $fn['browserslash']
284 24
			],
285 24
			'Chrome/' => [
286 24
				'match' => 'start',
287 24
				'categories' => fn (string $value) : array => [
288 52
					'type' => 'human',
289 52
					'browser' => 'Chrome',
290 52
					'engine' => 'Blink',
291 52
					'browserversion' => \mb_substr($value, 7),
292 52
					'engineversion' => \mb_substr($value, 7)
293 52
				]
294 24
			],
295 24
			'Safari/' =>  [
296 24
				'match' => 'start',
297 24
				'categories' => $fn['browserslash']
298 24
			],
299 24
			'Mozilla/' => [
300 24
				'match' => 'start',
301 24
				'categories' => $fn['browserslash']
302 24
			],
303 24
			'rv:' => [
304 24
				'match' => 'start',
305 24
				'categories' => fn (string $value) : array => [
306 31
					'browserversion' => \mb_substr($value, 3)
307 31
				]
308 24
			]
309 24
		];
310
	}
311
}