RequestUtil::getBrowserInfo()   F
last analyzed

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 68
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 866.9668

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 68
ccs 4
cts 47
cp 0.0851
rs 0
c 0
b 0
f 0
cc 33
nc 1728001
nop 0
crap 866.9668

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
/**
3
 * Request Utils basic file.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App;
13
14
/**
15
 * Request Utils basic class.
16
 */
17
class RequestUtil
18
{
19
	/** @var stdClass Browser cache variable. */
0 ignored issues
show
Bug introduced by
The type App\stdClass was not found. Did you mean stdClass? If so, make sure to prefix the type with \.
Loading history...
20
	protected static $browserCache;
21
22
	/** @var bool Cache https check variable. */
23
	protected static $httpsCache;
24
25
	/** @var bool Net connection cache. */
26
	protected static $connectionCache;
27
28
	/** @var string Cache request id variable. */
29
	protected static $requestId;
30
31 2
	/**
32
	 * IP fields names variable.
33 2
	 *
34 2
	 * @var string[]
35 2
	 */
36
	protected static $ipFields = [
37
		'HTTP_CLIENT_IP',
38
		'HTTP_X_FORWARDED_FOR',
39
		'HTTP_X_FORWARDED',
40
		'HTTP_FORWARDED_FOR',
41
		'HTTP_FORWARDED',
42
		'HTTP_X_CLUSTER_CLIENT_IP',
43
		'HTTP_CF_CONNECTING_IP',
44
	];
45
46
	public static function getRemoteIP($onlyIP = false)
47
	{
48
		$address = Request::_getServer('REMOTE_ADDR');
0 ignored issues
show
Bug introduced by
The method _getServer() does not exist on App\Request. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
		/** @scrutinizer ignore-call */ 
49
  $address = Request::_getServer('REMOTE_ADDR');
Loading history...
49
		if ($onlyIP) {
50
			return empty($address) ? '' : $address;
51
		}
52 2
		// append the NGINX X-Real-IP header, if set
53
		if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
54 2
			$remoteIp[] = 'X-Real-IP: ' . Request::_getServer('HTTP_X_REAL_IP');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$remoteIp was never initialized. Although not strictly required by PHP, it is generally a good practice to add $remoteIp = array(); before regardless.
Loading history...
55
		}
56
		foreach (self::$ipFields as $key) {
57
			if (isset($_SERVER[$key])) {
58
				$remoteIp[] = "$key: " . Request::_getServer($key);
59
			}
60
		}
61
		if (!empty($remoteIp)) {
62
			$address .= '(' . implode(',', $remoteIp) . ')';
63
		}
64
		return empty($address) ? '' : $address;
65
	}
66
67
	/**
68
	 * Get browser details.
69
	 *
70
	 * @return object
71
	 */
72
	public static function getBrowserInfo(): object
73
	{
74
		if (empty(self::$browserCache)) {
75
			$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
76
77
			$browser = new \stdClass();
78
			$browser->ver = 0;
79
			$browser->win = false !== strpos($browserAgent, 'win');
80
			$browser->mac = false !== strpos($browserAgent, 'mac');
81
			$browser->linux = false !== strpos($browserAgent, 'linux');
82
			$browser->unix = false !== strpos($browserAgent, 'unix');
83
			$browser->webkit = false !== strpos($browserAgent, 'applewebkit');
84
			$browser->opera = false !== strpos($browserAgent, 'opera') || ($browser->webkit && false !== strpos($browserAgent, 'opr/'));
85
			$browser->ns = false !== strpos($browserAgent, 'netscape');
86
			$browser->chrome = !$browser->opera && false !== strpos($browserAgent, 'chrome');
87
			$browser->ie = !$browser->opera && (false !== strpos($browserAgent, 'compatible; msie') || false !== strpos($browserAgent, 'trident/'));
88
			$browser->safari = !$browser->opera && !$browser->chrome && ($browser->webkit || false !== strpos($browserAgent, 'safari'));
89
			$browser->mz = !$browser->ie && !$browser->safari && !$browser->chrome && !$browser->ns && !$browser->opera && false !== strpos($browserAgent, 'mozilla');
90
91
			if (false !== strpos($browserAgent, 'msie')) {
92
				$browser->name = 'Internet explorer';
93
			} elseif (false !== strpos($browserAgent, 'trident')) { //For Supporting IE 11
94
				$browser->name = 'Internet explorer';
95
			} elseif (false !== strpos($browserAgent, 'firefox')) {
96
				$browser->name = 'Mozilla Firefox';
97
			} elseif (false !== strpos($browserAgent, 'chrome')) {
98
				$browser->name = 'Google Chrome';
99
			} elseif (false !== strpos($browserAgent, 'opera mini')) {
100
				$browser->name = 'Opera Mini';
101
			} elseif (false !== strpos($browserAgent, 'opera')) {
102
				$browser->name = 'Opera';
103
			} elseif (false !== strpos($browserAgent, 'safari')) {
104
				$browser->name = 'Safari';
105
			} else {
106
				$browser->name = 'unknow';
107
			}
108
109
			if ($browser->opera) {
110
				if (preg_match('/(opera|opr)\/([0-9.]+)/', $browserAgent, $regs)) {
111
					$browser->ver = (float) $regs[2];
112
				}
113
			} elseif (preg_match('/(chrome|msie|version|khtml)(\s*|\/)([0-9.]+)/', $browserAgent, $regs)) {
114
				$browser->ver = (float) $regs[3];
115
			} elseif (preg_match('/rv:([0-9.]+)/', $browserAgent, $regs)) {
116
				$browser->ver = (float) $regs[1];
117
			}
118
119
			if (preg_match('/ ([a-z]{2})-([a-z]{2})/', $browserAgent, $regs)) {
120
				$browser->lang = $regs[1];
121
			} else {
122
				$browser->lang = 'en';
123
			}
124
			$browser->https = self::isHttps();
125
			$sp = strtolower(Request::_getServer('SERVER_PROTOCOL'));
126 2
			$protocol = substr($sp, 0, strpos($sp, '/')) . (($browser->https) ? 's' : '');
127
			$port = isset($_SERVER['SERVER_PORT']) ? (int) $_SERVER['SERVER_PORT'] : 0;
128
			$port = ((!$browser->https && 80 === $port) || ($browser->https && 443 === $port)) ? '' : ':' . $port;
129
			$host = Request::_getServer('HTTP_X_FORWARDED_HOST', Request::_getServer('HTTP_HOST', ''));
130
			$host = $host ?? Request::_getServer('SERVER_NAME') . $port;
131
			$dirPath = explode('/', Request::_getServer('SCRIPT_NAME'));
132
			array_pop($dirPath);
133
			$dirPath = implode('/', $dirPath);
134 7
			$browser->url = $protocol . '://' . $host . Request::_getServer('REQUEST_URI');
135
			$browser->siteUrl = $protocol . '://' . $host . $dirPath . '/';
136 7
			$browser->requestUri = ltrim(Request::_getServer('REQUEST_URI'), '/');
137
			self::$browserCache = $browser;
0 ignored issues
show
Documentation Bug introduced by
It seems like $browser of type stdClass is incompatible with the declared type App\stdClass of property $browserCache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
138
		}
139 7
		return self::$browserCache;
140 6
	}
141
142 1
	/**
143
	 * Check net connection.
144
	 *
145
	 * @return bool
146
	 */
147
	public static function isNetConnection(): bool
148
	{
149
		if (!\App\Config::performance('ACCESS_TO_INTERNET')) {
150
			return false;
151
		}
152
		if (isset(self::$connectionCache)) {
153
			return self::$connectionCache;
154
		}
155
		return self::$connectionCache = 'www.google.com' !== gethostbyname('www.google.com');
156
	}
157
158
	/**
159
	 * Check that the connection is https.
160
	 *
161
	 * @return bool
162
	 */
163
	public static function isHttps(): bool
164
	{
165
		if (!isset(self::$httpsCache)) {
166
			self::$httpsCache = (!empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']))
167
				|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']));
168
		}
169
		return self::$httpsCache;
170
	}
171
172
	/**
173
	 * Get the IP address corresponding to a given Internet host name.
174
	 *
175
	 * @param string $name
176
	 *
177
	 * @return string
178
	 */
179
	public static function getIpByName(string $name): string
180
	{
181
		if (!self::isNetConnection()) {
182
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the type-hinted return string.
Loading history...
183
		}
184
		if (\App\Cache::has(__METHOD__, $name)) {
185
			return \App\Cache::get(__METHOD__, $name);
186
		}
187
		$ip = gethostbyname($name);
188
		if ($ip === $name) {
189
			$ip = '';
190
		}
191
		return \App\Cache::save(__METHOD__, $name, $ip);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Cache::save(__METHOD__, $name, $ip) returns the type boolean which is incompatible with the type-hinted return string.
Loading history...
192
	}
193
194
	/**
195
	 * Get request id.
196
	 *
197
	 * @return string
198
	 */
199
	public static function requestId(): string
200
	{
201
		if (empty(self::$requestId)) {
202
			self::$requestId = sprintf('%08x', abs(crc32($_SERVER['REMOTE_ADDR'] . $_SERVER['REQUEST_TIME_FLOAT'] . $_SERVER['REMOTE_PORT'])));
203
		}
204
		return self::$requestId;
205
	}
206
}
207