diff options
Diffstat (limited to 'guzzlehttp/psr7/src/MessageTrait.php')
-rw-r--r-- | guzzlehttp/psr7/src/MessageTrait.php | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/guzzlehttp/psr7/src/MessageTrait.php b/guzzlehttp/psr7/src/MessageTrait.php index 0ac8663d..d2dc28b6 100644 --- a/guzzlehttp/psr7/src/MessageTrait.php +++ b/guzzlehttp/psr7/src/MessageTrait.php @@ -1,7 +1,10 @@ <?php +declare(strict_types=1); + namespace GuzzleHttp\Psr7; +use Psr\Http\Message\MessageInterface; use Psr\Http\Message\StreamInterface; /** @@ -9,10 +12,10 @@ use Psr\Http\Message\StreamInterface; */ trait MessageTrait { - /** @var array Map of all registered headers, as original name => array of values */ + /** @var array<string, string[]> Map of all registered headers, as original name => array of values */ private $headers = []; - /** @var array Map of lowercase header name => original name at registration */ + /** @var array<string, string> Map of lowercase header name => original name at registration */ private $headerNames = []; /** @var string */ @@ -21,12 +24,12 @@ trait MessageTrait /** @var StreamInterface|null */ private $stream; - public function getProtocolVersion() + public function getProtocolVersion(): string { return $this->protocol; } - public function withProtocolVersion($version) + public function withProtocolVersion($version): MessageInterface { if ($this->protocol === $version) { return $this; @@ -37,17 +40,17 @@ trait MessageTrait return $new; } - public function getHeaders() + public function getHeaders(): array { return $this->headers; } - public function hasHeader($header) + public function hasHeader($header): bool { return isset($this->headerNames[strtolower($header)]); } - public function getHeader($header) + public function getHeader($header): array { $header = strtolower($header); @@ -60,12 +63,12 @@ trait MessageTrait return $this->headers[$header]; } - public function getHeaderLine($header) + public function getHeaderLine($header): string { return implode(', ', $this->getHeader($header)); } - public function withHeader($header, $value) + public function withHeader($header, $value): MessageInterface { $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); @@ -81,7 +84,7 @@ trait MessageTrait return $new; } - public function withAddedHeader($header, $value) + public function withAddedHeader($header, $value): MessageInterface { $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); @@ -99,7 +102,7 @@ trait MessageTrait return $new; } - public function withoutHeader($header) + public function withoutHeader($header): MessageInterface { $normalized = strtolower($header); @@ -115,7 +118,7 @@ trait MessageTrait return $new; } - public function getBody() + public function getBody(): StreamInterface { if (!$this->stream) { $this->stream = Utils::streamFor(''); @@ -124,7 +127,7 @@ trait MessageTrait return $this->stream; } - public function withBody(StreamInterface $body) + public function withBody(StreamInterface $body): MessageInterface { if ($body === $this->stream) { return $this; @@ -135,15 +138,16 @@ trait MessageTrait return $new; } - private function setHeaders(array $headers) + /** + * @param array<string|int, string|string[]> $headers + */ + private function setHeaders(array $headers): void { $this->headerNames = $this->headers = []; foreach ($headers as $header => $value) { - if (is_int($header)) { - // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec - // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass. - $header = (string) $header; - } + // Numeric array keys are converted to int by PHP. + $header = (string) $header; + $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); $normalized = strtolower($header); @@ -162,7 +166,7 @@ trait MessageTrait * * @return string[] */ - private function normalizeHeaderValue($value) + private function normalizeHeaderValue($value): array { if (!is_array($value)) { return $this->trimAndValidateHeaderValues([$value]); @@ -189,7 +193,7 @@ trait MessageTrait * * @see https://tools.ietf.org/html/rfc7230#section-3.2.4 */ - private function trimAndValidateHeaderValues(array $values) + private function trimAndValidateHeaderValues(array $values): array { return array_map(function ($value) { if (!is_scalar($value) && null !== $value) { @@ -210,10 +214,8 @@ trait MessageTrait * @see https://tools.ietf.org/html/rfc7230#section-3.2 * * @param mixed $header - * - * @return void */ - private function assertHeader($header) + private function assertHeader($header): void { if (!is_string($header)) { throw new \InvalidArgumentException(sprintf( @@ -222,10 +224,6 @@ trait MessageTrait )); } - if ($header === '') { - throw new \InvalidArgumentException('Header name can not be empty.'); - } - if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $header)) { throw new \InvalidArgumentException( sprintf( @@ -237,10 +235,6 @@ trait MessageTrait } /** - * @param string $value - * - * @return void - * * @see https://tools.ietf.org/html/rfc7230#section-3.2 * * field-value = *( field-content / obs-fold ) @@ -250,7 +244,7 @@ trait MessageTrait * obs-text = %x80-FF * obs-fold = CRLF 1*( SP / HTAB ) */ - private function assertValue($value) + private function assertValue(string $value): void { // The regular expression intentionally does not support the obs-fold production, because as // per RFC 7230#3.2.4: |