Passed
Push — main ( b74a3d...1ce707 )
by Will
12:35
created

agentzero   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Test Coverage

Coverage 85.29%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 137
c 4
b 0
f 0
dl 0
loc 262
ccs 87
cts 102
cp 0.8529
rs 9.76
wmc 33

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 54 1
B __get() 0 16 11
B parseDefault() 0 37 7
B getTokens() 0 32 7
B parse() 0 24 7
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 $browserstatus;
39
	public readonly ?string $browserreleased;
40
	public readonly ?string $browserlatest;
41
	public readonly ?string $language;
42
43
	// app
44
	public readonly ?string $app;
45
	public readonly ?string $appname;
46
	public readonly ?string $appversion;
47
	public readonly ?string $framework;
48
	public readonly ?string $frameworkversion;
49
	public readonly ?string $url;
50
51
	// network
52
	public readonly ?string $nettype;
53
	public readonly ?string $proxy;
54
55
	// screen
56
	public readonly ?int $width;
57
	public readonly ?int $height;
58
	public readonly ?int $dpi;
59
	public readonly ?float $density;
60
	public readonly ?bool $darkmode;
61
62
	/**
63
	 * Constructs a new AgentZero object, private because it can only be created internally
64
	 * 
65
	 * @param string $ua The user-agent string
66
	 * @param \stdClass $data A stdClass object containing the UA details
67
	 */
68 99
	private function __construct(string $ua, \stdClass $data) {
69 99
		$this->string = $ua;
0 ignored issues
show
Bug introduced by
The property string is declared read-only in hexydec\agentzero\agentzero.
Loading history...
70
71
		// categories
72 99
		$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...
73 99
		$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...
74
75
		// device
76 99
		$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...
77 99
		$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...
78 99
		$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...
79 99
		$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...
80 99
		$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...
81
82
		// architecture
83 99
		$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...
84 99
		$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...
85 99
		$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...
86 99
		$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...
87 99
		$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...
88
89
		// platform
90 99
		$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...
91 99
		$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...
92 99
		$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...
93
94
		// browser
95 99
		$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...
96 99
		$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...
97 99
		$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...
98 99
		$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...
99 99
		$this->browserstatus = $data->browserstatus ?? null;
0 ignored issues
show
Bug introduced by
The property browserstatus is declared read-only in hexydec\agentzero\agentzero.
Loading history...
100 99
		$this->browserreleased = $data->browserreleased ?? null;
0 ignored issues
show
Bug introduced by
The property browserreleased is declared read-only in hexydec\agentzero\agentzero.
Loading history...
101 99
		$this->browserlatest = $data->browserlatest ?? null;
0 ignored issues
show
Bug introduced by
The property browserlatest is declared read-only in hexydec\agentzero\agentzero.
Loading history...
102 99
		$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...
103
104
		// app
105 99
		$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...
106 99
		$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...
107 99
		$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...
108 99
		$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...
109 99
		$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...
110 99
		$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...
111
112
		// network
113 99
		$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...
114 99
		$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...
115
116
		// screen
117 99
		$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...
118 99
		$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...
119 99
		$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...
120 99
		$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...
121 99
		$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...
122
	}
123
124
	/**
125
	 * Retrieves calculated properties
126
	 * 
127
	 * @param string $key The name of the property to retrieve
128
	 * @return string|int|null The requested property or null if it doesn't exist
129
	 */
130
	public function __get(string $key) : string|int|null {
131
		switch ($key) {
132
			case 'host':
133
				if ($this->url !== null && ($host = \parse_url($this->url, PHP_URL_HOST)) !== false && $host !== null) {
134
					return \str_starts_with($host, 'www.') ? \substr($host, 4) : $host;
135
				}
136
				return null;
137
			case 'browsermajorversion':
138
			case 'enginemajorversion':
139
			case 'platformmajorversion':
140
			case 'appmajorversion':
141
				$item = \str_replace('major', '', $key);
142
				$value = $this->{$item} ?? null;
143
				return $value === null ? null : \intval(\substr($value, 0, \strspn($value, '0123456789')));
144
		}
145
		return $this->{$key} ?? null;
146
	}
147
148
	/**
149
	 * Extracts tokens from a UA string
150
	 * 
151
	 * @param string $ua The User Agent string to be tokenised
152
	 * @param array<string> $single An array of strings that can appear on their own, enables the tokens to be split correctly
153
	 * @param array<string> $ignore An array of tokens that can be ignored in the UA string
154
	 * @return false|array<int,string> An array of tokens, or false if no tokens could be extracted
155
	 */
156 99
	protected static function getTokens(string $ua, array $single, array $ignore) : array|false {
157
158
		// prepare regexp
159 99
		$single = \implode('|', \array_map('\\preg_quote', $single, \array_fill(0, \count($single), '/')));
160 99
		$pattern = '/\{[^}]++\}|[^()\[\];,\/  _-](?:(?<!'.$single.') (?!https?:\/\/)|(?<=[a-z])\([^)]+\)|[^()\[\];,\/ ]*)*[^()\[\];,\/  _-](?:\/[^;,()\[\]  ]++)?|[0-9]/i';
161
162
		// split up ua string
163 99
		if (\preg_match_all($pattern, $ua, $match)) {
164
165
			// userland token processing
166 99
			$tokens = [];
167 99
			foreach ($match[0] AS $key => $item) {
168 99
				$lower = \mb_strtolower($item);
169
170
				// special case for handling like
171 99
				if (\str_starts_with($lower, 'like ')) {
172
173
					// chop off words up to a useful token e.g. Platform/Version
174 76
					if (\str_contains($item, '/') && ($pos = \mb_strrpos($item, ' ')) !== false) {
175 76
						$tokens[$key] = \mb_substr($item, $pos + 1);
176
					}
177
178
				// check token is not ignored
179 99
				} elseif (!\in_array($lower, $ignore, true)) {
180 99
					$tokens[$key] = $item;
181
				}
182
			}
183
184
			// rekey and return
185 99
			return \array_values($tokens);
186
		}
187
		return false;
188
	}
189
190
	/**
191
	 * Parses a User Agent string
192
	 * 
193
	 * @param string $ua The User Agent string to be parsed
194
	 * @param array $config An array of configuration keys 
195
	 * @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed
196
	 */
197 99
	public static function parse(string $ua, array $config = []) : agentzero|false {
198 99
		if (($ua = \preg_replace('/\s{2,}/', ' ', $ua)) === null) {
199
200 99
		} elseif (($config = config::get($config)) === null) {
201
202 99
		} elseif (($tokens = self::getTokens(\trim($ua, ' "\''), $config['single'], $config['ignore'])) !== false) {
203
204
			// extract UA info
205 99
			$browser = new \stdClass();
206 99
			$tokenslower = \array_map('mb_strtolower', $tokens);
207 99
			foreach ($config['match'] AS $key => $item) {
208 99
				$item->match($browser, $key, $tokens, $tokenslower, $config);
209
			}
210
211
			// default information
212 99
			$arr = (array) $browser;
213 99
			if (empty($arr) && !empty($tokens)) {
214 1
				self::parseDefault($browser, $tokens);
215
			}
216
217
			// create agentzero object and return
218 99
			return new agentzero($ua, $browser);
219
		}
220
		return false;
221
	}
222
223
	/**
224
	 * Parse the UA string when no other extractions were able to be made
225
	 * 
226
	 * @param \stdClass $obj A standard class object to populate
227
	 * @param array<string> $tokens An array of tokens
228
	 * @return void
229
	 */
230 1
	protected static function parseDefault(\stdClass $obj, array $tokens) : void {
231 1
		$obj->type = 'robot';
232 1
		$obj->category = 'scraper';
233
234
		// find app names
235 1
		foreach ($tokens AS $item) {
236 1
			if (\str_contains($item, '/')) {
237 1
				$parts = \explode('/', $item);
238 1
				$obj->app = crawlers::normaliseAppname($parts[0]);
239 1
				$obj->appname = $parts[0];
240 1
				if (!empty($parts[1])) {
241 1
					$obj->appversion = \ltrim($parts[1], 'v');
242
				}
243 1
				return;
244
			}
245
		}
246
247
		// parse the string
248 1
		foreach ($tokens AS $token) {
249 1
			$name = [];
250 1
			foreach (\explode(' ', $token) AS $item) {
251 1
				$ver = \ltrim($item, 'v'); // strip 'v' off the front of version number
252 1
				if (\strpbrk($ver, '0123456789.') === $ver) {
253 1
					$app = \implode(' ', $name);
254 1
					$obj->app = crawlers::normaliseAppname($app);
255 1
					$obj->appname = $app;
256 1
					$obj->appversion = $ver;
257 1
					return;
258
				} else {
259 1
					$name[] = $item;
260
				}
261
			}
262
		}
263
264
		// just use the string
265 1
		$obj->app = crawlers::normaliseAppname($tokens[0]);
266 1
		$obj->appname = $tokens[0];
267
	}
268
}