Passed
Push — 0.7.0 ( 8420c9...9eca06 )
by Alexander
03:11 queued 10s
created

Request::setJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Http;
24
25
Use Locale;
26
use Exception;
27
use LogicException;
28
use Syscodes\Support\Str;
29
use Syscodes\Http\Contributors\Files;
30
use Syscodes\Http\Contributors\Inputs;
31
use Syscodes\Http\Contributors\Server;
32
use Syscodes\Http\Contributors\Headers;
33
use Syscodes\Http\Contributors\Parameters;
34
35
/**
36
 * Request represents an HTTP request.
37
 * 
38
 * @author Alexander Campo <[email protected]>
39
 */
40
class Request
41
{
42
	/**
43
	 * Holds the global active request instance.
44
	 *
45
	 * @var bool $requestURI
46
	 */
47
	protected static $requestURI;
48
49
	/**
50
	 * The base URL.
51
	 * 
52
	 * @var string $baseUrl
53
	 */
54
	protected $baseUrl;
55
56
	/**
57
	 * Gets cookies ($_COOKIE).
58
	 * 
59
	 * @var string $cookies
60
	 */
61
	public $cookies;
62
63
	/**
64
	 * Gets the string with format JSON.
65
	 * 
66
	 * @var string|resource|null $content
67
	 */
68
	protected $content;
69
70
	/**
71
	 * The default Locale this request.
72
	 * 
73
	 * @var string $defaultLocale
74
	 */
75
	protected $defaultLocale = 'en';
76
	
77
	/**
78
	 * Gets files request ($_FILES).
79
	 * 
80
	 * @var string $files
81
	 */
82
	public $files;
83
84
	/**
85
	 * The detected uri and server variables.
86
	 * 
87
	 * @var string $http
88
	 */
89
	protected $http;
90
91
	/**
92
	 * The decoded JSON content for the request.
93
	 * 
94
	 * @var \Syscodes\Http\Contributors\Parameters|null $json
95
	 */
96
	protected $json;
97
98
	/**
99
	 * The current language of the application.
100
	 * 
101
	 * @var string $languages
102
	 */
103
	protected $languages;
104
	
105
	/** 
106
	 * The method name.
107
	 * 
108
	 * @var string $method
109
	 */
110
	protected $method;
111
112
	/**
113
	 * The path info of URL.
114
	 * 
115
	 * @var string $pathInfo
116
	 */
117
	protected $pathInfo;
118
119
	/**
120
	 * Request body parameters ($_POST).
121
	 * 
122
	 * @var \Syscodes\Http\Contributors\Parameters $request
123
	 */
124
	public $request;
125
126
	/**
127
	 * Get request URI.
128
	 * 
129
	 * @var string $requestToURI
130
	 */
131
	protected $requestToURI;
132
133
	/**
134
	 * The detected uri and server variables ($_FILES).
135
	 * 
136
	 * @var array $server
137
	 */
138
	public $server = [];
139
140
	/** 
141
	 * List of routes uri.
142
	 *
143
	 * @var string|array $uri 
144
	 */
145
	public $uri;
146
147
	/**
148
	 * Stores the valid locale codes.
149
	 * 
150
	 * @var array $validLocales
151
	 */
152
	protected $validLocales = [];
153
154
	/**
155
	 * Constructor: Create new the Request class.
156
	 * 
157
	 * @param  array  $request
158
	 * @param  array  $cookies
159
	 * @param  array  $files
160
	 * @param  array  $server
161
	 * @param  string|resource|null $content  (null by default)
162
	 * 
163
	 * @return void
164
	 */
165
	public function __construct(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null)
166
	{
167
		static::$requestURI = $this;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this of type Syscodes\Http\Request is incompatible with the declared type boolean of property $requestURI.

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...
168
		
169
		$this->initialize($request, $cookies, $files, $server, $content);
170
171
		$this->detectURI(config('app.uriProtocol'), config('app.baseUrl'));
0 ignored issues
show
Bug introduced by
It seems like config('app.baseUrl') can also be of type Syscodes\Config\Configure; however, parameter $baseUrl of Syscodes\Http\Request::detectURI() does only seem to accept string, 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

171
		$this->detectURI(config('app.uriProtocol'), /** @scrutinizer ignore-type */ config('app.baseUrl'));
Loading history...
Bug introduced by
It seems like config('app.uriProtocol') can also be of type Syscodes\Config\Configure; however, parameter $protocol of Syscodes\Http\Request::detectURI() does only seem to accept string, 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

171
		$this->detectURI(/** @scrutinizer ignore-type */ config('app.uriProtocol'), config('app.baseUrl'));
Loading history...
172
173
		$this->detectLocale();
174
	}
175
176
	/**
177
	 * Sets the parameters for this request.
178
	 * 
179
	 * @param  array  $request
180
	 * @param  array  $cookies
181
	 * @param  array  $files
182
	 * @param  array  $server
183
	 * 
184
	 * @return void
185
	 */
186
	public function initialize(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null)
187
	{
188
		$this->request      = new Parameters($request);
189
		$this->cookies      = new Inputs($cookies);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Syscodes\Http\Contributors\Inputs($cookies) of type Syscodes\Http\Contributors\Inputs is incompatible with the declared type string of property $cookies.

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...
190
		$this->files        = new Files($files);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Syscodes\Http\Contributors\Files($files) of type Syscodes\Http\Contributors\Files is incompatible with the declared type string of property $files.

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...
191
		$this->server       = new Server($server);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Syscodes\Http\Contributors\Server($server) of type Syscodes\Http\Contributors\Server is incompatible with the declared type array of property $server.

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...
192
		$this->headers      = new Headers($this->server->all());
0 ignored issues
show
Bug Best Practice introduced by
The property headers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
193
194
		$this->uri          = new URI;
195
		$this->http         = new Http;
0 ignored issues
show
Documentation Bug introduced by
It seems like new Syscodes\Http\Http() of type Syscodes\Http\Http is incompatible with the declared type string of property $http.

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...
196
		$this->method       = null;
197
		$this->baseUrl      = null;
198
		$this->content      = $content;
199
		$this->pathInfo     = null;
200
		$this->languages    = null;
201
		$this->validLocales = config('app.supportedLocales');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('app.supportedLocales') can also be of type Syscodes\Config\Configure. However, the property $validLocales is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
202
	}
203
204
	/**
205
	 * Create a new Syscodes HTTP request from server variables.
206
	 * 
207
	 * @return static
208
	 */
209
	public static function capture()
210
	{
211
		return static::createFromRequest(static::createFromRequestGlobals());
212
	}
213
214
	/**
215
	 * Creates an Syscodes request from of the Request class instance.
216
	 * 
217
	 * @param  \Syscodes\Http\Request  $request
218
	 * 
219
	 * @return static
220
	 */
221
	public static function createFromRequest($request)
222
	{
223
		$newRequest = (new static)->duplicate(
224
			$request->request->all(), $request->cookies->all(), 
225
			$request->files->all(), $request->server->all()
226
		);
227
228
		$newRequest->content = $request->content;
229
230
		return $newRequest;
231
	}
232
233
	/**
234
	 * Creates a new request with value from PHP's super global.
235
	 * 
236
	 * @return static
237
	 */
238
	public static function createFromRequestGlobals()
239
	{
240
		$request = static::createFromRequestFactory($_POST, $_COOKIE, $_FILES, $_SERVER);
241
242
		return $request;
243
	}
244
245
	/**
246
	 * Creates a new request from a factory.
247
	 * 
248
	 * @param  array  $request
249
	 * @param  array  $cookies
250
	 * @param  array  $files
251
	 * @param  array  $server
252
	 * 
253
	 * @return static
254
	 */
255
	protected static function createFromRequestFactory(array $request = [], array $cookies = [], array $files = [], array $server = [])
256
	{
257
		if (self::$requestURI)
258
		{
259
			$request = (self::$requestURI)($request, $cookies, $files, $server);
260
261
			if ( ! $request instanceof self)
262
			{
263
				throw new LogicException('The Request active must return an instance of Syscodes\Http\Request');
264
			}
265
266
			return $request;
267
		}
268
269
		return new static($request, $cookies, $files, $server);
270
	}
271
272
	/**
273
	 * Clones a request and overrides some of its parameters.
274
	 * 
275
	 * @param  array  $request
276
	 * @param  array  $cookies
277
	 * @param  array  $files
278
	 * @param  array  $server
279
	 * 
280
	 * @return static
281
	 */
282
	public function duplicate(array $request = [], array $cookies = [], array $files = [], array $server = [])
283
	{
284
		$duplicate = clone $this;
285
286
		if (null !== $request)
0 ignored issues
show
introduced by
The condition null !== $request is always true.
Loading history...
287
		{
288
			$duplicate->request = new Parameters($request);
289
		}
290
291
		if (null !== $cookies)
0 ignored issues
show
introduced by
The condition null !== $cookies is always true.
Loading history...
292
		{
293
			$duplicate->cookies = new Inputs($cookies);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Syscodes\Http\Contributors\Inputs($cookies) of type Syscodes\Http\Contributors\Inputs is incompatible with the declared type string of property $cookies.

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...
294
		}
295
296
		if (null !== $files)
0 ignored issues
show
introduced by
The condition null !== $files is always true.
Loading history...
297
		{
298
			$duplicate->files = new Files($files);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Syscodes\Http\Contributors\Files($files) of type Syscodes\Http\Contributors\Files is incompatible with the declared type string of property $files.

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...
299
		}
300
301
		if (null !== $server)
0 ignored issues
show
introduced by
The condition null !== $server is always true.
Loading history...
302
		{
303
			$duplicate->server  = new Server($server);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Syscodes\Http\Contributors\Server($server) of type Syscodes\Http\Contributors\Server is incompatible with the declared type array of property $server.

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...
304
			$duplicate->headers = new Headers($duplicate->server->all());
0 ignored issues
show
Bug Best Practice introduced by
The property headers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
305
		}
306
307
		$duplicate->uri          = new URI;
308
		$duplicate->http         = new Http;
0 ignored issues
show
Documentation Bug introduced by
It seems like new Syscodes\Http\Http() of type Syscodes\Http\Http is incompatible with the declared type string of property $http.

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...
309
		$duplicate->locale       = null;
0 ignored issues
show
Bug Best Practice introduced by
The property locale does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
310
		$duplicate->method       = null;
311
		$duplicate->baseUrl      = null;
312
		$duplicate->pathInfo     = null;
313
		$duplicate->validLocales = config('app.supportedLocales');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('app.supportedLocales') can also be of type Syscodes\Config\Configure. However, the property $validLocales is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
314
315
		return $duplicate;		
316
	}
317
318
	/**
319
	 * Returns the active request currently being used.
320
	 *
321
	 * @param  \Syscodes\Http\Request|bool|null  $request  Overwrite current request 
322
	 *                                                     before returning, false prevents 
323
	 *                                                     overwrite
324
	 *
325
	 * @return \Syscodes\Http\Request
326
	 */
327
	public static function active($request = false)
328
	{
329
		if ($request !== false)
330
		{
331
			static::$requestURI = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request can also be of type Syscodes\Http\Request. However, the property $requestURI is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
332
		}
333
334
		return static::$requestURI;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::requestURI returns the type boolean which is incompatible with the documented return type Syscodes\Http\Request.
Loading history...
335
	}
336
337
	/**
338
	 * Returns the desired segment, or $default if it does not exist.
339
	 *
340
	 * @param  int  $index  The segment number (1-based index)
341
	 * @param  mixed  $default  Default value to return
342
	 *
343
	 * @return  string
344
	 */
345
	public function segment($index, $default = null)
346
	{
347
		if ($request = static::active())
348
		{
349
			return $request->uri->getSegment($index, $default);
350
		}
351
352
		return null;
353
	}
354
355
	/**
356
	 * Returns all segments in an array. For total of segments
357
	 * used the function PHP count().
358
	 *
359
	 * @return array
360
	 */
361
	public function segments()
362
	{
363
		if ($request = static::active())
364
		{
365
			return $request->uri->getSegments();
366
		}
367
368
		return null;
369
	}
370
371
	/**
372
	 * Returns the total number of segment.
373
	 *
374
	 * @return int|null  
375
	 */
376
	public function totalSegments()
377
	{
378
		if ($request = static::active())
379
		{
380
			return $request->uri->getTotalSegments();
381
		}
382
383
		return null;
384
	}
385
386
	/**
387
	 * Detects and returns the current URI based on a number of different server variables.
388
	 * 
389
	 * @param  string  $protocol
390
	 * @param  string  $baseUrl
391
	 * 
392
	 * @return string
393
	 */
394
	protected function detectURI(string $protocol, string $baseUrl)
395
	{
396
		$this->uri->setPath($this->http->detectPath($protocol));
397
398
		$baseUrl = ! empty($baseUrl) ? rtrim($baseUrl, '/ ').'/' : $baseUrl;
399
400
		if ( ! empty($baseUrl))
401
		{
402
			$this->uri->setScheme(parse_url($baseUrl, PHP_URL_SCHEME));
403
			$this->uri->setHost(parse_url($baseUrl, PHP_URL_HOST));
404
			$this->uri->setPort(parse_url($baseUrl, PHP_URL_PORT));
405
		}
406
		else 
407
		{
408
			if ( ! $this->http->isCli())
409
			{
410
				exit('You have an empty or invalid base URL. The baseURL value must be set in config/app.php, or through the .env file.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
411
			}
412
		}
413
	}
414
415
	/**
416
	 * Handles setting up the locale, auto-detecting of language.
417
	 * 
418
	 * @return void
419
	 */
420
	public function detectLocale()
421
	{
422
		$this->languages = $this->defaultLocale = config('app.locale');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->defaultLocale = config('app.locale') can also be of type Syscodes\Config\Configure. However, the property $languages is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Documentation Bug introduced by
It seems like config('app.locale') can also be of type Syscodes\Config\Configure. However, the property $defaultLocale is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
423
424
		$this->setLocale($this->validLocales);
0 ignored issues
show
Bug introduced by
$this->validLocales of type array is incompatible with the type string expected by parameter $locale of Syscodes\Http\Request::setLocale(). ( Ignorable by Annotation )

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

424
		$this->setLocale(/** @scrutinizer ignore-type */ $this->validLocales);
Loading history...
425
	}
426
427
	/**
428
	 * Returns the default locale as set.
429
	 * 
430
	 * @return string
431
	 */
432
	public function getDefaultLocale()
433
	{
434
		return $this->defaultLocale;
435
	}
436
437
	/**
438
	 * Gets the current locale, with a fallback to the default.
439
	 * 
440
	 * @return string 
441
	 */
442
	public function getLocale()
443
	{
444
		return $this->languages ?: $this->defaultLocale;
445
	}
446
447
	/**
448
	 * Sets the locale string for this request.
449
	 * 
450
	 * @param  string  $locale
451
	 * 
452
	 * @return \Syscodes\Http\Request
453
	 */
454
	public function setLocale($locale)
455
	{
456
		if ( ! in_array($locale, $this->validLocales))
457
		{
458
			$locale = $this->defaultLocale;
459
		}
460
		
461
		$this->languages = $locale;
462
463
		try
464
		{
465
		    if (class_exists('Locale', false))
466
			{
467
				Locale::setDefault($locale);
468
			}
469
		}
470
		catch (Exception $exception) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
471
472
		return $this;
473
	}
474
475
	/**
476
	 * Returns the full request string.
477
	 *
478
	 * @return string|null  The Request string
479
	 */
480
	public function get() 
481
	{
482
		if ($request = static::active())
483
		{
484
			return $request->uri->get();
485
		}
486
487
		return null;
488
	}
489
490
	/**
491
	 * Get the JSON payload for the request.
492
	 * 
493
	 * @param  string|null  $key  (null by default)
494
	 * @param  mixed  $default  (null by default)
495
	 * 
496
	 * @return \Syscodes\Http\Contributors\Parameters|mixed
497
	 */
498
	public function json($key = null, $default = null)
499
	{
500
		if ( ! isset($this->json)) {
501
			$this->json = new Parameters((array) json_decode($this->getContent(), true));
502
		}
503
504
		if (is_null($key)) {
505
			return $this->json;
506
		}
507
508
		return Arr::get($this->json->all(), $key, $default);
0 ignored issues
show
Bug introduced by
The method all() does not exist on null. ( Ignorable by Annotation )

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

508
		return Arr::get($this->json->/** @scrutinizer ignore-call */ all(), $key, $default);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The type Syscodes\Http\Arr was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
509
	}
510
511
	/**
512
	 * Set the JSON payload for the request
513
	 * 
514
	 * @param  \Syscodes\Http\Contributors\Parameters  $json
515
	 * 
516
	 * @return $this
517
	 */
518
	public function setJson($json)
519
	{
520
		$this->json = $json;
521
522
		return $this;
523
	}
524
525
	/**
526
	 * Returns whether this is an AJAX request or not.
527
	 *
528
	 * @return bool
529
	 */
530
	public function isXmlHttpRequest()
531
	{
532
		return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && 
533
				strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest';
534
	}
535
536
	/**
537
	 * Returns the input method used (GET, POST, DELETE, etc.).
538
	 *
539
	 * @return string
540
	 * 
541
	 * @throws \LogicException  
542
	 */
543
	public function getmethod()
544
	{
545
		if (null !== $this->method)
546
		{
547
			return $this->method;
548
		}
549
		
550
		$method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
551
		
552
		if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true))
553
		{
554
			return $this->method = $method;
555
		}
556
		
557
		if ( ! preg_match('~^[A-Z]++$#~D', $method))
558
		{
559
			throw new logicException(sprintf('Invalid method override "%s"', $method));
560
		}
561
562
		return $this->method = $method;
563
	}
564
565
	/**
566
	 * Sets the request method.
567
	 *
568
	 * @param  string  $method  
569
	 *
570
	 * @return string
571
	 */
572
	public function setMethod(string $method) 
573
	{
574
		$this->method = null;
575
576
		$this->server->set('REQUEST_METHOD', $method);
577
	}
578
	
579
	/**
580
	 * Determine if the current request URI matches a pattern.
581
	 * 
582
	 * @param  mixed  ...$patterns
583
	 * 
584
	 * @return bool
585
	 */
586
	public function is(...$patterns)
587
	{
588
		$path = $this->decodedPath();
589
		
590
		foreach ($patterns as $pattern)
591
		{
592
			if (Str::is($pattern, $path))
593
			{
594
				return true;
595
			}
596
		}
597
598
		return false;
599
	}
600
601
	/**
602
	 * Determine if the route name matches a given pattern.
603
	 * 
604
	 * @param  mixed  ...$patterns
605
	 * 
606
	 * @return bool
607
	 */
608
	public function routeIs(...$patterns)
609
	{
610
		return $this->route() && $this->route()->is(...$patterns);
0 ignored issues
show
Bug introduced by
The method is() does not exist on Syscodes\Routing\Route. ( Ignorable by Annotation )

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

610
		return $this->route() && $this->route()->/** @scrutinizer ignore-call */ is(...$patterns);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
611
	}
612
613
	/**
614
	 * Get the route handling the request.
615
	 * 
616
	 * @param  string|null  $param  (null by default)
617
	 * @param  mixed  $default  (null by default)
618
	 * 
619
	 * @return \Syscodes\Routing\Route|object|string|null
620
	 */
621
	public function route($param = null, $default = null)
622
	{
623
		$route = $this->getRoute();
624
625
		if (is_null($route) || is_null($param))
626
		{
627
			return $route;
628
		}
629
630
		return $route->parameter($param, $default);
0 ignored issues
show
Bug introduced by
The method parameter() does not exist on Syscodes\Routing\Router. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

630
		return $route->/** @scrutinizer ignore-call */ parameter($param, $default);
Loading history...
631
	}
632
633
	/**
634
	 * Get the current decoded path info for the request.
635
	 * 
636
	 * @return string
637
	 */
638
	public function decodedPath()
639
	{
640
		return rawurldecode($this->path());
641
	}
642
643
	/**
644
	 * Get the current path info for the request.
645
	 * 
646
	 * @return string
647
	 */
648
	public function path()
649
	{
650
		$path = trim($this->getPathInfo(), '/');
651
652
		return $path == '' ? '/' : $path;
653
	}
654
655
	/**
656
	 * Retunrs the request body content.
657
	 * 
658
	 * @return string
659
	 */
660
	public function getContent()
661
	{
662
		if (null === $this->content || false === $this->content)
663
		{
664
			$this->content = file_get_contents('php://input');
665
		}
666
667
		return $this->content;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->content also could return the type resource which is incompatible with the documented return type string.
Loading history...
668
	}
669
670
	/**
671
	 * Returns the path being requested relative to the executed script. 
672
	 * 
673
	 * @return string
674
	 */
675
	public function getPathInfo()
676
	{
677
		if (null === $this->pathInfo)
678
		{
679
			$this->pathInfo = $this->http->parsePathInfo();
680
		}
681
682
		return $this->pathInfo;
683
	}
684
685
	/**
686
	 * Returns the root URL from which this request is executed.
687
	 * 
688
	 * @return string
689
	 */
690
	public function getBaseUrl()
691
	{
692
		if (null === $this->baseUrl)
693
		{
694
			$this->baseUrl = $this->http->parseBaseUrl();
695
		}
696
697
		return $this->baseUrl;
698
	}
699
700
	/**
701
	 * Returns the requested URI.
702
	 * 
703
	 * @return string
704
	 */
705
	public function getRequestUri()
706
	{
707
		if (null === $this->requestToUri)
708
		{
709
			$this->requestToUri = $this->http->parseRequestUri();
0 ignored issues
show
Bug Best Practice introduced by
The property requestToUri does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
710
		}
711
712
		return $this->requestToUri;
713
	}
714
	
715
	/**
716
	 * Gets the request's scheme.
717
	 * 
718
	 * @return string
719
	 */
720
	public function getScheme()
721
	{
722
		return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http');
723
	}
724
725
	/**
726
	 * Returns the host name.
727
	 * 
728
	 * @return void
729
	 */
730
	public function getHost()
731
	{
732
		if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $forwardedHost is dead and can be removed.
Loading history...
733
			$host = $forawardedHost[0];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $forawardedHost does not exist. Did you maybe mean $forwardedHost?
Loading history...
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
734
		}
735
		elseif ( ! $host = $this->headers->get('HOST')) {
736
			if ( ! $host = $this->server->get('SERVER_NAME')) {
737
				$host = $this->server->get('REMOTE_ADDR', '');
738
			}
739
		}
740
741
		$host = $_SERVER['SERVER_NAME'];
742
743
		$host = strtolower(preg_replace('/:\d+$/', '', trim(($host))));
744
		
745
		return $host;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $host returns the type string which is incompatible with the documented return type void.
Loading history...
746
	}
747
748
	/**
749
	 * Returns the port on which the request is made.
750
	 * 
751
	 * @return int
752
	 */
753
	public function getPort()
754
	{
755
		if ( ! $this->server->get('HTTP_HOST')) 
756
		{
757
			return $this->server->get('SERVER_PORT');
758
		}
759
		
760
		return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80);
761
	}
762
763
	/**
764
	 * Returns the HTTP host being requested.
765
	 * 
766
	 * @return string
767
	 */
768
	public function getHttpHost()
769
	{
770
		$scheme = $this->getScheme();
771
		$port   = $this->getPort();
772
773
		if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port))		
774
		{
775
			return $this->getHost();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getHost() returns the type void which is incompatible with the documented return type string.
Loading history...
Bug introduced by
Are you sure the usage of $this->getHost() targeting Syscodes\Http\Request::getHost() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
776
		}
777
778
		return $this->getHost().':'.$port;
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getHost() targeting Syscodes\Http\Request::getHost() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure $this->getHost() of type void can be used in concatenation? ( Ignorable by Annotation )

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

778
		return /** @scrutinizer ignore-type */ $this->getHost().':'.$port;
Loading history...
779
	}
780
781
	/**
782
	 * Gets the scheme and HTTP host.
783
	 * 
784
	 * @return string
785
	 */
786
	public function getSchemeWithHttpHost()
787
	{
788
		return $this->getScheme().'://'.$this->getHttpHost();
789
	}
790
791
	/**
792
	 * Get the root URL for the application.
793
	 * 
794
	 * @return string
795
	 */
796
	public function root()
797
	{
798
		return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/');
799
	}
800
801
	/**
802
	 * Get the URL for the request.
803
	 * 
804
	 * @return string
805
	 */
806
	public function url()
807
	{
808
		return trim(preg_replace('/\?.*/', '', $this->get()), '/');
809
	}
810
811
	/**
812
	 * Returns the referer.
813
	 * 
814
	 * @param  string  $default
815
	 * 
816
	 * @return string
817
	 */
818
	public function referer(string $default = '')
819
	{
820
		return $this->server->get('HTTP_REFERER', $default);
821
	}
822
	
823
	/**
824
	 * Attempts to detect if the current connection is secure through 
825
	 * over HTTPS protocol.
826
	 * 
827
	 * @return bool
828
	 */
829
	public function secure()
830
	{
831
		if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off')
832
		{
833
			return true;
834
		}
835
		elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https')
836
		{
837
			return true;
838
		}
839
		elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off')
840
		{
841
			return true;
842
		}
843
844
		return false;
845
	}
846
847
	/**
848
	 * Returns the user agent.
849
	 *
850
	 * @param  string|null  $default
851
	 *
852
	 * @return string
853
	 */
854
	public function userAgent(string $default = null)
855
	{
856
		return $this->server->get('HTTP_USER_AGENT', $default);
857
	}
858
859
	/**
860
	 * Get the route resolver.
861
	 * 
862
	 * @return \Syscodes\Routing\Router
863
	 */
864
	public function getRoute()
865
	{
866
		return app('router');
867
	}
868
869
	/**
870
	 * Get an element from the request.
871
	 * 
872
	 * @return string[]
873
	 */
874
	public function __get($key)
875
	{
876
		$all = $this->server->all();
877
878
		if (array_key_exists($key, $all))
879
		{
880
			return $all[$key];
881
		}
882
		else
883
		{
884
			return $key;
885
		}
886
	}
887
888
	/**
889
	 * Returns the Request as an HTTP string.
890
	 * 
891
	 * @return string
892
	 */
893
	public function __toString()
894
	{
895
		try
896
		{
897
			$content = $this->getContent();
898
		}
899
		catch (LogicException $e)
900
		{
901
			if (PHP_VERSION_ID > 70400)
902
			{
903
				throw $e;
904
			}
905
906
			return trigger_error($e, E_USER_ERROR);
0 ignored issues
show
Bug Best Practice introduced by
The expression return trigger_error($e,...odes\Http\E_USER_ERROR) returns the type boolean which is incompatible with the documented return type string.
Loading history...
907
		}
908
909
		$cookieHeader = '';
910
		$cookies      = [];
911
912
		foreach ($this->cookies as $key => $value)
0 ignored issues
show
Bug introduced by
The expression $this->cookies of type string is not traversable.
Loading history...
913
		{
914
			$cookies[]= "{$key} = {$value}";
915
		}
916
917
		if ( ! empty($cookies))
918
		{
919
			$cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n";
920
		}
921
		
922
		return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
923
			$this->headers.
924
			$cookieHeader."\r\n".
925
			$content;
926
	}
927
928
	/**
929
	 * Clones the current request.
930
	 * 
931
	 * @return void
932
	 */
933
	public function __clone()
934
	{
935
		$this->request = clone $this->request;
936
		$this->cookies = clone $this->cookies;
0 ignored issues
show
Documentation Bug introduced by
It seems like clone $this->cookies of type object is incompatible with the declared type string of property $cookies.

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...
937
		$this->files   = clone $this->files;
0 ignored issues
show
Documentation Bug introduced by
It seems like clone $this->files of type object is incompatible with the declared type string of property $files.

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...
938
		$this->server  = clone $this->server;
0 ignored issues
show
Documentation Bug introduced by
It seems like clone $this->server of type object is incompatible with the declared type array of property $server.

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...
939
		$this->headers = clone $this->headers;
0 ignored issues
show
Bug Best Practice introduced by
The property headers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
940
	}
941
}