Passed
Push — main ( d6bbf3...3c010b )
by Will
02:49
created

agentzero   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 22
eloc 95
c 3
b 0
f 0
dl 0
loc 190
ccs 50
cts 65
cp 0.7692
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 48 1
B __get() 0 16 11
A getTokens() 0 31 6
A parse() 0 13 4
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
/**
6
 * @phpstan-import-type MatchValue from config
7
 */
8
class agentzero {
9
10
	// ua string
11
	public readonly string $string;
12
13
	// categories
14
	public readonly ?string $type;
15
	public readonly ?string $category;
16
17
	// device
18
	public readonly ?string $vendor;
19
	public readonly ?string $device;
20
	public readonly ?string $model;
21
	public readonly ?string $build;
22
	public readonly ?int $ram;
23
24
	// architecture
25
	public readonly ?string $processor;
26
	public readonly ?string $architecture;
27
	public readonly ?int $bits;
28
	public readonly ?string $cpu;
29
	public readonly ?int $cpuclock;
30
31
	// platform
32
	public readonly ?string $kernel;
33
	public readonly ?string $platform;
34
	public readonly ?string $platformversion;
35
36
	// browser
37
	public readonly ?string $engine;
38
	public readonly ?string $engineversion;
39
	public readonly ?string $browser;
40
	public readonly ?string $browserversion;
41
	public readonly ?string $language;
42
43
	// app
44
	public readonly ?string $app;
45
	public readonly ?string $appversion;
46
	public readonly ?string $url;
47
48
	// network
49
	public readonly ?string $nettype;
50
	public readonly ?string $proxy;
51
52
	// screen
53
	public readonly ?int $width;
54
	public readonly ?int $height;
55
	public readonly ?int $dpi;
56
	public readonly ?float $density;
57
	public readonly ?bool $darkmode;
58
59
	/**
60
	 * Constructs a new AgentZero object, private because it can only be created internally
61
	 * 
62
	 * @param \stdClass $data A stdClass object containing the UA details
63
	 */
64 87
	private function __construct(string $ua, \stdClass $data) {
65 87
		$this->string = $ua;
0 ignored issues
show
Bug introduced by
The property string is declared read-only in hexydec\agentzero\agentzero.
Loading history...
66
67
		// categories
68 87
		$this->type = $data->type ?? null;
0 ignored issues
show
Bug introduced by
The property type is declared read-only in hexydec\agentzero\agentzero.
Loading history...
69 87
		$this->category = $data->category ?? null;
0 ignored issues
show
Bug introduced by
The property category is declared read-only in hexydec\agentzero\agentzero.
Loading history...
70
71
		// device
72 87
		$this->vendor = $data->vendor ?? null;
0 ignored issues
show
Bug introduced by
The property vendor is declared read-only in hexydec\agentzero\agentzero.
Loading history...
73 87
		$this->device = $data->device ?? null;
0 ignored issues
show
Bug introduced by
The property device is declared read-only in hexydec\agentzero\agentzero.
Loading history...
74 87
		$this->model = $data->model ?? null;
0 ignored issues
show
Bug introduced by
The property model is declared read-only in hexydec\agentzero\agentzero.
Loading history...
75 87
		$this->build = $data->build ?? null;
0 ignored issues
show
Bug introduced by
The property build is declared read-only in hexydec\agentzero\agentzero.
Loading history...
76 87
		$this->ram = $data->ram ?? null;
0 ignored issues
show
Bug introduced by
The property ram is declared read-only in hexydec\agentzero\agentzero.
Loading history...
77
78
		// architecture
79 87
		$this->processor = $data->processor ?? null;
0 ignored issues
show
Bug introduced by
The property processor is declared read-only in hexydec\agentzero\agentzero.
Loading history...
80 87
		$this->architecture = $data->architecture ?? null;
0 ignored issues
show
Bug introduced by
The property architecture is declared read-only in hexydec\agentzero\agentzero.
Loading history...
81 87
		$this->bits = $data->bits ?? null;
0 ignored issues
show
Bug introduced by
The property bits is declared read-only in hexydec\agentzero\agentzero.
Loading history...
82 87
		$this->cpu = $data->cpu ?? null;
0 ignored issues
show
Bug introduced by
The property cpu is declared read-only in hexydec\agentzero\agentzero.
Loading history...
83 87
		$this->cpuclock = $data->cpuclock ?? null;
0 ignored issues
show
Bug introduced by
The property cpuclock is declared read-only in hexydec\agentzero\agentzero.
Loading history...
84
85
		// platform
86 87
		$this->kernel = $data->kernel ?? null;
0 ignored issues
show
Bug introduced by
The property kernel is declared read-only in hexydec\agentzero\agentzero.
Loading history...
87 87
		$this->platform = $data->platform ?? null;
0 ignored issues
show
Bug introduced by
The property platform is declared read-only in hexydec\agentzero\agentzero.
Loading history...
88 87
		$this->platformversion = $data->platformversion ?? null;
0 ignored issues
show
Bug introduced by
The property platformversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
89
90
		// browser
91 87
		$this->engine = $data->engine ?? null;
0 ignored issues
show
Bug introduced by
The property engine is declared read-only in hexydec\agentzero\agentzero.
Loading history...
92 87
		$this->engineversion = $data->engineversion ?? null;
0 ignored issues
show
Bug introduced by
The property engineversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
93 87
		$this->browser = $data->browser ?? null;
0 ignored issues
show
Bug introduced by
The property browser is declared read-only in hexydec\agentzero\agentzero.
Loading history...
94 87
		$this->browserversion = $data->browserversion ?? null;
0 ignored issues
show
Bug introduced by
The property browserversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
95 87
		$this->language = $data->language ?? null;
0 ignored issues
show
Bug introduced by
The property language is declared read-only in hexydec\agentzero\agentzero.
Loading history...
96
97
		// app
98 87
		$this->app = $data->app ?? null;
0 ignored issues
show
Bug introduced by
The property app is declared read-only in hexydec\agentzero\agentzero.
Loading history...
99 87
		$this->appversion = $data->appversion ?? null;
0 ignored issues
show
Bug introduced by
The property appversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
100 87
		$this->url = $data->url ?? null;
0 ignored issues
show
Bug introduced by
The property url is declared read-only in hexydec\agentzero\agentzero.
Loading history...
101
102
		// network
103 87
		$this->nettype = $data->nettype ?? null;
0 ignored issues
show
Bug introduced by
The property nettype is declared read-only in hexydec\agentzero\agentzero.
Loading history...
104 87
		$this->proxy = $data->proxy ?? null;
0 ignored issues
show
Bug introduced by
The property proxy is declared read-only in hexydec\agentzero\agentzero.
Loading history...
105
106
		// screen
107 87
		$this->width = $data->width ?? null;
0 ignored issues
show
Bug introduced by
The property width is declared read-only in hexydec\agentzero\agentzero.
Loading history...
108 87
		$this->height = $data->height ?? null;
0 ignored issues
show
Bug introduced by
The property height is declared read-only in hexydec\agentzero\agentzero.
Loading history...
109 87
		$this->dpi = $data->dpi ?? null;
0 ignored issues
show
Bug introduced by
The property dpi is declared read-only in hexydec\agentzero\agentzero.
Loading history...
110 87
		$this->density = $data->density ?? null;
0 ignored issues
show
Bug introduced by
The property density is declared read-only in hexydec\agentzero\agentzero.
Loading history...
111 87
		$this->darkmode = $data->darkmode ?? null;
0 ignored issues
show
Bug introduced by
The property darkmode is declared read-only in hexydec\agentzero\agentzero.
Loading history...
112
	}
113
114
	/**
115
	 * Retrieves calculated properties
116
	 * 
117
	 * @param string $key The name of the property to retrieve
118
	 * @return string|int|null The requested property or null if it doesn't exist
119
	 */
120
	public function __get(string $key) : string|int|null {
121
		switch ($key) {
122
			case 'host':
123
				if ($this->url !== null && ($host = \parse_url($this->url, PHP_URL_HOST)) !== false && $host !== null) {
124
					return \str_starts_with($host, 'www.') ? \substr($host, 4) : $host;
125
				}
126
				return null;
127
			case 'browsermajorversion':
128
			case 'enginemajorversion':
129
			case 'platformmajorversion':
130
			case 'appmajorversion':
131
				$item = \str_replace('major', '', $key);
132
				$value = $this->{$item} ?? null;
133
				return $value === null ? null : \intval(\substr($value, 0, \strspn($value, '0123456789')));
134
		}
135
		return $this->{$key} ?? null;
136
	}
137
138
	/**
139
	 * Extracts tokens from a UA string
140
	 * 
141
	 * @param string $ua The User Agent string to be tokenised
142
	 * @param array<string> $single An array of strings that can appear on their own, enables the tokens to be split correctly
143
	 * @param array<string> $ignore An array of tokens that can be ignored in the UA string
144
	 * @return false|array<int,string> An array of tokens, or false if no tokens could be extracted
145
	 */
146 87
	protected static function getTokens(string $ua, array $single, array $ignore) : array|false {
147
148
		// prepare regexp
149 87
		$single = \implode('|', \array_map('preg_quote', $single));
150 87
		$pattern = '/\{[^}]++\}|[^()\[\];,\/  _-](?:(?<!'.$single.') (?!https?:\/\/)|(?<=[a-z])\([^)]+\)|[^()\[\];,\/ ]*)*[^()\[\];,\/  _-](?:\/[^;,()\[\]  ]++)?|[0-9]/i';
151
152
		// split up ua string
153 87
		if (\preg_match_all($pattern, $ua, $match)) {
154
155
			// remove ignore values
156 87
			$tokens = \array_diff($match[0], $ignore);
157
158
			// special case for handling like
159 87
			foreach ($tokens AS $key => $item) {
160 87
				if (\str_starts_with($item, 'like ')) {
161
162
					// chop off words up to a useful token e.g. Platform/Version
163 6
					if (\str_contains($item, '/') && ($pos = \mb_strrpos($item, ' ')) !== false) {
164 5
						$tokens[$key] = \mb_substr($item, $pos + 1);
165
166
					// just remove the token
167
					} else {
168 1
						unset($tokens[$key]);
169
					}
170
				}
171
			}
172
173
			// rekey and return
174 87
			return \array_values($tokens);
175
		}
176
		return false;
177
	}
178
179
	/**
180
	 * Parses a User Agent string
181
	 * 
182
	 * @param string $ua The User Agent string to be parsed
183
	 * @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed
184
	 */
185 87
	public static function parse(string $ua) : agentzero|false {
186 87
		if (($config = config::get()) === null) {
187
188 87
		} elseif (($tokens = self::getTokens($ua, $config['single'], $config['ignore'])) !== false) {
189
190
			// extract UA info
191 87
			$browser = new \stdClass();
192 87
			foreach ($config['match'] AS $key => $item) {
193 87
				$item->match($browser, $key, $tokens);
194
			}
195 87
			return new agentzero($ua, $browser);
196
		}
197
		return false;
198
	}
199
}