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

apps::get()   F

Complexity

Conditions 20
Paths 1

Size

Total Lines 292
Code Lines 204

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 273
CRAP Score 20.0001

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 204
c 1
b 0
f 0
dl 0
loc 292
ccs 273
cts 275
cp 0.9927
rs 3.3333
cc 20
nc 1
nop 0
crap 20.0001

How to fix   Long Method    Complexity   

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 apps {
9
10
	/**
11
	 * Generates a configuration array for matching apps
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 40
	public static function get() : array {
16 1
		$fn = [
17 1
			'appslash' => function (string $value, int $i, array $tokens, string $match) : ?array {
18 40
				if (\mb_stripos($value, 'AppleWebKit') !== 0 && !\str_contains($value, '://')) {
19 14
					$parts = \explode('/', $value, 4);
20 14
					$offset = isset($parts[2]) ? 1 : 0;
21 14
					$map = [
22 14
						'YaApp_iOS' => 'Yandex',
23 14
						'YaApp_Android' => 'Yandex',
24 14
						'YaSearchApp' => 'Yandex',
25 14
						'LinkedInApp' => 'LinkedIn',
26 14
						'[LinkedInApp]' => 'LinkedIn',
27 14
						'GoogleApp' => 'Google',
28 14
						'com.zhiliaoapp.musically' => 'TikTok',
29 14
						'com.google.android.apps.searchlite' => 'Google (Lite)',
30 14
						'com.google.photos' => 'Google Photos',
31 14
						'com.google.ios.youtube' => 'YouTube',
32 14
						'AlohaBrowserApp' => 'Aloha'
33 14
					];
34 14
					$app = $parts[0 + $offset];
35 14
					if (\mb_stripos($app, \rtrim($match, '/')) !== false) { // check the match is before the slash
36 13
						return [
37 13
							'app' => $map[$app] ?? $app,
38 13
							'appversion' => $parts[1 + $offset] ?? null
39 13
						];
40
					}
41
				}
42 38
				return null;
43 1
			},
44 1
			'langslash' => function (string $value) : array {
45 6
				$parts = \explode('-', \str_replace('_', '-', \explode('/', $value, 2)[1]), 4);
46 6
				$suffix = $parts[2] ?? $parts[1] ?? null;
47 6
				return [
48 6
					'language' => \strtolower($parts[0]).($suffix !== null ? '-'.\strtoupper($suffix) : '')
49 6
				];
50 1
			}
51 1
		];
52 1
		return [
53 1
			'OcIdWebView' => [
54 1
				'match' => 'exact',
55 1
				'categories' => function (string $value, int $i, array $tokens) : array {
56 1
					$data = [
57 1
						'app' => $value
58 1
					];
59 1
					if (!empty($tokens[$i+1]) && ($json = \json_decode($tokens[$i+1], true)) !== null) {
60 1
						$data['appversion'] = $json['appVersion'] ?? null;
61 1
						$data['darkmode'] = isset($json['isDarkTheme']) ? \boolval($json['isDarkTheme']) : null;
62
					}
63 1
					return $data;
64 1
				}
65 1
			],
66 1
			'com.google.android.apps.' => [
67 1
				'match' => 'any',
68 1
				'categories' => $fn['appslash']
69 1
			],
70 1
			'Instagram' => [
71 1
				'match' => 'any',
72 1
				'categories' => function (string $value, int $i, array $tokens) : array {
73 5
					$data = [
74 5
						'app' => 'Instagram',
75 5
						'appversion' => \explode(' ', $value, 3)[1] ?? null
76 5
					];
77 5
					foreach (\array_slice($tokens, $i + 1) AS $item) {
78 5
						if (\str_starts_with($item, 'scale=')) {
79 2
							$data['density'] = \floatval(\mb_substr($item, 6));
80 5
						} elseif (\str_ends_with($item, 'dpi')) {
81 4
							$data['dpi'] = \intval(\mb_substr($item, 0, -3));
82 5
						} elseif (\str_contains($item, 'x') && \strspn($item, '0123456789x') === \strlen($item)) {
83 5
							list($data['width'], $data['height']) = \array_map('intval', \explode('x', $item, 2));
84
						}
85
					}
86 5
					return $data;
87 1
				}
88 1
			],
89 1
			'GSA/' => [
90 1
				'match' => 'any',
91 1
				'categories' => fn (string $value) : array => [
92 3
					'app' => 'Google',
93 3
					'appversion' => \mb_substr($value, 4)
94 3
				]
95 1
			],
96 1
			'DuckDuckGo/' => [
97 1
				'match' => 'start',
98 1
				'categories' => $fn['appslash']
99 1
			],
100 1
			'FlipboardProxy/' => [
101 1
				'match' => 'start',
102 1
				'categories' => $fn['appslash']
103 1
			],
104 1
			'AndroidDownloadManager/' => [
105 1
				'match' => 'start',
106 1
				'categories' => $fn['appslash']
107 1
			],
108 1
			'Google-Read-Aloud' => [
109 1
				'match' => 'exact',
110 1
				'categories' => [
111 1
					'type' => 'human',
112 1
					'app' => 'Google-Read-Aloud'
113 1
				]
114 1
			],
115 1
			'Zoom ' => [
116 1
				'match' => 'start',
117 1
				'categories' => fn (string $value) : array => [
118 1
					'app' => 'Zoom',
119 1
					'appversion' => \mb_substr($value, 5)
120 1
				]
121 1
			],
122
123
			// special parser for Facebook app because it is completely different to any other
124 1
			'FBAN/' => [
125 1
				'match' => 'any',
126 1
				'categories' => function (string $value) : array {
127 4
					$map = [
128 4
						'FBAN/MessengerLiteForiOS' => [
129 4
							'type' => 'human',
130 4
							'app' => 'Facebook Messenger Lite',
131 4
							'platform' => 'iOS'
132 4
						],
133 4
						'FBAN/FB4A' => [
134 4
							'type' => 'human',
135 4
							'app' => 'Facebook',
136 4
							'platform' => 'Android'
137 4
						],
138 4
						'FBAN/FBIOS' => [
139 4
							'type' => 'human',
140 4
							'app' => 'Facebook',
141 4
							'platform' => 'iOS'
142 4
						],
143 4
						'FBAN/FB4FireTV' => [
144 4
							'type' => 'human',
145 4
							'category' => 'tv',
146 4
							'app' => 'Facebook',
147 4
							'platform' => 'Android'
148 4
						],
149 4
						'FBAN/MessengerDesktop' => [
150 4
							'type' => 'human',
151 4
							'category' => 'desktop',
152 4
							'app' => 'Facebook Messenger Desktop'
153 4
						],
154 4
						'FacebookCanvasDesktop FBAN/GamesWindowsDesktopApp' => [
155 4
							'type' => 'human',
156 4
							'platform' => 'Windows',
157 4
							'category' => 'desktop',
158 4
							'app' => 'Facebook Gamesroom'
159 4
						]
160 4
					];
161 4
					return $map[$value] ?? [
162 4
						'app' => 'Facebook',
163 4
						'type' => 'human'
164
					];
165 1
				}
166 1
			],
167 1
			'FB_IAB/' => [
168 1
				'match' => 'start',
169 1
				'categories' => [
170 1
					'app' => 'Facebook'
171 1
				]
172 1
			],
173 1
			'FBAV/' => [
174 1
				'match' => 'start',
175 1
				'categories' => fn (string $value) : array => [
176 8
					'appversion' => \mb_substr($value, 5)
177 8
				]
178 1
			],
179 1
			'FBMF/' => [
180 1
				'match' => 'start',
181 1
				'categories' => fn (string $value) : array => [
182 1
					'vendor' => \mb_substr($value, 5)
183 1
				]
184 1
			],
185 1
			'FBDV/' => [
186 1
				'match' => 'start',
187 1
				'categories' => fn (string $value) : array => [
188 2
					'device' => \mb_substr($value, 5)
189 2
				]
190 1
			],
191 1
			'FBMD/' => [
192 1
				'match' => 'start',
193 1
				'categories' => fn (string $value) : array => [
194 2
					'model' => \mb_substr($value, 5)
195 2
				]
196 1
			],
197 1
			'FBLC/' => [
198 1
				'match' => 'start',
199 1
				'categories' => $fn['langslash']
200 1
			],
201 1
			'FBDM/' => [
202 1
				'match' => 'start',
203 1
				'categories' => function (string $value) : array {
204 1
					$data = [];
205 1
					foreach (\explode(',', \trim(\mb_substr($value, 5), '{}')) AS $item) {
206 1
						$parts = \explode('=', $item);
207 1
						if (!empty($parts[1])) {
208 1
							if (\is_numeric($parts[1])) {
209 1
								$parts[1] = \str_contains($parts[1], '.') ? \floatval($parts[1]) : \intval($parts[1]);
210
							}
211 1
							$data[$parts[0]] = $parts[1];
212
						}
213
					}
214 1
					return $data;
215 1
				}
216 1
			],
217 1
			'width=' => [
218 1
				'match' => 'start',
219 1
				'categories' => fn (string $value) : array => [
220 1
					'width' => \intval(\mb_substr($value, 6))
221 1
				]
222 1
			],
223 1
			'height=' => [
224 1
				'match' => 'start',
225 1
				'categories' => fn (string $value) : array => [
226 1
					'height' => \intval(\mb_substr($value, 7))
227 1
				]
228 1
			],
229 1
			'dpi=' => [
230 1
				'match' => 'start',
231 1
				'categories' => fn (string $value) : array => [
232
					'dpi' => \mb_substr($value, 4)
233
				]
234 1
			],
235 1
			'FBSN/' => [
236 1
				'match' => 'start',
237 1
				'categories' => fn (string $value) : array => [
238 2
					'platform' => \mb_substr($value, 5)
239 2
				]
240 1
			],
241 1
			'FBSV' => [
242 1
				'match' => 'start',
243 1
				'categories' => fn (string $value) : array => [
244 2
					'platformversion' => \mb_substr($value, 5)
245 2
				]
246 1
			],
247 1
			'isDarkMode/' => [
248 1
				'match' => 'start',
249 1
				'categories' => function (string $value) : array {
250 1
					$mode = \mb_substr($value, 11);
251 1
					return [
252 1
						'darkmode' => \in_array($mode, ['0', '1'], true) ? \boolval($mode) : null
253 1
					];
254 1
				}
255 1
			],
256 1
			'ByteFullLocale/' => [
257 1
				'match' => 'start',
258 1
				'categories' => $fn['langslash']
259 1
			],
260 1
			'NetType/' => [
261 1
				'match' => 'start',
262 1
				'categories' => fn (string $value) : array => [
263 2
					'nettype' => \mb_convert_case(\mb_substr($value, 8), MB_CASE_UPPER)
264 2
				]
265 1
			],
266
267
			// other
268 1
			'MAUI' => [
269 1
				'match' => 'start',
270 1
				'categories' => fn (string $value) : array => [
271 1
					'type' => 'human',
272 1
					'app' => $value
273 1
				]
274 1
			],
275
276
			// TikTok
277 1
			'AppName/' => [
278 1
				'match' => 'start',
279 1
				'categories' => fn(string $value) : array => [
280 2
					'app' => $value === 'AppName/musical_ly' ? 'TikTok' : \mb_substr($value, 8)
281 2
				]
282 1
			],
283 1
			'app_version/' => [
284 1
				'match' => 'start',
285 1
				'categories' => fn(string $value) : array => [
286 2
					'appversion' => \mb_substr($value, 12)
287 2
				]
288 1
			],
289 1
			'AppVersion/' => [
290 1
				'match' => 'any',
291 1
				'categories' => fn(string $value) : array => [
292 1
					'appversion' => \explode('/', $value, 2)[1]
293 1
				]
294 1
			],
295 1
			'musical_ly' => [
296 1
				'match' => 'start',
297 1
				'categories' => fn(string $value) : array => [
298 1
					'app' => 'TikTok',
299 1
					'appversion' => \str_replace('_', '.', \mb_substr(\explode(' ', $value, 2)[0], 11))
300 1
				]
301 1
			],
302
				
303
			// generic
304 1
			'App' => [
305 1
				'match' => 'any',
306 1
				'categories' => $fn['appslash']
307 1
			],
308 1
		];
309
	}
310
}