other::get()   B
last analyzed

Complexity

Conditions 11
Paths 1

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 11.0594

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 37
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 54
ccs 35
cts 38
cp 0.9211
crap 11.0594
rs 7.3166

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
class other {
6
7
	/**
8
	 * Generates a configuration array for matching other
9
	 * 
10
	 * @return array<string,props> An array with keys representing the string to match, and values a props object defining how to generate the match and which properties to set
11
	 */
12 2
	public static function get() : array {
13 1
		return [
14 1
			'{' => new props('any', function (string $value) : ?array {
15 2
				if (($start = \mb_strpos($value, '{')) === false) {
16
17 2
				} elseif (($end = \mb_strpos($value, '}', $start)) !== false) {
18 1
					$json = \mb_substr($value, $start, $end - $start + 1);
19
20
					// decode JSON
21 1
					if (($data = \json_decode($json, true)) !== null) {
22 1
						$cat = [
23 1
							'type' => 'human'
24 1
						];
25 1
						$data = \array_change_key_case((array) $data);
26
27
						// special case for chrome - browser
28 1
						if (($data['app'] ?? null) === 'com.google.chrome.ios') {
29
							$cat['browser'] = 'Chrome';
30
							$cat['browserversion'] = $data['appversion'] ?? null;
31
							unset($data['app'], $data['appversion']);
32
						}
33
34
						// map values
35 1
						$map = [
36 1
							'os' => 'platform',
37 1
							'osversion' => 'platformversion',
38 1
							'isdarktheme' => 'darkmode',
39 1
							'locale' => 'language'
40 1
						];
41 1
						$fields = ['os', 'osversion', 'platform', 'platformversion', 'app', 'appversion', 'isdarktheme', 'locale', 'device'];
42 1
						foreach ($fields AS $item) {
43 1
							if (isset($data[$item])) {
44 1
								if ($item === 'app') {
45 1
									$cat['app'] = apps::getApp($data[$item]);
46 1
									$cat['appname'] = $data[$item];
47 1
								} elseif ($item === 'device') {
48 1
									$cat = \array_merge($cat, devices::getDevice($data[$item]));
49 1
								} elseif ($item === 'os') {
50 1
									$parts = \explode(' ', $data[$item]);
51 1
									if (\is_numeric($parts[1] ?? null)) {
52 1
										$cat['platform'] = $parts[0];
53 1
										$cat['platformversion'] = $parts[1];
54
									} else {
55 1
										$cat['platform'] = $data[$item];
56
									}
57
								} else {
58 1
									$cat[$map[$item] ?? $item] = $data[$item];
59
								}
60
							}
61
						}
62 1
						return $cat;
63
					}
64
				}
65 1
				return null;
66 1
			})
67 1
		];
68
	}
69
}