Skip to content

Commit 1d9ba2f

Browse files
committed
- improved error consistency
- improved headers and status code extraction
1 parent 66144b5 commit 1d9ba2f

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

Client/PhpClient.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public function read(): Response
6161
if ($this->hasError($resource)) {
6262
return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY);
6363
}
64-
$this->extractFromResponseHeaders($resource, $statusCode, $headers);
65-
return new ServerResponse(\stream_get_contents($resource), $statusCode, $headers);
64+
return new ServerResponse(
65+
\stream_get_contents($resource),
66+
...$this->extractHeadersFromResource($resource));
6667
} catch (\ValueError $e) {
6768
return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY, $e->getMessage());
6869
} catch (\Throwable $e) {
@@ -156,28 +157,29 @@ protected function prepareOptions(): void
156157
/**
157158
* Extracts the headers and status code from the response.
158159
*
159-
* @param resource $response The resource from fopen()
160-
* @param int $statusCode Response status code
161-
* @param array $headers Parsed response headers
160+
* @param resource $resource The resource from fopen()
161+
* @return array Status code and headers
162162
*/
163-
protected function extractFromResponseHeaders($response, &$statusCode, &$headers): void
163+
protected function extractHeadersFromResource($resource): array
164164
{
165165
try {
166-
$meta = \stream_get_meta_data($response)['wrapper_data'] ?? [];
166+
$headers = [];
167+
$meta = \stream_get_meta_data($resource)['wrapper_data'] ?? [];
167168
/* HTTP status may not always be the first header in the response headers,
168169
* for example, if the stream follows one or multiple redirects, the last
169170
* status line is what is expected here.
170171
*/
171-
$statusCode = \array_filter($meta, fn(string $header) => \str_starts_with($header, 'HTTP/'));
172-
$statusCode = \array_pop($statusCode) ?: 'HTTP/1.1 200 OK';
173-
$statusCode = (int)(\explode(' ', $statusCode)[1] ?? HttpStatus::OK);
172+
$status = \array_filter($meta, fn(string $header) => \str_starts_with($header, 'HTTP/'));
173+
$status = \array_pop($status) ?: 'HTTP/1.1 200 OK';
174+
$status = (int)(\explode(' ', $status)[1] ?? HttpStatus::OK);
174175
foreach ($meta as $header) {
175176
[$k, $v] = \explode(':', $header, 2) + [1 => null];
176177
if (null === $v) {
177178
continue;
178179
}
179180
$headers[$k] = $v;
180181
}
182+
return [$status, $headers];
181183
} finally {
182184
unset($meta, $header, $k, $v);
183185
}

0 commit comments

Comments
 (0)