Request   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 44
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A validateUri() 0 8 4
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 10
    public function __construct(string $uri = null, string $method = self::METHOD_GET, $body = 'php://temp', array $headers = [])
44
    {
45 10
        $this->validateUri($uri);
46 10
        $this->setHeaders($headers);
47 10
        $this->method = $method;
48 10
        $this->uri = $uri ? new Uri($uri) : new Uri();
49 10
        $this->stream = Stream::create($body, 'wb+');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Igni\Network\Http\Stream::create($body, 'wb+') of type object<self> is incompatible with the declared type object<Psr\Http\Message\StreamInterface> of property $stream.

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...
50
51 10
        $headers['Host'] = $headers['Host'] ?? [$this->getHostFromUri()];
52 10
    }
53
54 10
    private function validateUri($uri)
55
    {
56 10
        if (!$uri instanceof UriInterface && !is_string($uri) && null !== $uri) {
57
            throw new InvalidArgumentException(
58
                'Invalid URI provided; must be null, a string, or a Psr\Http\Message\UriInterface instance'
59
            );
60
        }
61 10
    }
62
}
63