Passed
Push — main ( ad9cc7...bacfe9 )
by Will
02:48
created

agentzero::getTokens()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 31
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 31
ccs 11
cts 12
cp 0.9167
rs 9.2222
cc 6
nc 5
nop 3
crap 6.0208
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
class agentzero {
6
7
	// ua string
8
	public readonly string $string;
9
10
	// categories
11
	public readonly ?string $type;
12
	public readonly ?string $category;
13
14
	// device
15
	public readonly ?string $vendor;
16
	public readonly ?string $device;
17
	public readonly ?string $model;
18
	public readonly ?string $build;
19
	public readonly ?int $ram;
20
21
	// architecture
22
	public readonly ?string $processor;
23
	public readonly ?string $architecture;
24
	public readonly ?int $bits;
25
	public readonly ?string $cpu;
26
	public readonly ?int $cpuclock;
27
28
	// platform
29
	public readonly ?string $kernel;
30
	public readonly ?string $platform;
31
	public readonly ?string $platformversion;
32
33
	// browser
34
	public readonly ?string $engine;
35
	public readonly ?string $engineversion;
36
	public readonly ?string $browser;
37
	public readonly ?string $browserversion;
38
	public readonly ?string $language;
39
40
	// app
41
	public readonly ?string $app;
42
	public readonly ?string $appname;
43
	public readonly ?string $appversion;
44
	public readonly ?string $framework;
45
	public readonly ?string $frameworkversion;
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 95
	private function __construct(string $ua, \stdClass $data) {
65 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$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 95
		$this->appname = $data->appname ?? null;
0 ignored issues
show
Bug introduced by
The property appname is declared read-only in hexydec\agentzero\agentzero.
Loading history...
100 95
		$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...
101 95
		$this->framework = $data->framework ?? null;
0 ignored issues
show
Bug introduced by
The property framework is declared read-only in hexydec\agentzero\agentzero.
Loading history...
102 95
		$this->frameworkversion = $data->frameworkversion ?? null;
0 ignored issues
show
Bug introduced by
The property frameworkversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
103 95
		$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...
104
105
		// network
106 95
		$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...
107 95
		$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...
108
109
		// screen
110 95
		$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...
111 95
		$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...
112 95
		$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...
113 95
		$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...
114 95
		$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...
115
	}
116
117
	/**
118
	 * Retrieves calculated properties
119
	 * 
120
	 * @param string $key The name of the property to retrieve
121
	 * @return string|int|null The requested property or null if it doesn't exist
122
	 */
123
	public function __get(string $key) : string|int|null {
124
		switch ($key) {
125
			case 'host':
126
				if ($this->url !== null && ($host = \parse_url($this->url, PHP_URL_HOST)) !== false && $host !== null) {
127
					return \str_starts_with($host, 'www.') ? \substr($host, 4) : $host;
128
				}
129
				return null;
130
			case 'browsermajorversion':
131
			case 'enginemajorversion':
132
			case 'platformmajorversion':
133
			case 'appmajorversion':
134
				$item = \str_replace('major', '', $key);
135
				$value = $this->{$item} ?? null;
136
				return $value === null ? null : \intval(\substr($value, 0, \strspn($value, '0123456789')));
137
		}
138
		return $this->{$key} ?? null;
139
	}
140
141
	/**
142
	 * Extracts tokens from a UA string
143
	 * 
144
	 * @param string $ua The User Agent string to be tokenised
145
	 * @param array<string> $single An array of strings that can appear on their own, enables the tokens to be split correctly
146
	 * @param array<string> $ignore An array of tokens that can be ignored in the UA string
147
	 * @return false|array<int,string> An array of tokens, or false if no tokens could be extracted
148
	 */
149 95
	protected static function getTokens(string $ua, array $single, array $ignore) : array|false {
150
151
		// prepare regexp
152 95
		$single = \implode('|', \array_map('preg_quote', $single));
153 95
		$pattern = '/\{[^}]++\}|[^()\[\];,\/  _-](?:(?<!'.$single.') (?!https?:\/\/)|(?<=[a-z])\([^)]+\)|[^()\[\];,\/ ]*)*[^()\[\];,\/  _-](?:\/[^;,()\[\]  ]++)?|[0-9]/i';
154
155
		// split up ua string
156 95
		if (\preg_match_all($pattern, $ua, $match)) {
157
158
			// remove ignore values
159 95
			$tokens = \array_diff($match[0], $ignore);
160
161
			// special case for handling like
162 95
			foreach ($tokens AS $key => $item) {
163 95
				if (\str_starts_with($item, 'like ')) {
164
165
					// chop off words up to a useful token e.g. Platform/Version
166 6
					if (\str_contains($item, '/') && ($pos = \mb_strrpos($item, ' ')) !== false) {
167 5
						$tokens[$key] = \mb_substr($item, $pos + 1);
168
169
					// just remove the token
170
					} else {
171 1
						unset($tokens[$key]);
172
					}
173
				}
174
			}
175
176
			// rekey and return
177 95
			return \array_values($tokens);
178
		}
179
		return false;
180
	}
181
182
	/**
183
	 * Parses a User Agent string
184
	 * 
185
	 * @param string $ua The User Agent string to be parsed
186
	 * @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed
187
	 */
188 95
	public static function parse(string $ua) : agentzero|false {
189 95
		if (($config = config::get()) === null) {
190
191 95
		} elseif (($tokens = self::getTokens($ua, $config['single'], $config['ignore'])) !== false) {
192
193
			// extract UA info
194 95
			$browser = new \stdClass();
195 95
			$tokenslower = \array_map('mb_strtolower', $tokens);
196 95
			foreach ($config['match'] AS $key => $item) {
197 95
				$item->match($browser, $key, $tokens, $tokenslower);
198
			}
199 95
			return new agentzero($ua, $browser);
200
		}
201
		return false;
202
	}
203
}