Skip to content

Commit

Permalink
Fix CURLOPT_POSTFIELDS param type
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedash95 committed Oct 2, 2022
1 parent 1c9f57c commit 228a345
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Expand Up @@ -646,7 +646,6 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
'CURLOPT_KRB4LEVEL',
'CURLOPT_LOGIN_OPTIONS',
'CURLOPT_PINNEDPUBLICKEY',
'CURLOPT_POSTFIELDS',
'CURLOPT_PRIVATE',
'CURLOPT_PRE_PROXY',
'CURLOPT_PROXY',
Expand Down Expand Up @@ -724,6 +723,18 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
}
}

$arrayOrStringConstants = [
'CURLOPT_POSTFIELDS',
];
foreach ($arrayOrStringConstants as $constName) {
if (defined($constName) && constant($constName) === $curlOpt) {
return new UnionType([
new StringType(),
new ArrayType(new MixedType(), new MixedType()),
]);
}
}

$resourceConstants = [
'CURLOPT_FILE',
'CURLOPT_INFILE',
Expand Down
Expand Up @@ -1213,6 +1213,10 @@ public function testCurlSetOpt(): void
'Parameter #3 $value of function curl_setopt expects resource, string given.',
24,
],
[
'Parameter #3 $value of function curl_setopt expects array|string, int given.',
26,
],
]);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Functions/data/curl_setopt.php
Expand Up @@ -22,6 +22,8 @@ public function errors(int $i, string $s) {
curl_setopt($curl, CURLOPT_CONNECT_TO, $s);
// expecting resource
curl_setopt($curl, CURLOPT_FILE, $s);
// expecting string or array
curl_setopt($curl, CURLOPT_POSTFIELDS, $i);
}

/**
Expand All @@ -45,5 +47,8 @@ public function allGood(string $url, array $header) {
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: text/plain', 'Content-length: 100'));
curl_setopt($curl, CURLOPT_POSTFIELDS, array('foo' => 'bar'));
curl_setopt($curl, CURLOPT_POSTFIELDS, '');
curl_setopt($curl, CURLOPT_POSTFIELDS, 'para1=val1&para2=val2');
}
}

0 comments on commit 228a345

Please sign in to comment.