Skip to content

Commit

Permalink
CURLOPT_HTTPHEADER should not accept a dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 9, 2023
1 parent d3802c7 commit 5680029
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,18 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
}
}

$intArrayStringKeysConstants = [
'CURLOPT_HTTPHEADER',
];
foreach ($intArrayStringKeysConstants as $constName) {
if (defined($constName) && constant($constName) === $curlOpt) {
return new ArrayType(new IntegerType(), new StringType());
}
}

$arrayConstants = [
'CURLOPT_CONNECT_TO',
'CURLOPT_HTTP200ALIASES',
'CURLOPT_HTTPHEADER',
'CURLOPT_POSTQUOTE',
'CURLOPT_PROXYHEADER',
'CURLOPT_QUOTE',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ public function testCurlSetOpt(): void
14,
],
[
'Parameter #3 $value of function curl_setopt expects array, int given.',
'Parameter #3 $value of function curl_setopt expects array<int, string>, int given.',
15,
],
[
Expand All @@ -1235,6 +1235,10 @@ public function testCurlSetOpt(): void
'Parameter #3 $value of function curl_setopt expects array|string, int given.',
26,
],
[
'Parameter #3 $value of function curl_setopt expects array<int, string>, array<string, string> given.',
65,
],
]);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Functions/data/curl_setopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ public function allGood(string $url, array $header) {
curl_setopt($curl, CURLOPT_PROXY, '');
curl_setopt($curl, CURLOPT_PRIVATE, '');
}

public function bug9263() {
$curl = curl_init();

$header_dictionary = [
'Accept' => 'application/json',
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_dictionary);

$header_list = [
'Accept: application/json',
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_list);
}
}

0 comments on commit 5680029

Please sign in to comment.