Completed
Push — master ( 4e4356...ccb503 )
by Dawid
02:52
created

src/Http/Request.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types=1);
2
3
namespace Igni\Network\Http;
4
5
use Igni\Network\Exception\InvalidArgumentException;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\StreamInterface;
8
use Psr\Http\Message\UriInterface;
9
use Zend\Diactoros\RequestTrait;
10
use Zend\Diactoros\Uri;
11
12
/**
13
 * PSR-7 implementation of RequestInterface.
14
 * Utilizes zend/diactoros implementation.
15
 *
16
 * @see RequestInterface
17
 * @package Igni\Http
18
 */
19
class Request implements RequestInterface
20
{
21
    use RequestTrait;
22
23
    public const METHOD_GET = 'GET';
24
    public const METHOD_HEAD = 'HEAD';
25
    public const METHOD_POST = 'POST';
26
    public const METHOD_PUT = 'PUT';
27
    public const METHOD_DELETE = 'DELETE';
28
    public const METHOD_CONNECT = 'CONNECT';
29
    public const METHOD_OPTIONS = 'OPTIONS';
30
    public const METHOD_PATCH = 'PATCH';
31
    public const METHOD_TRACE = 'TRACE';
32
33
    /** @var StreamInterface */
34
    private $stream;
35
36
    /**
37
     * @param null|string $uri URI for the request, if any.
38
     * @param string $method HTTP method for the request, if any.
39
     * @param string|resource|StreamInterface $body Output body, if any.
40
     * @param array $headers Headers for the message, if any.
41
     * @throws InvalidArgumentException for any invalid value.
42
     */
43 8
    public function __construct(string $uri = null, string $method = self::METHOD_GET, $body = 'php://temp', array $headers = [])
44
    {
45 8
        $this->validateMethod($method);
46 8
        $this->validateUri($uri);
47 8
        $this->setHeaders($headers);
48 8
        $this->method = $method;
49 8
        $this->uri = $uri ? new Uri($uri) : new Uri();
50 8
        $this->stream = Stream::create($body, 'wb+');
51
52 8
        $headers['Host'] = $headers['Host'] ?? [$this->getHostFromUri()];
53 8
    }
54
55 8
    private function validateUri($uri)
56
    {
57 8
        if (!$uri instanceof UriInterface && !is_string($uri) && null !== $uri) {
0 ignored issues
show
The class Psr\Http\Message\UriInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
            throw new InvalidArgumentException(
59
                'Invalid URI provided; must be null, a string, or a Psr\Http\Message\UriInterface instance'
60
            );
61
        }
62 8
    }
63
}
64