Completed
Push — master ( 48fb97...1f3568 )
by Auke
03:18
created

ServerRequest::getProtocol()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of the Ariadne Component Library.
4
 *
5
 * (c) Muze <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace ac\http;
11
12
/**
13
 * Class ServerRequest
14
 * Implements a simple container to retrieve information from the http server request
15
 *
16
 * Usage:
17
 *   $request = \arc\http::serverRequest();
18
 *   echo $request->url;
19
 *
20
 * @package arc
21
 * @property string $protocol The HTTP request protocol
22
 * @property string $method The HTTP request method
23
 * @property \arc\url\Url $url The requested URL 
24
 * @property array $headers The Headers sent in the request. Note that Apache's REDIRECT_ prefixes aren't parsed, you have to do that yourself.
25
 * @property string $body The HTTP request body
26
 * @property string $user The user name from HTTP Basic authentication, if specified
27
 * @property string $password The user password from HTTP Basic authentication, if specified
28
 */
29
class ServerRequest
30
{
31
	
32
	/**
33
	 * Lazy load one of the Request properties
34
	 */
35
	public function __get($name)
36
	{
37
		switch($name) {
38
			case 'protocol':
39
				$this->protocol = $this->getProtocol();
40
				return $this->protocol;
41
			break;
42
			case 'method':
43
				$this->method = $this->getMethod();
44
				return $this->method;
45
			break;
46
			case 'url':
47
				$this->url = $this->getURL();
48
				return $this->url;
49
			break;
50
			case 'headers':
51
				$this->headers = $this->getHeaders();
52
				return $this->headers;
53
			break;
54
			case 'body':
55
				$this->body = $this->getBody();
56
				return $this->body;
57
			break;
58
			case 'user':
59
				$this->user = $this->getUser();
60
				return $this->user;
61
			break;
62
			case 'password':
63
				$this->password = $this->getPassword();
64
				return $this->password;
65
			break;
66
			default:
67
				throw new \arc\IllegalRequest('Unknown property '.$name, \arc\exceptions::OBJECT_NOT_FOUND);
68
			break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
69
		}
70
	}
71
72
	/**
73
	 * Returns the first header found from a list of headers.
74
	 * Will try REDIRECT_* headers first, if $followRedirects>0. 
75
	 * ( Apache adds 'REDIRECT_' to some headers when you use mod_rewrite. )
76
	 * @param array $list An array of headers to try, in order
77
	 * @param int $followRedirects The maximum number of REDIRECT_ prefixes to try
78
	 */
79
	public function getHeader($list, $followRedirects=0) {
80
		$redirect = 'REDIRECT_';
81
		if (!is_array($list)) {
82
			$list = [ $list => false ];
83
		}
84
		foreach ( $list as $header => $extraInfo ) {
85
			for ($i=$followRedirects; $i>=0; $i--) {
86
				$check = str_repeat($redirect, $i).$header;
87
				if ( isset($_SERVER[$check]) ) {
88
					return [$header, $_SERVER[$check]];
89
				}
90
			}
91
		}
92
		return [false, ''];
93
	}
94
95 View Code Duplication
	private static function getUser()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
96
	{
97
		$checks = [ 
98
			'PHP_AUTH_USER'               => false, 
99
			'REMOTE_USER'                 => false, 
100
			'HTTP_AUTHORIZATION'          => function($v) { list($user,$password)=http::parseAuthUser($v); return $user; },
0 ignored issues
show
Unused Code introduced by
The assignment to $password is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
101
		];
102
		list($header, $headerValue) = $this->getHeader($checks, 3);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
103
		if (isset($checks[$header]) && is_callable($checks[$header])) {
104
			$headerValue = ($checks[$header])($headerValue);
105
		}
106
		return $headerValue;
107
	}
108
109 View Code Duplication
	private static function getPassword()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
110
	{
111
		$checks = [ 
112
			'PHP_AUTH_PW'                 => false, 
113
			'HTTP_AUTHORIZATION'          => function($v) { list($user,$password)=http::parseAuthUser($v); return $password; },
0 ignored issues
show
Unused Code introduced by
The assignment to $user is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
114
		];
115
		list($header, $headerValue) = $this->getHeader($checks, 3);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
116
		if (isset($checks[$header]) && is_callable($checks[$header])) {
117
			$headerValue = ($checks[$header])($headerValue);
118
		}
119
		return $headerValue;
120
	}
121
122
	private static function parseAuthUser($auth) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
123
		return explode(':',base64_decode(substr($auth, 6)));
124
	}
125
126
	private function getProtocol()
127
	{
128
		list($header, $headerValue) = $this->getHeader('SERVER_PROTOCOL',3);
0 ignored issues
show
Unused Code introduced by
The assignment to $header is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Documentation introduced by
'SERVER_PROTOCOL' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
		return $headerValue ?: 'HTTP/1.1';
130
	}
131
132
	private function getMethod()
133
	{
134
		list($header, $headerValue) = $this->getHeader('REQUEST_METHOD',3);
0 ignored issues
show
Unused Code introduced by
The assignment to $header is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Documentation introduced by
'REQUEST_METHOD' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
		return $headerValue;
136
	}
137
138
	private function getURL()
139
	{
140
		return \arc\url($_SERVER['REQUEST_URI']);
141
	}
142
143
	private function getHeaders()
144
	{
145
		return getallheaders(); //polyfill via composer require ralouphie/getallheaders
146
	}
147
148
	private function getBody()
149
	{
150
		return stream_get_contents(STDIN);	
151
	}
152
}