Completed
Push — master ( ec2c45...874b07 )
by Tobias
03:11
created

src/Request.php (1 issue)

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
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7;
6
7
use InvalidArgumentException;
8
use Nyholm\Psr7\Factory\StreamFactory;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\StreamInterface;
11
use Psr\Http\Message\UriInterface;
12
13
/**
14
 * @author Michael Dowling and contributors to guzzlehttp/psr7
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
class Request implements RequestInterface
18
{
19
    use MessageTrait;
20
21
    /** @var string */
22
    private $method;
23
24
    /** @var null|string */
25
    private $requestTarget;
26
27
    /** @var null|UriInterface */
28
    private $uri;
29
30
    /**
31
     * @param string                               $method  HTTP method
32
     * @param string|UriInterface                  $uri     URI
33
     * @param array                                $headers Request headers
34
     * @param string|null|resource|StreamInterface $body    Request body
35
     * @param string                               $version Protocol version
36
     */
37 100
    public function __construct(
38
        $method,
39
        $uri,
40
        array $headers = [],
41
        $body = null,
42
        $version = '1.1'
43
    ) {
44 100
        if (!($uri instanceof UriInterface)) {
45 70
            $uri = new Uri($uri);
46
        }
47
48 99
        $this->method = $method;
49 99
        $this->uri = $uri;
50 99
        $this->setHeaders($headers);
51 99
        $this->protocol = $version;
52
53 99
        if (!$this->hasHeader('Host')) {
54 98
            $this->updateHostFromUri();
55
        }
56
57 99 View Code Duplication
        if ('' !== $body && null !== $body) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58 3
            $this->stream = (new StreamFactory())->createStream($body);
59
        }
60 99
    }
61
62 5
    public function getRequestTarget(): string
63
    {
64 5
        if (null !== $this->requestTarget) {
65 2
            return $this->requestTarget;
66
        }
67
68 5
        $target = $this->uri->getPath();
69 5
        if ('' == $target) {
70 1
            $target = '/';
71
        }
72 5
        if ('' != $this->uri->getQuery()) {
73 2
            $target .= '?'.$this->uri->getQuery();
74
        }
75
76 5
        return $target;
77
    }
78
79 3
    public function withRequestTarget($requestTarget): self
80
    {
81 3
        if (preg_match('#\s#', $requestTarget)) {
82 1
            throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
83
        }
84
85 2
        $new = clone $this;
86 2
        $new->requestTarget = $requestTarget;
87
88 2
        return $new;
89
    }
90
91 26
    public function getMethod(): string
92
    {
93 26
        return $this->method;
94
    }
95
96 4
    public function withMethod($method): self
97
    {
98 4
        if (!is_string($method)) {
99 3
            throw new \InvalidArgumentException('Method must be a string');
100
        }
101
102 1
        $new = clone $this;
103 1
        $new->method = $method;
104
105 1
        return $new;
106
    }
107
108 30
    public function getUri(): UriInterface
109
    {
110 30
        return $this->uri;
111
    }
112
113 9
    public function withUri(UriInterface $uri, $preserveHost = false): self
114
    {
115 9
        if ($uri === $this->uri) {
116 1
            return $this;
117
        }
118
119 8
        $new = clone $this;
120 8
        $new->uri = $uri;
121
122 8
        if (!$preserveHost || !$this->hasHeader('Host')) {
123 7
            $new->updateHostFromUri();
124
        }
125
126 8
        return $new;
127
    }
128
129 98
    private function updateHostFromUri(): void
130
    {
131 98
        $host = $this->uri->getHost();
132
133 98
        if ('' == $host) {
134 60
            return;
135
        }
136
137 43
        if (null !== ($port = $this->uri->getPort())) {
138 2
            $host .= ':'.$port;
139
        }
140
141 43
        if (isset($this->headerNames['host'])) {
142 2
            $header = $this->headerNames['host'];
143
        } else {
144 43
            $header = 'Host';
145 43
            $this->headerNames['host'] = 'Host';
146
        }
147
        // Ensure Host is the first header.
148
        // See: http://tools.ietf.org/html/rfc7230#section-5.4
149 43
        $this->headers = [$header => [$host]] + $this->headers;
150 43
    }
151
}
152