Completed
Push — master ( e33e66...44de45 )
by Tobias
03:12
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 68
    public function __construct(
38
        $method,
39
        $uri,
40
        array $headers = [],
41
        $body = null,
42
        $version = '1.1'
43
    ) {
44 68
        if (!($uri instanceof UriInterface)) {
45 53
            $uri = new Uri($uri);
46
        }
47
48 67
        $this->method = $method;
49 67
        $this->uri = $uri;
50 67
        $this->setHeaders($headers);
51 67
        $this->protocol = $version;
52
53 67
        if (!$this->hasHeader('Host')) {
54 66
            $this->updateHostFromUri();
55
        }
56
57 67 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 67
    }
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 1
    public function withMethod($method): self
97
    {
98 1
        if (!is_string($method)) {
99 1
            throw new \InvalidArgumentException('Method must be a string');
100
        }
101 1
102
        $new = clone $this;
103
        $new->method = $method;
104 30
105
        return $new;
106 30
    }
107
108
    public function getUri(): UriInterface
109 9
    {
110
        return $this->uri;
111 9
    }
112 1
113
    public function withUri(UriInterface $uri, $preserveHost = false): self
114
    {
115 8
        if ($uri === $this->uri) {
116 8
            return $this;
117
        }
118 8
119 7
        $new = clone $this;
120
        $new->uri = $uri;
121
122 8
        if (!$preserveHost || !$this->hasHeader('Host')) {
123
            $new->updateHostFromUri();
124
        }
125 66
126
        return $new;
127 66
    }
128
129 66
    private function updateHostFromUri()
130 28
    {
131
        $host = $this->uri->getHost();
132
133 43
        if ('' == $host) {
134 2
            return;
135
        }
136
137 43
        if (null !== ($port = $this->uri->getPort())) {
138 2
            $host .= ':'.$port;
139
        }
140 43
141 43
        if (isset($this->headerNames['host'])) {
142
            $header = $this->headerNames['host'];
143
        } else {
144
            $header = 'Host';
145 43
            $this->headerNames['host'] = 'Host';
146 43
        }
147
        // Ensure Host is the first header.
148
        // See: http://tools.ietf.org/html/rfc7230#section-5.4
149
        $this->headers = [$header => [$host]] + $this->headers;
150
    }
151
}
152