Request::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 20

Duplication

Lines 19
Ratio 95 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 19
loc 20
ccs 12
cts 12
cp 1
rs 9.2888
c 0
b 0
f 0
cc 5
nc 8
nop 5
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7;
6
7
use Psr\Http\Message\{RequestInterface, StreamInterface, UriInterface};
8
9
/**
10
 * @author Tobias Nyholm <[email protected]>
11
 * @author Martijn van der Ven <[email protected]>
12
 */
13
final class Request implements RequestInterface
14
{
15
    use MessageTrait;
16
    use RequestTrait;
17
18
    /**
19
     * @param string $method HTTP method
20
     * @param string|UriInterface $uri URI
21
     * @param array $headers Request headers
22
     * @param string|resource|StreamInterface|null $body Request body
23
     * @param string $version Protocol version
24
     */
25 73 View Code Duplication
    public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1')
26
    {
27 73
        if (!($uri instanceof UriInterface)) {
28 71
            $uri = new Uri($uri);
29
        }
30
31 72
        $this->method = $method;
32 72
        $this->uri = $uri;
33 72
        $this->setHeaders($headers);
34 72
        $this->protocol = $version;
35
36 72
        if (!$this->hasHeader('Host')) {
37 71
            $this->updateHostFromUri();
38
        }
39
40
        // If we got no body, defer initialization of the stream until Request::getBody()
41 72
        if ('' !== $body && null !== $body) {
42 4
            $this->stream = Stream::create($body);
43
        }
44 72
    }
45
}
46