Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 41 additions & 39 deletions src/Analytics/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,54 +136,56 @@ public function call(string $method, string $path = '', array $headers = [], arr
unset($headers[$i]);
}

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, php_uname('s').'-'.php_uname('r').':php-'.phpversion());
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
$len = strlen($header);
$header = explode(':', strtolower($header), 2);

if (count($header) < 2) { // ignore invalid headers
return $len;
}

$responseHeaders[strtolower(trim($header[0]))] = trim($header[1]);

return $len;
});
try {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, php_uname('s').'-'.php_uname('r').':php-'.phpversion());
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
$len = strlen($header);
$header = explode(':', strtolower($header), 2);

if (count($header) < 2) { // ignore invalid headers
return $len;
}

$responseHeaders[strtolower(trim($header[0]))] = trim($header[1]);

if ($method != 'GET') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
}
return $len;
});

$responseBody = curl_exec($ch);
if ($method != 'GET') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
}

$responseType = $responseHeaders['Content-Type'] ?? '';
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responseBody = curl_exec($ch);

switch (substr($responseType, 0, strpos($responseType, ';'))) {
case 'application/json':
$responseBody = json_decode($responseBody, true);
break;
}
$responseType = $responseHeaders['Content-Type'] ?? '';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Case mismatch: header key stored lowercase but accessed with mixed case.

Line 153 stores headers with strtolower(), but this line accesses with 'Content-Type'. PHP arrays are case-sensitive, so this will always return the default empty string, preventing JSON response parsing.

-            $responseType = $responseHeaders['Content-Type'] ?? '';
+            $responseType = $responseHeaders['content-type'] ?? '';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$responseType = $responseHeaders['Content-Type'] ?? '';
$responseType = $responseHeaders['content-type'] ?? '';
🤖 Prompt for AI Agents
In src/Analytics/Adapter.php around line 164, the code reads
$responseHeaders['Content-Type'] but headers were normalized to lowercase
earlier, so that key will never match; update the access to use the lowercase
key (e.g. 'content-type') or call strtolower on the looked-up key, so the
Content-Type value is retrieved correctly and JSON responses can be parsed.

$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch)) {
throw new \Exception(curl_error($ch), $responseStatus);
}
switch (substr($responseType, 0, strpos($responseType, ';'))) {
case 'application/json':
$responseBody = json_decode($responseBody, true);
break;
}

curl_close($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch), $responseStatus);
}

if ($responseStatus >= 400) {
if (is_array($responseBody)) {
throw new \Exception(json_encode($responseBody), $responseStatus);
} else {
throw new \Exception($responseStatus.': '.$responseBody, $responseStatus);
if ($responseStatus >= 400) {
if (is_array($responseBody)) {
throw new Exception(json_encode($responseBody), $responseStatus);
} else {
throw new Exception($responseStatus.': '.$responseBody, $responseStatus);
}
}
}

return $responseBody;
return $responseBody;
} finally {
curl_close($ch);
}
}

/**
Expand Down
Loading