Passed
Push — main ( 607587...1999b9 )
by Will
03:01
created

agentzero::getTokens()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 32
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

Changes 0
Metric Value
cc 7
eloc 13
c 0
b 0
f 0
nc 6
nop 3
dl 0
loc 32
ccs 13
cts 14
cp 0.9286
crap 7.0178
rs 8.8333
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 97
	private function __construct(string $ua, \stdClass $data) {
65 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
		$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 97
	protected static function getTokens(string $ua, array $single, array $ignore) : array|false {
150
151
		// prepare regexp
152 97
		$single = \implode('|', \array_map('\\preg_quote', $single));
153 97
		$pattern = '/\{[^}]++\}|[^()\[\];,\/  _-](?:(?<!'.$single.') (?!https?:\/\/)|(?<=[a-z])\([^)]+\)|[^()\[\];,\/ ]*)*[^()\[\];,\/  _-](?:\/[^;,()\[\]  ]++)?|[0-9]/i';
154
155
		// split up ua string
156 97
		if (\preg_match_all($pattern, $ua, $match)) {
157
158
			// userland token processing
159 97
			$tokens = [];
160 97
			foreach ($match[0] AS $key => $item) {
161 97
				$lower = \mb_strtolower($item);
162
163
				// special case for handling like
164 97
				if (\str_starts_with($lower, 'like ')) {
165
166
					// chop off words up to a useful token e.g. Platform/Version
167 76
					if (\str_contains($item, '/') && ($pos = \mb_strrpos($item, ' ')) !== false) {
168 76
						$tokens[$key] = \mb_substr($item, $pos + 1);
169
					}
170
171
				// check token is not ignored
172 97
				} elseif (!\in_array($lower, $ignore)) {
173 97
					$tokens[$key] = $item;
174
				}
175
			}
176
177
			// rekey and return
178 97
			return \array_values($tokens);
179
		}
180
		return false;
181
	}
182
183
	/**
184
	 * Parses a User Agent string
185
	 * 
186
	 * @param string $ua The User Agent string to be parsed
187
	 * @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed
188
	 */
189 97
	public static function parse(string $ua) : agentzero|false {
190 97
		if (($config = config::get()) === null) {
191
192 97
		} elseif (($tokens = self::getTokens($ua, $config['single'], $config['ignore'])) !== false) {
193
194
			// extract UA info
195 97
			$browser = new \stdClass();
196 97
			$tokenslower = \array_map('mb_strtolower', $tokens);
197 97
			foreach ($config['match'] AS $key => $item) {
198 97
				$item->match($browser, $key, $tokens, $tokenslower);
199
			}
200 97
			return new agentzero($ua, $browser);
201
		}
202
		return false;
203
	}
204
}