Skip to content

Commit 816585c

Browse files
committed
Fix type annotations for headers
`ResponseInterface::getHeaders()` only ever returns `array<string, string[]>`, let’s work with that internally.
1 parent cbba26b commit 816585c

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/ContentTypeExtractor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static function getContentTypeFromHeader(array $headers, string $targetEn
3737

3838
if (\is_array($contentType)) {
3939
// Multiple Content-Type headers are not permitted, as the header does not accept a comma-separated list:
40-
// https://tools.ietf.org/html/rfc2616#section-4.2
40+
// https://www.rfc-editor.org/rfc/rfc2616#section-4.2
41+
// https://www.rfc-editor.org/rfc/rfc2616#section-14.17
4142
// We are attempting to handle it gracefully by dropping all but the first instance.
4243
$contentType = $contentType[0];
4344
}

src/GuzzleTranscoder.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private function createTranscoder(): TranscoderInterface {
5656
public function convert(ResponseInterface $response): ResponseInterface {
5757
$stream = $response->getBody();
5858

59-
/** @var array<string, array<string>|string> */
59+
/** @var array<string, string[]> */
6060
$headers = $response->getHeaders();
6161
$result = $this->convertResponse($headers, (string) $stream);
6262
if ($result !== null) {
@@ -100,14 +100,13 @@ public function __invoke(callable $handler): callable {
100100
*
101101
* Otherwise an array containing the new headers and content is returned.
102102
*
103-
* @param array<string, array<string>|string> $headers
103+
* @param array<string, string[]> $headers
104104
*
105-
* @return ?array{headers: array<string, array<string>|string>, content: string}
105+
* @return ?array{headers: array<string, string[]>, content: string}
106106
*/
107107
public function convertResponse(array $headers, string $content): ?array {
108108
$headerDeclaredEncoding = null;
109109
$bodyDeclaredEncoding = null;
110-
/** @var array<string, array<string>|string> */
111110
$headerReplacements = [];
112111
$contentReplacements = [];
113112

@@ -116,7 +115,7 @@ public function convertResponse(array $headers, string $content): ?array {
116115
if ($type !== null) {
117116
[$contentType, $headerDeclaredEncoding, $params] = $type;
118117

119-
$headerReplacements['content-type'] = $contentType . (\count($params) > 0 ? '; ' . Utils::joinHttpHeaderWords($params) : '');
118+
$headerReplacements['content-type'] = [$contentType . (\count($params) > 0 ? '; ' . Utils::joinHttpHeaderWords($params) : '')];
120119
} else {
121120
return null;
122121
}

tests/GuzzleTranscoderTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public function testConvertResponse(): void {
214214
* Gets the headers from a HTTP response as one dimensional associative array
215215
* with header names as keys. The header values will not be parsed but saved as-is!
216216
*
217-
* @return array{headers: array<string>, body: string}
217+
* @return array{headers: array<string, string[]>, body: string}
218218
*/
219219
private function splitHeadersAndContentFromHttpResponseString(string $responseString): array {
220220
$lines = explode("\n", $responseString);
@@ -225,13 +225,9 @@ private function splitHeadersAndContentFromHttpResponseString(string $responseSt
225225
if ($line === '') {
226226
break;
227227
}
228-
$parts = explode(':', $line);
229-
$key = array_shift($parts);
230-
if (\count($parts) > 0) {
231-
$headers[$key] = trim(implode(':', $parts));
232-
} else {
233-
$headers[$key] = '';
234-
}
228+
$parts = explode(':', $line, 2);
229+
$key = $parts[0];
230+
$headers[$key] = \count($parts) > 1 ? [trim($parts[1])] : [];
235231
}
236232
$body = implode("\n", \array_slice($lines, $i + 1));
237233

0 commit comments

Comments
 (0)