Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CURLOPT_POSTFIELDS param type #1782

Merged
merged 2 commits into from Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
}
}