Skip to content

Commit 57b41bd

Browse files
committed
refactor: Improve $http_response_header forward compatibilty to avoid false positives
See php/php-src#19611, keeping the deprecated variable name leads to false positives
1 parent 45d261a commit 57b41bd

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/JsonSchema/Uri/Retrievers/FileGetContents.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,12 @@ public function retrieve($uri)
5151
}
5252

5353
$this->messageBody = $response;
54-
if (function_exists('http_get_last_response_headers')) {
55-
// Use http_get_last_response_headers() for BC compatibility with PHP 8.5+
56-
// where $http_response_header is deprecated.
57-
$http_response_header = http_get_last_response_headers();
58-
}
59-
if (!empty($http_response_header)) {
60-
// $http_response_header cannot be tested, because it's defined in the method's local scope
61-
// See http://php.net/manual/en/reserved.variables.httpresponseheader.php for more info.
62-
$this->fetchContentType($http_response_header); // @codeCoverageIgnore
63-
} else { // @codeCoverageIgnore
64-
// Could be a "file://" url or something else - fake up the response
54+
55+
$httpResponseHeaders = PHP_VERSION >= 80400 ? http_get_last_response_headers() : $http_response_header ?? [];
56+
57+
if (!empty($httpResponseHeaders)) {
58+
$this->fetchContentType($httpResponseHeaders);
59+
} else {
6560
$this->contentType = null;
6661
}
6762

@@ -73,7 +68,7 @@ public function retrieve($uri)
7368
*
7469
* @return bool Whether the Content-Type header was found or not
7570
*/
76-
private function fetchContentType(array $headers)
71+
private function fetchContentType(array $headers): bool
7772
{
7873
foreach (array_reverse($headers) as $header) {
7974
if ($this->contentType = self::getContentTypeMatchInHeader($header)) {

0 commit comments

Comments
 (0)