Passed
Push — main ( e55150...6ce6d7 )
by Will
02:49
created

browsers::get()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 186
Code Lines 157

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 183
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 157
c 2
b 0
f 0
dl 0
loc 186
ccs 183
cts 183
cp 1
rs 7.3777
cc 6
nc 1
nop 0
crap 6

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 props from config
7
 */
8
class browsers {
9
10
	/**
11
	 * Generates a configuration array for matching browsers
12
	 * 
13
	 * @return props An array with keys representing the string to match, and a value of an array containing parsing and output settings
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
					'edga' => 'Edge',
28 68
					'webpositive' => 'WebPositive',
29 68
					'nintendobrowser' => 'NintendoBrowser',
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 [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('HeadlessCh...on(...) { /* ... */ })) returns the type array<string,hexydec\agentzero\props> which is incompatible with the documented return type hexydec\agentzero\props.
Loading history...
76 25
			'HeadlessChrome/' => new props('start', fn (string $value) : array => [
77 1
				'type' => 'robot',
78 1
				'category' => 'crawler',
79 1
				'browser' => 'HeadlessChrome',
80 1
				'browserversion' => \mb_substr($value, 15)
81 1
			]),
82 25
			'IEMobile/' => new props('start', $fn['browserslash']),
83 25
			'Opera Mini/' => new props('start', $fn['presto']),
84 25
			'Opera/' => new props('start', $fn['presto']),
85 25
			'OPR/' => new props('start', $fn['browserslash']),
86 25
			'CriOS/' => new props('start', $fn['browserslash']),
87 25
			'Brave/' => new props('start', $fn['browserslash']),
88 25
			'Vivaldi/' => new props('start', $fn['browserslash']),
89 25
			'Maxthon/' => new props('start', $fn['browserslash']),
90 25
			'Maxthon ' => new props('start', fn (string $value) : array => [
91 2
				'browser' => 'Maxthon',
92 2
				'browserversion' => \mb_substr($value, 8)
93 2
			]),
94 25
			'Konqueror/' => new props('start', $fn['browserslash']),
95 25
			'K-Meleon/' => new props('start', $fn['browserslash']),
96 25
			'UCBrowser/' => new props('start', $fn['browserslash']),
97 25
			'Waterfox/' => new props('start', $fn['browserslash']),
98 25
			'PaleMoon/' => new props('start', $fn['browserslash']),
99 25
			'IceWeasel/' => new props('start', $fn['browserslash']),
100 25
			'IceCat/' => new props('start', $fn['browserslash']),
101 25
			'IceApe/' => new props('start', $fn['browserslash']),
102 25
			'Basilisk/' => new props('start', $fn['browserslash']),
103 25
			'SeaMonkey/' => new props('start', $fn['browserslash']),
104 25
			'UP.Browser/' => new props('start', $fn['browserslash']),
105 25
			'Netscape/' => new props('start', $fn['browserslash']),
106 25
			'Thunderbird/' => new props('start', $fn['browserslash']),
107 25
			'Galeon/' => new props('start', $fn['browserslash']),
108 25
			'WebPositive/' => new props('start', $fn['browserslash']),
109 25
			'K-Ninja/' => new props('start', $fn['browserslash']),
110 25
			'SamsungBrowser/' => new props('start', $fn['browserslash']),
111 25
			'NintendoBrowser/' => new props('start', $fn['browserslash']),
112 25
			'Epiphany/' => new props('start', $fn['browserslash']),
113 25
			'Silk/' => new props('start', $fn['browserslash']),
114 25
			'NetFront/' => new props('start', $fn['browserslash']),
115 25
			'Dooble/' => new props('start', $fn['browserslash']),
116 25
			'Falkon/' => new props('start', $fn['browserslash']),
117 25
			'Namoroka/' => new props('start', $fn['browserslash']),
118 25
			'Lynx/' => new props('start', fn (string $value) : array => [
119 2
				'browser' => 'Lynx',
120 2
				'browserversion' => \explode('/', $value, 2)[1] ?? null,
121 2
				'engine' => 'Libwww',
122 2
				'type' => 'human',
123 2
				'category' => 'desktop'
124 2
			]),
125 25
			'Midori' => new props('start', function (string $value) : array {
126 1
				$parts = \explode('/', $value, 2);
127 1
				return [
128 1
					'type' => 'human',
129 1
					'browser' => 'Midori',
130 1
					'browserversion' => $parts[1] ?? \explode(' ', $value, 2)[1] ?? null
131 1
				];
132 25
			}),
133 25
			'PrivacyBrowser/' => new props('start', $fn['browserslash']),
134 25
			'Fennec/' => new props('start', fn (string $value) : array => [
135 3
				'type' => 'human',
136 3
				'browser' => 'Fennec',
137 3
				'engine' => 'Gecko',
138 3
				'browserversion' => \mb_substr($value, 7),
139 3
				'engineversion' => \mb_substr($value, 7)
140 3
			]),
141 25
			'Firefox/' => new props('start', function (string $value) use ($fn) : array {
142 23
				$data = $fn['browserslash']($value);
143 23
				return \array_merge($data, [
144 23
					'engine' => 'Gecko',
145 23
					'engineversion' => $data['browserversion'] ?? null
146 23
				]);
147 25
			}),
148 25
			' Firefox/' => new props('any', function (string $value) use ($fn) : array {
149 1
				$data = $fn['browserslash']($value);
150 1
				return \array_merge($data, [
151 1
					'engine' => 'Gecko',
152 1
					'engineversion' => $data['browserversion'] ?? null
153 1
				]);
154 25
			}),
155 25
			'Firefox' => new props('exact', [
156 25
				'type' => 'human',
157 25
				'engine' => 'Gecko',
158 25
				'browser' => 'Firefox'
159 25
			]),
160 25
			'Minimo/' => new props('start', function (string $value) use ($fn) : array {
161 1
				$data = $fn['browserslash']($value);
162 1
				return \array_merge($data, [
163 1
					'engine' => 'Gecko'
164 1
				]);
165 25
			}),
166 25
			'BonEcho/' => new props('start', function (string $value) use ($fn) : array {
167 1
				$data = $fn['browserslash']($value);
168 1
				return \array_merge($data, [
169 1
					'engine' => 'Gecko'
170 1
				]);
171 25
			}),
172 25
			'Links/' => new props('start', $fn['browserslash']),
173 25
			'Links' => new props('exact', fn (string $value, int $i, array $tokens) => [
174 2
				'type' => 'human',
175 2
				'browser' => $value,
176 2
				'browserversion' => $tokens[$i + 1]
177 2
			]),
178 25
			'Elinks/' => new props('start', $fn['browserslash']),
179 25
			'ELinks' => new props('exact', fn (string $value, int $i, array $tokens) => [
180 2
				'type' => 'human',
181 2
				'browser' => $value,
182 2
				'browserversion' => $tokens[$i + 1]
183 2
			]),
184 25
			'Edg/' => new props('start', $fn['browserslash']),
185 25
			'EdgA/' => new props('start', $fn['browserslash']),
186 25
			'Edge/' => new props('start', $fn['browserslash']),
187 25
			'EdgiOS/' => new props('start', $fn['browserslash']),
188 25
			'MSIE ' => new props('start', fn (string $value) : array => [
189 8
				'type' => 'human',
190 8
				'browser' => 'Internet Explorer',
191 8
				'browserversion' => \mb_substr($value, 5),
192 8
				'engine' => 'Trident'
193 8
			]),
194 25
			'Cronet/' => new props('start', $fn['chromium']),
195 25
			'Chromium/' => new props('start', $fn['chromium']),
196 25
			'Chrome/' => new props('start', $fn['chromium']),
197 25
			'Safari/' => new props('start', $fn['browserslash']),
198 25
			'Mozilla/' => new props('start', $fn['browserslash']),
199 25
			'rv:' => new props('start', fn (string $value) : array => [
200 34
				'browserversion' => \mb_substr($value, 3)
201 34
			])
202 25
		];
203
	}
204
}