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

apps::get()   C

Complexity

Conditions 13
Paths 1

Size

Total Lines 218
Code Lines 150

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 203
CRAP Score 13.0001

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 150
c 1
b 0
f 0
dl 0
loc 218
ccs 203
cts 205
cp 0.9902
rs 5.2933
cc 13
nc 1
nop 0
crap 13.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 39
	public static function get() : array {
16 1
		$fn = [
17 1
			'appslash' => function (string $value) : ?array {
18 39
				if (!\str_starts_with($value, 'AppleWebKit') && !\str_contains($value, '://')) {
19 12
					$parts = \explode('/', $value, 2);
20 12
					return [
21 12
						'app' => $parts[0],
22 12
						'appversion' => $parts[1] ?? null
23 12
					];
24
				}
25 35
				return null;
26 1
			}
27 1
		];
28 1
		return [
29 1
			'com.google.android.apps.' => [
30 1
				'match' => 'any',
31 1
				'categories' => $fn['appslash']
32 1
			],
33 1
			'Instagram' => [
34 1
				'match' => 'any',
35 1
				'categories' => function (string $value, int $i, array $tokens) : array {
36 5
					$data = [
37 5
						'app' => 'Instagram',
38 5
						'appversion' => \explode(' ', $value, 3)[1] ?? null
39 5
					];
40 5
					foreach (\array_slice($tokens, $i + 1) AS $item) {
41 5
						if (\str_starts_with($item, 'scale=')) {
42 2
							$data['density'] = \floatval(\mb_substr($item, 6));
43 5
						} elseif (\str_ends_with($item, 'dpi')) {
44 4
							$data['dpi'] = \intval(\mb_substr($item, 0, -3));
45 5
						} elseif (\str_contains($item, 'x') && \strspn($item, '0123456789x') === \strlen($item)) {
46 5
							list($data['width'], $data['height']) = \array_map('intval', \explode('x', $item, 2));
47
						}
48
					}
49 5
					return $data;
50 1
				}
51 1
			],
52 1
			'GSA/' => [
53 1
				'match' => 'any',
54 1
				'categories' => $fn['appslash']
55 1
			],
56 1
			'DuckDuckGo/' => [
57 1
				'match' => 'start',
58 1
				'categories' => $fn['appslash']
59 1
			],
60 1
			'FlipboardProxy/' => [
61 1
				'match' => 'start',
62 1
				'categories' => $fn['appslash']
63 1
			],
64 1
			'AndroidDownloadManager/' => [
65 1
				'match' => 'start',
66 1
				'categories' => $fn['appslash']
67 1
			],
68 1
			'Google-Read-Aloud' => [
69 1
				'match' => 'exact',
70 1
				'categories' => [
71 1
					'type' => 'human',
72 1
					'app' => 'Google-Read-Aloud'
73 1
				]
74 1
			],
75 1
			'Zoom ' => [
76 1
				'match' => 'start',
77 1
				'categories' => fn (string $value) : array => [
78 1
					'app' => 'Zoom',
79 1
					'appversion' => \mb_substr($value, 5)
80 1
				]
81 1
			],
82
83
			// special parser for Facebook app because it is completely different to any other
84 1
			'FBAN/' => [
85 1
				'match' => 'start',
86 1
				'categories' => function (string $value) : array {
87 4
					$map = [
88 4
						'FBAN/MessengerLiteForiOS' => [
89 4
							'type' => 'human',
90 4
							'app' => 'Facebook Messenger Lite',
91 4
							'platform' => 'iOS'
92 4
						],
93 4
						'FBAN/FB4A' => [
94 4
							'type' => 'human',
95 4
							'app' => 'Facebook',
96 4
							'platform' => 'Android'
97 4
						],
98 4
						'FBAN/FBIOS' => [
99 4
							'type' => 'human',
100 4
							'app' => 'Facebook',
101 4
							'platform' => 'iOS'
102 4
						],
103 4
						'FBAN/FB4FireTV' => [
104 4
							'type' => 'human',
105 4
							'category' => 'tv',
106 4
							'app' => 'Facebook',
107 4
							'platform' => 'Android'
108 4
						],
109 4
						'FBAN/MessengerDesktop' => [
110 4
							'type' => 'human',
111 4
							'category' => 'desktop',
112 4
							'app' => 'Facebook Messenger Desktop'
113 4
						]
114 4
					];
115 4
					return $map[$value] ?? [
116 4
						'app' => 'Facebook',
117 4
						'type' => 'human'
118
					];
119 1
				}
120 1
			],
121 1
			'FB_IAB/' => [
122 1
				'match' => 'start',
123 1
				'categories' => [
124 1
					'app' => 'Facebook'
125 1
				]
126 1
			],
127 1
			'FBAV/' => [
128 1
				'match' => 'start',
129 1
				'categories' => fn (string $value) : array => [
130 8
					'appversion' => \mb_substr($value, 5)
131 8
				]
132 1
			],
133 1
			'FBMF/' => [
134 1
				'match' => 'start',
135 1
				'categories' => fn (string $value) : array => [
136 1
					'vendor' => \mb_substr($value, 5)
137 1
				]
138 1
			],
139 1
			'FBDV/' => [
140 1
				'match' => 'start',
141 1
				'categories' => fn (string $value) : array => [
142 2
					'device' => \mb_substr($value, 5)
143 2
				]
144 1
			],
145 1
			'FBMD/' => [
146 1
				'match' => 'start',
147 1
				'categories' => fn (string $value) : array => [
148 2
					'model' => \mb_substr($value, 5)
149 2
				]
150 1
			],
151 1
			'FBLC/' => [
152 1
				'match' => 'start',
153 1
				'categories' => function (string $value) : array {
154 4
					$parts = \explode('-', \str_replace('_', '-', \mb_substr($value, 5)), 4);
155 4
					$suffix = $parts[2] ?? $parts[1] ?? null;
156 4
					return [
157 4
						'language' => \strtolower($parts[0]).($suffix !== null ? '-'.\strtoupper($suffix) : '')
158 4
					];
159 1
				}
160 1
			],
161 1
			'FBDM/' => [
162 1
				'match' => 'start',
163 1
				'categories' => function (string $value) : array {
164 1
					$data = [];
165 1
					foreach (\explode(',', \trim(\mb_substr($value, 5), '{}')) AS $item) {
166 1
						$parts = \explode('=', $item);
167 1
						if (!empty($parts[1])) {
168 1
							if (\is_numeric($parts[1])) {
169 1
								$parts[1] = \str_contains($parts[1], '.') ? \floatval($parts[1]) : \intval($parts[1]);
170
							}
171 1
							$data[$parts[0]] = $parts[1];
172
						}
173
					}
174 1
					return $data;
175 1
				}
176 1
			],
177 1
			'width=' => [
178 1
				'match' => 'start',
179 1
				'categories' => fn (string $value) : array => [
180 1
					'width' => \intval(\mb_substr($value, 6))
181 1
				]
182 1
			],
183 1
			'height=' => [
184 1
				'match' => 'start',
185 1
				'categories' => fn (string $value) : array => [
186 1
					'height' => \intval(\mb_substr($value, 7))
187 1
				]
188 1
			],
189 1
			'dpi=' => [
190 1
				'match' => 'start',
191 1
				'categories' => fn (string $value) : array => [
192
					'dpi' => \mb_substr($value, 4)
193
				]
194 1
			],
195 1
			'FBSN/' => [
196 1
				'match' => 'start',
197 1
				'categories' => fn (string $value) : array => [
198 2
					'platform' => \mb_substr($value, 5)
199 2
				]
200 1
			],
201 1
			'FBSV' => [
202 1
				'match' => 'start',
203 1
				'categories' => fn (string $value) : array => [
204 2
					'platformversion' => \mb_substr($value, 5)
205 2
				]
206 1
			],
207
208
			// other
209 1
			'MAUI' => [
210 1
				'match' => 'start',
211 1
				'categories' => fn (string $value) : array => [
212 1
					'type' => 'human',
213 1
					'app' => $value
214 1
				]
215 1
			],
216 1
			'AppName/' => [
217 1
				'match' => 'start',
218 1
				'categories' => fn(string $value) : array => [
219 1
					'app' => \mb_substr($value, 8)
220 1
				]
221 1
			],
222 1
			'app_version/' => [
223 1
				'match' => 'start',
224 1
				'categories' => fn(string $value) : array => [
225 1
					'appversion' => \mb_substr($value, 12)
226 1
				]
227 1
			],
228
				
229
			// generic
230 1
			'App' => [
231 1
				'match' => 'any',
232 1
				'categories' => $fn['appslash']
233 1
			],
234 1
		];
235
	}
236
}