Passed
Push — main ( 7300c8...1902e7 )
by Will
13:33
created

browsers::get()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 292
Code Lines 224

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 285
CRAP Score 4.0001

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 224
c 2
b 0
f 0
dl 0
loc 292
ccs 285
cts 291
cp 0.9794
rs 8
cc 4
nc 1
nop 0
crap 4.0001

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
			'chromium' => function (string $value) : array {
58 52
				$parts = \explode('/', $value, 3);
59 52
				return [
60 52
					'type' => 'human',
61 52
					'browser' => \mb_convert_case($parts[0], MB_CASE_TITLE),
62 52
					'engine' => 'Blink',
63 52
					'browserversion' => $parts[1] ?? null,
64 52
					'engineversion' => isset($parts[1]) && \strspn($parts[1], '1234567890.') === \strlen($parts[1]) ? $parts[1] : null
65 52
				];
66 1
			}
67 1
		];
68 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...
69 24
			'HeadlessChrome/' =>  [
70 24
				'match' => 'start',
71 24
				'categories' => fn (string $value) : array => [
72
					'type' => 'robot',
73
					'category' => 'crawler',
74
					'browser' => 'HeadlessChrome',
75
					'browserversion' => \mb_substr($value, 15)
76
				]
77 24
			],
78 24
			'IEMobile/' => [
79 24
				'match' => 'start',
80 24
				'categories' => $fn['browserslash']
81 24
			],
82 24
			'Opera Mini/' => [
83 24
				'match' => 'start',
84 24
				'categories' => $fn['presto']
85 24
			],
86 24
			'Opera/' => [
87 24
				'match' => 'start',
88 24
				'categories' => $fn['presto']
89 24
			],
90 24
			'OPR/' =>  [
91 24
				'match' => 'start',
92 24
				'categories' => $fn['browserslash']
93 24
			],
94 24
			'CriOS/' => [
95 24
				'match' => 'start',
96 24
				'categories' => $fn['browserslash']
97 24
			],
98 24
			'Brave/' =>  [
99 24
				'match' => 'start',
100 24
				'categories' => $fn['browserslash']
101 24
			],
102 24
			'Vivaldi/' =>  [
103 24
				'match' => 'start',
104 24
				'categories' => $fn['browserslash']
105 24
			],
106 24
			'Maxthon/' =>  [
107 24
				'match' => 'start',
108 24
				'categories' => $fn['browserslash']
109 24
			],
110 24
			'Maxthon ' =>  [
111 24
				'match' => 'start',
112 24
				'categories' => fn (string $value) : array => [
113 1
					'browser' => 'Maxthon',
114 1
					'browserversion' => \mb_substr($value, 8)
115 1
				]
116 24
			],
117 24
			'Konqueror/' =>  [
118 24
				'match' => 'start',
119 24
				'categories' => $fn['browserslash']
120 24
			],
121 24
			'K-Meleon/' => [
122 24
				'match' => 'start',
123 24
				'categories' => $fn['browserslash']
124 24
			],
125 24
			'UCBrowser/' => [
126 24
				'match' => 'start',
127 24
				'categories' => $fn['browserslash']
128 24
			],
129 24
			'Waterfox/' =>  [
130 24
				'match' => 'start',
131 24
				'categories' => $fn['browserslash']
132 24
			],
133 24
			'PaleMoon/' => [
134 24
				'match' => 'start',
135 24
				'categories' => $fn['browserslash']
136 24
			],
137 24
			'Iceweasel/' => [
138 24
				'match' => 'start',
139 24
				'categories' => $fn['browserslash']
140 24
			],
141 24
			'IceCat/' => [
142 24
				'match' => 'start',
143 24
				'categories' => $fn['browserslash']
144 24
			],
145 24
			'Basilisk/' => [
146 24
				'match' => 'start',
147 24
				'categories' => $fn['browserslash']
148 24
			],
149 24
			'SeaMonkey/' => [
150 24
				'match' => 'start',
151 24
				'categories' => $fn['browserslash']
152 24
			],
153 24
			'YaBrowser/' => [
154 24
				'match' => 'start',
155 24
				'categories' => $fn['browserslash']
156 24
			],
157 24
			'UP.Browser/' => [
158 24
				'match' => 'start',
159 24
				'categories' => $fn['browserslash']
160 24
			],
161 24
			'Netscape/' => [
162 24
				'match' => 'start',
163 24
				'categories' => $fn['browserslash']
164 24
			],
165 24
			'Thunderbird/' => [
166 24
				'match' => 'start',
167 24
				'categories' => $fn['browserslash']
168 24
			],
169 24
			'Galeon/' => [
170 24
				'match' => 'start',
171 24
				'categories' => $fn['browserslash']
172 24
			],
173 24
			'WebPositive/' => [
174 24
				'match' => 'start',
175 24
				'categories' => $fn['browserslash']
176 24
			],
177 24
			'K-Ninja/' => [
178 24
				'match' => 'start',
179 24
				'categories' => $fn['browserslash']
180 24
			],
181 24
			'OculusBrowser/' => [
182 24
				'match' => 'start',
183 24
				'categories' => $fn['browserslash']
184 24
			],
185 24
			'SamsungBrowser/' => [
186 24
				'match' => 'start',
187 24
				'categories' => $fn['browserslash']
188 24
			],
189 24
			'NintendoBrowser/' => [
190 24
				'match' => 'start',
191 24
				'categories' => $fn['browserslash']
192 24
			],
193 24
			'Epiphany/' => [
194 24
				'match' => 'start',
195 24
				'categories' => $fn['browserslash']
196 24
			],
197 24
			'Silk/' => [
198 24
				'match' => 'start',
199 24
				'categories' => $fn['browserslash']
200 24
			],
201 24
			'NetFront/' => [
202 24
				'match' => 'start',
203 24
				'categories' => $fn['browserslash']
204 24
			],
205 24
			'Dooble/' => [
206 24
				'match' => 'start',
207 24
				'categories' => $fn['browserslash']
208 24
			],
209 24
			'Falkon/' => [
210 24
				'match' => 'start',
211 24
				'categories' => $fn['browserslash']
212 24
			],
213 24
			'Namoroka/' => [
214 24
				'match' => 'start',
215 24
				'categories' => $fn['browserslash']
216 24
			],
217 24
			'Lynx/' => [
218 24
				'match' => 'start',
219 24
				'categories' => fn (string $value) : array => [
220 1
					'browser' => 'Lynx',
221 1
					'browserversion' => \explode('/', $value, 2)[1] ?? null,
222 1
					'engine' => 'Libwww',
223 1
					'type' => 'human',
224 1
					'category' => 'desktop'
225 1
				]
226 24
			],
227 24
			'Midori' => [
228 24
				'match' => 'start',
229 24
				'categories' => function (string $value) : array {
230 1
					$parts = \explode('/', $value, 2);
231 1
					return [
232 1
						'type' => 'human',
233 1
						'browser' => 'Midori',
234 1
						'browserversion' => $parts[1] ?? \explode(' ', $value, 2)[1] ?? null
235 1
					];
236 24
				}
237 24
			],
238 24
			'PrivacyBrowser/' => [
239 24
				'match' => 'start',
240 24
				'categories' => $fn['browserslash']
241 24
			],
242 24
			'Fennec/' =>  [
243 24
				'match' => 'start',
244 24
				'categories' => fn (string $value) : array => [
245 2
					'type' => 'human',
246 2
					'browser' => 'Fennec',
247 2
					'engine' => 'Gecko',
248 2
					'browserversion' => \mb_substr($value, 7),
249 2
					'engineversion' => \mb_substr($value, 7)
250 2
				]
251 24
			],
252 24
			'Firefox/' =>  [
253 24
				'match' => 'start',
254 24
				'categories' => function (string $value) use ($fn) : array {
255 23
					$data = $fn['browserslash']($value);
256 23
					return \array_merge($data, [
257 23
						'engine' => 'Gecko',
258 23
						'engineversion' => $data['browserversion'] ?? null
259 23
					]);
260 24
				}
261 24
			],
262 24
			'Edg/' =>  [
263 24
				'match' => 'start',
264 24
				'categories' => $fn['browserslash']
265 24
			],
266 24
			'Edge/' =>  [
267 24
				'match' => 'start',
268 24
				'categories' => $fn['browserslash']
269 24
			],
270 24
			'EdgiOS/' =>  [
271 24
				'match' => 'start',
272 24
				'categories' => $fn['browserslash']
273 24
			],
274 24
			'MSIE ' =>  [
275 24
				'match' => 'start',
276 24
				'categories' => fn (string $value) : array => [
277 7
					'type' => 'human',
278 7
					'browser' => 'Internet Explorer',
279 7
					'browserversion' => \mb_substr($value, 5),
280 7
					'engine' => 'Trident'
281 7
				]
282 24
			],
283 24
			'Cronet/' => [
284 24
				'match' => 'start',
285 24
				'categories' => $fn['chromium']
286 24
			],
287 24
			'Chromium/' => [
288 24
				'match' => 'start',
289 24
				'categories' => $fn['chromium']
290 24
			],
291 24
			'Chrome/' => [
292 24
				'match' => 'start',
293 24
				'categories' => $fn['chromium']
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
}