GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7b42e1...c93f43 )
by Marius
10:41 queued 04:09
created

ServerRequestTrait   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 239
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 36.59%

Importance

Changes 0
Metric Value
dl 18
loc 239
ccs 30
cts 82
cp 0.3659
rs 8.2769
c 0
b 0
f 0
wmc 41
lcom 1
cbo 2

21 Methods

Rating   Name   Duplication   Size   Complexity  
setAuthentication() 0 1 ?
A getDoNotTrack() 0 3 1
A getServerName() 0 3 1
A getServerProtocol() 0 3 1
A getHttpMethod() 0 3 1
A getHttpAccept() 0 3 1
A getHttpAcceptCharset() 0 3 1
A hasContentType() 0 3 1
A getContentType() 0 3 1
A getHttpAcceptEncoding() 0 3 1
A getHttpAcceptLanguage() 0 3 1
A getIfModifiedSince() 0 3 1
A getIfNoneMatch() 0 3 1
A getHttpReferer() 0 3 1
A getHttpUserAgent() 0 3 1
A isSecure() 0 3 2
A loadServerHeaders() 0 8 3
C loadAcceptHeaders() 18 23 8
A loadCachingHeaders() 0 8 3
A loadAuthenticationHeaders() 0 9 4
C initServer() 0 28 7

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ServerRequestTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ServerRequestTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @package presentation
4
 * @subpackage requests
5
 * @author marius orcsik <[email protected]>
6
 * @date 28.07.15
7
 */
8
9
namespace vsc\presentation\requests;
10
11
trait ServerRequestTrait {
12
	/**
13
	 * @var string
14
	 */
15
	protected $sHttpMethod;
16
	/**
17
	 * @var string
18
	 */
19
	protected $sServerName;
20
	/**
21
	 * @var string
22
	 */
23
	protected $sServerProtocol;
24
	/**
25
	 * @var array
26
	 */
27
	protected $aAccept = [];
28
	/**
29
	 * @var array
30
	 */
31
	protected $aAcceptCharset = [];
32
	/**
33
	 * @var array
34
	 */
35
	protected $aAcceptEncoding	= [];
36
	/**
37
	 * @var array
38
	 */
39
	protected $aAcceptLanguage	= [];
40
	/**
41
	 * @var string
42
	 */
43
	protected $sReferer = '';
44
	/**
45
	 * @var string
46
	 */
47
	protected $sUserAgent = '';
48
	/**
49
	 * @var string
50
	 */
51
	protected $sIfModifiedSince = '';
52
	/**
53
	 * @var string
54
	 */
55
	protected $sIfNoneMatch		= '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
56
	/**
57
	 * @var string
58
	 */
59
	protected $sContentType		= '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
60
	/**
61
	 * @var bool
62
	 */
63
	protected $bDoNotTrack = false;
64
	/**
65
	 * @param HttpAuthenticationA $oHttpAuthentication
66
	 */
67
	abstract public function setAuthentication(HttpAuthenticationA $oHttpAuthentication);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
68
69
	/**
70
	 * @return string
71
	 */
72
	public function getServerName() {
73
		return $this->sServerName;
74
	}
75
	/**
76
	 * @return string
77
	 */
78
	public function getServerProtocol() {
79
		return $this->sServerProtocol;
80
	}
81
	/**
82
	 * @return string
83
	 */
84
	public function getHttpMethod() {
85
		return $this->sHttpMethod;
86
	}
87
	/**
88
	 * @return array
89
	 */
90
	public function getHttpAccept() {
91
		return $this->aAccept;
92
	}
93
	/**
94
	 * @return array
95
	 */
96
	public function getHttpAcceptCharset() {
97
		return $this->aAcceptCharset;
98
	}
99
	/**
100
	 * @return bool
101
	 */
102
	public function hasContentType() {
103
		return !empty($this->sContentType);
104
	}
105
	/**
106
	 * @return string
107
	 */
108
	public function getContentType() {
109
		return $this->sContentType;
110
	}
111
	/**
112
	 * @return array
113
	 */
114
	public function getHttpAcceptEncoding() {
115
		return $this->aAcceptEncoding;
116
	}
117
	/**
118
	 * @return array
119
	 */
120
	public function getHttpAcceptLanguage() {
121
		return $this->aAcceptLanguage;
122
	}
123
	/**
124
	 * @return string
125
	 */
126
	public function getIfModifiedSince() {
127
		return $this->sIfModifiedSince;
128
	}
129
	/**
130
	 * @return string
131
	 */
132
	public function getIfNoneMatch() {
133
		return $this->sIfNoneMatch;
134
	}
135
	/**
136
	 * @return string
137
	 */
138
	public function getHttpReferer() {
139
		return $this->sReferer;
140
	}
141
	/**
142
	 * @return string
143
	 */
144
	public function getHttpUserAgent() {
145
		return $this->sUserAgent;
146
	}
147
	/**
148
	 * @return bool
149
	 */
150
	public function getDoNotTrack() {
151
		return $this->bDoNotTrack;
152
	}
153
	/**
154
	 * @return bool
155
	 */
156
	public static function isSecure() {
0 ignored issues
show
Coding Style introduced by
isSecure uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
157
		return (array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] == 'on');
158
	}
159
160
	/**
161
	 * @param array $aServer
162
	 */
163 17
	private function loadServerHeaders($aServer) {
164 17
		if (isset ($aServer['SERVER_PROTOCOL'])) {
165
			$this->sServerProtocol = $aServer['SERVER_PROTOCOL'];
166
		}
167 17
		if (isset ($aServer['SERVER_NAME'])) {
168
			$this->sServerName = $aServer['SERVER_NAME'];
169
		}
170 17
	}
171
172
	/**
173
	 * @param array $aServer
174
	 */
175 17
	private function loadAcceptHeaders($aServer) {
176 17
		if (isset ($aServer['HTTP_ACCEPT'])) {
177 17
			$this->aAccept = explode(',', $aServer['HTTP_ACCEPT']);
178
		}
179 17 View Code Duplication
		if (isset ($aServer['HTTP_ACCEPT_LANGUAGE'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
			$this->aAcceptLanguage = explode(',', $aServer['HTTP_ACCEPT_LANGUAGE']);
181
			foreach ($this->aAcceptLanguage as $index => $value) {
182
				$this->aAcceptLanguage[$index] = trim($value);
183
			}
184
		}
185 17 View Code Duplication
		if (isset ($aServer['HTTP_ACCEPT_ENCODING'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
			$this->aAcceptEncoding = explode(',', $aServer['HTTP_ACCEPT_ENCODING']);
187
			foreach ($this->aAcceptEncoding as $index => $value) {
188
				$this->aAcceptEncoding[$index] = trim($value);
189
			}
190
		}
191 17 View Code Duplication
		if (isset ($aServer['HTTP_ACCEPT_CHARSET'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
			$this->aAcceptCharset = explode(',', $aServer['HTTP_ACCEPT_CHARSET']);
193
			foreach ($this->aAcceptCharset as $index => $value) {
194
				$this->aAcceptCharset[$index] = trim($value);
195
			}
196
		}
197 17
	}
198
199 17
	private function loadCachingHeaders($aServer) {
200 17
		if (isset($aServer['HTTP_IF_MODIFIED_SINCE'])) {
201
			$this->sIfModifiedSince = $aServer['HTTP_IF_MODIFIED_SINCE'];
202
		}
203 17
		if (isset($aServer['HTTP_IF_NONE_MATCH'])) {
204
			$this->sIfNoneMatch = $aServer['HTTP_IF_NONE_MATCH'];
205
		}
206 17
	}
207
208 17
	private function loadAuthenticationHeaders($aServer) {
209 17
		if (isset($aServer['PHP_AUTH_DIGEST'])) {
210
			// DIGEST authorization attempt
211
			$this->setAuthentication(new DigestHttpAuthentication($aServer['PHP_AUTH_DIGEST'], $aServer['REQUEST_METHOD']));
212
		}
213 17
		if (isset($aServer['PHP_AUTH_USER']) && isset($aServer['PHP_AUTH_PW'])) {
214
			$this->setAuthentication(new BasicHttpAuthentication($aServer['PHP_AUTH_USER'], $aServer['PHP_AUTH_PW']));
215
		}
216 17
	}
217
218
	/**
219
	 * @param $aServer
220
	 */
221 17
	public function initServer($aServer) {
222 17
		if (isset ($aServer['REQUEST_METHOD'])) {
223
			$this->sHttpMethod = $aServer['REQUEST_METHOD'];
224
		}
225
226 17
		$this->loadServerHeaders($aServer);
227 17
		$this->loadAcceptHeaders($aServer);
228 17
		$this->loadCachingHeaders($aServer);
229 17
		$this->loadAuthenticationHeaders($aServer);
230
231 17
		if (isset ($aServer['HTTP_USER_AGENT'])) {
232
			$this->sUserAgent = $aServer['HTTP_USER_AGENT'];
233
		}
234 17
		if (isset ($aServer['HTTP_REFERER'])) {
235
			$this->sReferer = $aServer['HTTP_REFERER'];
236
		}
237 17
		if (isset($aServer['CONTENT_TYPE'])) {
238
			if (stripos($aServer['CONTENT_TYPE'], ';') !== false) {
239
				$this->sContentType = substr($aServer['CONTENT_TYPE'], 0, stripos($aServer['CONTENT_TYPE'], ';'));
240
			} else {
241
				$this->sContentType = $aServer['CONTENT_TYPE'];
242
			}
243
		}
244
245 17
		if (isset($aServer['HTTP_DNT'])) {
246
			$this->bDoNotTrack = (bool)$aServer['HTTP_DNT'];
247
		}
248 17
	}
249
}
250