Passed
Push — developer ( 1b3c7a...748bda )
by Mariusz
30:45
created

RequestUtil::getBrowserInfo()   F

Complexity

Conditions 16
Paths 1081

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 1.4
c 0
b 0
f 0
cc 16
nc 1081
nop 0

How to fix   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 Sp. z o.o
8
 * @license   YetiForce Public License 3.0 (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 bool Cache https check variable. */
20
	protected static $httpsCache;
21
22
	/** @var string Cache request id variable. */
23
	protected static $requestId;
24
25
	/**
26
	 * Browser cache variable.
27
	 *
28
	 * @var stdClass
29
	 */
30
	protected static $browserCache;
31
32
	/**
33
	 * Get browser details.
34
	 *
35
	 * @return object
36
	 */
37
	public static function getBrowserInfo(): object
38
	{
39
		if (empty(self::$browserCache)) {
40
			$browserAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
41
			$browser = new \stdClass();
42
			$browser->win = false !== strpos($browserAgent, 'win');
43
			$browser->mac = false !== strpos($browserAgent, 'mac');
44
			$browser->linux = false !== strpos($browserAgent, 'linux');
45
			$browser->unix = false !== strpos($browserAgent, 'unix');
46
			$browser->webkit = false !== strpos($browserAgent, 'applewebkit');
47
			$browser->opera = false !== strpos($browserAgent, 'opera') || ($browser->webkit && false !== strpos($browserAgent, 'opr/'));
48
			$browser->ns = false !== strpos($browserAgent, 'netscape');
49
			$browser->chrome = !$browser->opera && false !== strpos($browserAgent, 'chrome');
50
			$browser->ie = !$browser->opera && (false !== strpos($browserAgent, 'compatible; msie') || false !== strpos($browserAgent, 'trident/'));
51
			$browser->safari = !$browser->opera && !$browser->chrome && ($browser->webkit || false !== strpos($browserAgent, 'safari'));
52
			$browser->mz = !$browser->ie && !$browser->safari && !$browser->chrome && !$browser->ns && !$browser->opera && false !== strpos($browserAgent, 'mozilla');
53
54
			if (preg_match('/ ([a-z]{2})-([a-z]{2})/', $browserAgent, $regs)) {
55
				$browser->lang = $regs[1];
56
			} else {
57
				$browser->lang = 'en';
58
			}
59
			$browser->https = self::isHttps();
60
			self::$browserCache = $browser;
0 ignored issues
show
Documentation Bug introduced by
It seems like $browser of type object<stdClass> is incompatible with the declared type object<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...
61
		}
62
		return self::$browserCache;
63
	}
64
65
	/**
66
	 * Check that the connection is https.
67
	 *
68
	 * @return bool
69
	 */
70
	public static function isHttps(): bool
71
	{
72
		if (!isset(self::$httpsCache)) {
73
			self::$httpsCache = (!empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']))
74
				|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']));
75
		}
76
		return self::$httpsCache;
77
	}
78
79
	/**
80
	 * Get request id.
81
	 *
82
	 * @return string
83
	 */
84
	public static function requestId(): string
85
	{
86
		if (empty(self::$requestId)) {
87
			self::$requestId = sprintf('%08x', abs(crc32($_SERVER['REMOTE_ADDR'] . $_SERVER['REQUEST_TIME'] . $_SERVER['REMOTE_PORT'])));
88
		}
89
		return self::$requestId;
90
	}
91
}
92