alexpts /
php-mutable-psr7
| 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
|
|||
| 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
|
|||
| 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 |
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.