Request::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 5
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\Psr7;
5
6
use InvalidArgumentException;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\StreamInterface;
9
use Psr\Http\Message\UriInterface;
10
use function preg_match;
11
12
class Request extends Message implements RequestInterface
13
{
14
    protected string $method;
15
16
    protected ?string $requestTarget = null;
17
    protected ?UriInterface $uri;
18
19
    /**
20
     * @param string $method HTTP method
21
     * @param UriInterface $uri URI
22
     * @param array $headers Request headers
23
     * @param string|resource|StreamInterface|null $body Request body
24
     * @param string $version Protocol version
25
     */
26 40
    public function __construct(
27
        string $method,
28
        UriInterface $uri,
29
        array $headers = [],
30
        $body = null,
31
        string $version = '1.1'
32
    ) {
33 40
        $this->method = $method;
34 40
        $this->uri = $uri;
35 40
        $this->setHeaders($headers);
36 40
        $this->protocol = $version;
37
38 40
        if (!$this->hasHeader('host')) {
39 39
            $this->updateHostFromUri();
40
        }
41
42 40
        if ('' !== $body && null !== $body) {
43 4
            $this->stream = Stream::create($body);
44
        }
45 40
    }
46
47 5
    public function getRequestTarget(): string
48
    {
49 5
        if (null !== $this->requestTarget) {
50 1
            return $this->requestTarget;
51
        }
52
53 5
        $target = $this->uri->getPath();
0 ignored issues
show
Bug introduced by
The method getPath() 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

53
        /** @scrutinizer ignore-call */ 
54
        $target = $this->uri->getPath();

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...
54 5
        $target = $target ?: '/';
55
56 5
        if ('' !== $this->uri->getQuery()) {
57 3
            $target .= '?' . $this->uri->getQuery();
58
        }
59
60 5
        $this->requestTarget = $target;
61 5
        return $target;
62
    }
63
64 3
    public function withRequestTarget($requestTarget): self
65
    {
66 3
        if (preg_match('#\s#', $requestTarget)) {
67 2
            throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
68
        }
69
70 1
        $this->requestTarget = $requestTarget;
71 1
        return $this;
72
    }
73
74 5
    public function getMethod(): string
75
    {
76 5
        return $this->method;
77
    }
78
79 1
    public function withMethod($method): self
80
    {
81 1
        $this->method = $method;
82 1
        return $this;
83
    }
84
85 5
    public function getUri(): UriInterface
86
    {
87 5
        return $this->uri;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->uri could return the type null which is incompatible with the type-hinted return Psr\Http\Message\UriInterface. Consider adding an additional type-check to rule them out.
Loading history...
88
    }
89
90 6
    public function withUri(UriInterface $uri, $preserveHost = false): self
91
    {
92 6
        if ($uri === $this->uri) {
93 1
            return $this;
94
        }
95
96 5
        $this->uri = $uri;
97
98 5
        if (!$preserveHost || !$this->hasHeader('Host')) {
99 4
            $this->updateHostFromUri();
100
        }
101
102 5
        return $this;
103
    }
104
105 39
    protected function updateHostFromUri(): void
106
    {
107 39
        if ('' === $host = $this->uri->getHost()) {
108 28
            return;
109
        }
110
111 14
        if (null !== ($port = $this->uri->getPort())) {
112 3
            $host .= ':' . $port;
113
        }
114
115 14
        $this->headers['host'] = [$host];
116 14
    }
117
}
118