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.

ServerRequestTrait   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 239
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.34%

Importance

Changes 0
Metric Value
dl 18
loc 239
ccs 79
cts 82
cp 0.9634
rs 9.1199
c 0
b 0
f 0
wmc 41
lcom 1
cbo 2

21 Methods

Rating   Name   Duplication   Size   Complexity  
setAuthentication() 0 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 getDoNotTrack() 0 3 1
A isSecure() 0 3 2
A loadServerHeaders() 0 8 3
B loadAcceptHeaders() 18 23 8
A loadCachingHeaders() 0 8 3
A loadAuthenticationHeaders() 0 9 4
B 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		= '';
56
	/**
57
	 * @var string
58
	 */
59
	protected $sContentType		= '';
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 1
	public function getServerName() {
73 1
		return $this->sServerName;
74
	}
75
	/**
76
	 * @return string
77
	 */
78 1
	public function getServerProtocol() {
79 1
		return $this->sServerProtocol;
80
	}
81
	/**
82
	 * @return string
83
	 */
84 1
	public function getHttpMethod() {
85 1
		return $this->sHttpMethod;
86
	}
87
	/**
88
	 * @return array
89
	 */
90 1
	public function getHttpAccept() {
91 1
		return $this->aAccept;
92
	}
93
	/**
94
	 * @return array
95
	 */
96 1
	public function getHttpAcceptCharset() {
97 1
		return $this->aAcceptCharset;
98
	}
99
	/**
100
	 * @return bool
101
	 */
102 2
	public function hasContentType() {
103 2
		return !empty($this->sContentType);
104
	}
105
	/**
106
	 * @return string
107
	 */
108 1
	public function getContentType() {
109 1
		return $this->sContentType;
110
	}
111
	/**
112
	 * @return array
113
	 */
114 1
	public function getHttpAcceptEncoding() {
115 1
		return $this->aAcceptEncoding;
116
	}
117
	/**
118
	 * @return array
119
	 */
120 1
	public function getHttpAcceptLanguage() {
121 1
		return $this->aAcceptLanguage;
122
	}
123
	/**
124
	 * @return string
125
	 */
126 1
	public function getIfModifiedSince() {
127 1
		return $this->sIfModifiedSince;
128
	}
129
	/**
130
	 * @return string
131
	 */
132 1
	public function getIfNoneMatch() {
133 1
		return $this->sIfNoneMatch;
134
	}
135
	/**
136
	 * @return string
137
	 */
138 1
	public function getHttpReferer() {
139 1
		return $this->sReferer;
140
	}
141
	/**
142
	 * @return string
143
	 */
144 1
	public function getHttpUserAgent() {
145 1
		return $this->sUserAgent;
146
	}
147
	/**
148
	 * @return bool
149
	 */
150
	public function getDoNotTrack() {
151
		return $this->bDoNotTrack;
152
	}
153
	/**
154
	 * @return bool
155
	 */
156 1
	public static function isSecure() {
157 1
		return (array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] == 'on');
158
	}
159
160
	/**
161
	 * @param array $aServer
162
	 */
163 18
	private function loadServerHeaders($aServer) {
164 18
		if (isset ($aServer['SERVER_PROTOCOL'])) {
165 1
			$this->sServerProtocol = $aServer['SERVER_PROTOCOL'];
166
		}
167 18
		if (isset ($aServer['SERVER_NAME'])) {
168 1
			$this->sServerName = $aServer['SERVER_NAME'];
169
		}
170 18
	}
171
172
	/**
173
	 * @param array $aServer
174
	 */
175 18
	private function loadAcceptHeaders($aServer) {
176 18
		if (isset ($aServer['HTTP_ACCEPT'])) {
177 18
			$this->aAccept = explode(',', $aServer['HTTP_ACCEPT']);
178
		}
179 18 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 1
			$this->aAcceptLanguage = explode(',', $aServer['HTTP_ACCEPT_LANGUAGE']);
181 1
			foreach ($this->aAcceptLanguage as $index => $value) {
182 1
				$this->aAcceptLanguage[$index] = trim($value);
183
			}
184
		}
185 18 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 1
			$this->aAcceptEncoding = explode(',', $aServer['HTTP_ACCEPT_ENCODING']);
187 1
			foreach ($this->aAcceptEncoding as $index => $value) {
188 1
				$this->aAcceptEncoding[$index] = trim($value);
189
			}
190
		}
191 18 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 1
			$this->aAcceptCharset = explode(',', $aServer['HTTP_ACCEPT_CHARSET']);
193 1
			foreach ($this->aAcceptCharset as $index => $value) {
194 1
				$this->aAcceptCharset[$index] = trim($value);
195
			}
196
		}
197 18
	}
198
199 18
	private function loadCachingHeaders($aServer) {
200 18
		if (isset($aServer['HTTP_IF_MODIFIED_SINCE'])) {
201 1
			$this->sIfModifiedSince = $aServer['HTTP_IF_MODIFIED_SINCE'];
202
		}
203 18
		if (isset($aServer['HTTP_IF_NONE_MATCH'])) {
204 1
			$this->sIfNoneMatch = $aServer['HTTP_IF_NONE_MATCH'];
205
		}
206 18
	}
207
208 19
	private function loadAuthenticationHeaders($aServer) {
209 19
		if (isset($aServer['PHP_AUTH_DIGEST'])) {
210
			// DIGEST authorization attempt
211 1
			$this->setAuthentication(new DigestHttpAuthentication($aServer['PHP_AUTH_DIGEST'], $aServer['REQUEST_METHOD']));
212
		}
213 19
		if (isset($aServer['PHP_AUTH_USER']) && isset($aServer['PHP_AUTH_PW'])) {
214 1
			$this->setAuthentication(new BasicHttpAuthentication($aServer['PHP_AUTH_USER'], $aServer['PHP_AUTH_PW']));
215
		}
216 19
	}
217
218
	/**
219
	 * @param $aServer
220
	 */
221 19
	public function initServer($aServer) {
222 19
		if (isset ($aServer['REQUEST_METHOD'])) {
223 1
			$this->sHttpMethod = $aServer['REQUEST_METHOD'];
224
		}
225
226 19
		$this->loadServerHeaders($aServer);
227 19
		$this->loadAcceptHeaders($aServer);
228 19
		$this->loadCachingHeaders($aServer);
229 19
		$this->loadAuthenticationHeaders($aServer);
230
231 19
		if (isset ($aServer['HTTP_USER_AGENT'])) {
232 1
			$this->sUserAgent = $aServer['HTTP_USER_AGENT'];
233
		}
234 19
		if (isset ($aServer['HTTP_REFERER'])) {
235 1
			$this->sReferer = $aServer['HTTP_REFERER'];
236
		}
237 19
		if (isset($aServer['CONTENT_TYPE'])) {
238 1
			if (stripos($aServer['CONTENT_TYPE'], ';') !== false) {
239
				$this->sContentType = substr($aServer['CONTENT_TYPE'], 0, stripos($aServer['CONTENT_TYPE'], ';'));
240
			} else {
241 1
				$this->sContentType = $aServer['CONTENT_TYPE'];
242
			}
243
		}
244
245 19
		if (isset($aServer['HTTP_DNT'])) {
246 1
			$this->bDoNotTrack = (bool)$aServer['HTTP_DNT'];
247
		}
248 19
	}
249
}
250