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 type for CURLOPT_POSTFIELDS #1777

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Expand Up @@ -515,6 +515,10 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(2)]);
}

if (defined('CURLOPT_POSTFIELDS') && $curlOpt === CURLOPT_SSL_VERIFYHOST) {
Copy link
Member

Choose a reason for hiding this comment

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

Copy paste error most likely here. Would be nice to verify also when a wrong type is passed when setting CURLOPT_POSTFIELDS.

return new UnionType([new StringType(), new ArrayType(new MixedType(), new MixedType())]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the array key/value types be more precise then mixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly, I dunno.

Doc say

The full data to post in a HTTP "POST" operation. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. Files can be sent using [CURLFile](https://www.php.net/manual/en/class.curlfile.php) or [CURLStringFile](https://www.php.net/manual/en/class.curlstringfile.php), in which case value must be an array.

So, value can be string, arrays, but maybe others things...

And keys, it's string. But I'm kinda worried bout the int keys.

$a[0] = 'foo';
$a[1] = 'bar';

which could be considered as '0=foo&1=bar'. (since '0' key would still be casted to int...)

}

$boolConstants = [
'CURLOPT_AUTOREFERER',
'CURLOPT_COOKIESESSION',
Expand Down Expand Up @@ -646,7 +650,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
3 changes: 3 additions & 0 deletions tests/PHPStan/Rules/Functions/data/curl_setopt.php
Expand Up @@ -45,5 +45,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');
}
}