Failed Conditions
Branch refactor/kernels (fbf61a)
by Atanas
01:47
created

src/Requests/Request.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Requests;
11
12
use WPEmerge\Helpers\Url;
13
use WPEmerge\Support\Arr;
14
15
/**
16
 * A representation of a request to the server.
17
 */
18
class Request implements RequestInterface {
19
	/**
20
	 * GET parameters.
21
	 *
22
	 * @var array
23
	 */
24
	protected $get = [];
25
26
	/**
27
	 * POST parameters.
28
	 *
29
	 * @var array
30
	 */
31
	protected $post = [];
32
33
	/**
34
	 * COOKIE parameters.
35
	 *
36
	 * @var array
37
	 */
38
	protected $cookie = [];
39
40
	/**
41
	 * FILES parameters.
42
	 *
43
	 * @var array
44
	 */
45
	protected $files = [];
46
47
	/**
48
	 * SERVER parameters.
49
	 *
50
	 * @var array
51
	 */
52
	protected $server = [];
53
54
	/**
55
	 * Headers.
56
	 *
57
	 * @var array
58
	 */
59
	protected $headers = [];
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64 1
	public static function fromGlobals() {
65 1
		return new static(
66 1
			stripslashes_deep( $_GET ),
1 ignored issue
show
It seems like stripslashes_deep($_GET) can also be of type object; however, parameter $get of WPEmerge\Requests\Request::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

66
			/** @scrutinizer ignore-type */ stripslashes_deep( $_GET ),
Loading history...
67 1
			stripslashes_deep( $_POST ),
68 1
			$_COOKIE,
69 1
			$_FILES,
70 1
			$_SERVER,
71 1
			getallheaders()
1 ignored issue
show
It seems like getallheaders() can also be of type false; however, parameter $headers of WPEmerge\Requests\Request::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

71
			/** @scrutinizer ignore-type */ getallheaders()
Loading history...
72 1
		);
73
	}
74
75
	/**
76
	 * Constructor.
77
	 *
78
	 * @param array $get
79
	 * @param array $post
80
	 * @param array $cookie
81
	 * @param array $files
82
	 * @param array $server
83
	 * @param array $headers
84
	 */
85 1
	public function __construct( $get, $post, $cookie, $files, $server, $headers ) {
86 1
		$this->get = $get;
87 1
		$this->post = $post;
88 1
		$this->cookie = $cookie;
89 1
		$this->files = $files;
90 1
		$this->server = $server;
91 1
		$this->headers = $headers;
92 1
	}
93
94
	/**
95
	 * {@inheritDoc}
96
	 */
97 1
	public function getMethod() {
98 1
		$method = (string) $this->server( 'REQUEST_METHOD', 'GET' );
99
100 1
		$header_override = (string) $this->headers( 'X-HTTP-METHOD-OVERRIDE' );
101 1
		if ( $method === 'POST' && $header_override ) {
102 1
			$method = strtoupper( $header_override );
103 1
		}
104
105 1
		$body_override = (string) $this->post( '_method' );
106 1
		if ( $method === 'POST' && $body_override ) {
107 1
			$method = strtoupper( $body_override );
108 1
		}
109
110 1
		return strtoupper( $method );
111
	}
112
113
	/**
114
	 * {@inheritDoc}
115
	 */
116 1
	public function isGet() {
117 1
		return $this->getMethod() === 'GET';
118
	}
119
120
	/**
121
	 * {@inheritDoc}
122
	 */
123 1
	public function isHead() {
124 1
		return $this->getMethod() === 'HEAD';
125
	}
126
127
	/**
128
	 * {@inheritDoc}
129
	 */
130 1
	public function isPost() {
131 1
		return $this->getMethod() === 'POST';
132
	}
133
134
	/**
135
	 * {@inheritDoc}
136
	 */
137 1
	public function isPut() {
138 1
		return $this->getMethod() === 'PUT';
139
	}
140
141
	/**
142
	 * {@inheritDoc}
143
	 */
144 1
	public function isPatch() {
145 1
		return $this->getMethod() === 'PATCH';
146
	}
147
148
	/**
149
	 * {@inheritDoc}
150
	 */
151 1
	public function isDelete() {
152 1
		return $this->getMethod() === 'DELETE';
153
	}
154
155
	/**
156
	 * {@inheritDoc}
157
	 */
158 1
	public function isOptions() {
159 1
		return $this->getMethod() === 'OPTIONS';
160
	}
161
162
	/**
163
	 * {@inheritDoc}
164
	 */
165 1
	public function isReadVerb() {
166 1
		return in_array( $this->getMethod(), ['GET', 'HEAD', 'OPTIONS'] );
167
	}
168
169
	/**
170
	 * {@inheritDoc}
171
	 */
172 1
	public function isAjax() {
173 1
		return $this->headers( 'X-Requested-With' ) === 'XMLHttpRequest';
174
	}
175
176
	/**
177
	 * {@inheritDoc}
178
	 */
179 3
	public function getUrl() {
180 3
		$https = $this->server( 'HTTPS' );
181
182 3
		$protocol = $https ? 'https' : 'http';
183 3
		$host = (string) $this->server( 'HTTP_HOST', '' );
184 3
		$uri = (string) $this->server( 'REQUEST_URI', '' );
185 3
		$uri = Url::addLeadingSlash( $uri );
186
187 3
		return $protocol . '://' . $host . $uri;
188
	}
189
190
	/**
191
	 * {@inheritDoc}
192
	 * @see \WPEmerge\Support\Arr
193
	 */
194 4
	protected function input() {
195 4
		$args = func_get_args();
196 4
		$source = $this->{$args[0]};
197
198 4
		if ( count( $args ) === 1 ) {
199 1
			return $source;
200
		}
201
202 3
		$args[0] = $source;
203 3
		return call_user_func_array( [Arr::class, 'get'], $args );
204
	}
205
206
	/**
207
	 * {@inheritDoc}
208
	 * @see ::input()
209
	 */
210 4
	public function get() {
211 4
		return call_user_func_array( [$this, 'input'], array_merge( ['get'], func_get_args() ) );
212
	}
213
214
	/**
215
	 * {@inheritDoc}
216
	 * @see ::input()
217
	 */
218 4
	public function post() {
219 4
		return call_user_func_array( [$this, 'input'], array_merge( ['post'], func_get_args() ) );
220
	}
221
222
	/**
223
	 * {@inheritDoc}
224
	 * @see ::input()
225
	 */
226 4
	public function cookie() {
227 4
		return call_user_func_array( [$this, 'input'], array_merge( ['cookie'], func_get_args() ) );
228
	}
229
230
	/**
231
	 * {@inheritDoc}
232
	 * @see ::input()
233
	 */
234 4
	public function files() {
235 4
		return call_user_func_array( [$this, 'input'], array_merge( ['files'], func_get_args() ) );
236
	}
237
238
	/**
239
	 * {@inheritDoc}
240
	 * @see ::input()
241
	 */
242 4
	public function server() {
243 4
		return call_user_func_array( [$this, 'input'], array_merge( ['server'], func_get_args() ) );
244
	}
245
246
	/**
247
	 * {@inheritDoc}
248
	 * @see ::input()
249
	 */
250 4
	public function headers() {
251 4
		return call_user_func_array( [$this, 'input'], array_merge( ['headers'], func_get_args() ) );
252
	}
253
}
254