Skip to content

Commit a34c2f4

Browse files
authored
Update curl_setopt string values and allow nullable
1 parent 4d295ca commit a34c2f4

File tree

3 files changed

+81
-20
lines changed

3 files changed

+81
-20
lines changed

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -918,49 +918,91 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
918918
}
919919
}
920920

921+
$nullableStringConstants = [
922+
'CURLOPT_ACCEPT_ENCODING',
923+
'CURLOPT_CUSTOMREQUEST',
924+
'CURLOPT_DNS_INTERFACE',
925+
'CURLOPT_DNS_LOCAL_IP4',
926+
'CURLOPT_DNS_LOCAL_IP6',
927+
'CURLOPT_DOH_URL',
928+
'CURLOPT_FTP_ACCOUNT',
929+
'CURLOPT_FTPPORT',
930+
'CURLOPT_HSTS',
931+
'CURLOPT_KRBLEVEL',
932+
'CURLOPT_RANGE',
933+
'CURLOPT_RTSP_SESSION_ID',
934+
'CURLOPT_UNIX_SOCKET_PATH',
935+
'CURLOPT_XOAUTH2_BEARER',
936+
];
937+
foreach ($nullableStringConstants as $constName) {
938+
if (defined($constName) && constant($constName) === $curlOpt) {
939+
return new UnionType([
940+
new NullType(),
941+
TypeCombinator::intersect(
942+
new StringType(),
943+
new AccessoryNonEmptyStringType(),
944+
),
945+
]);
946+
}
947+
}
948+
921949
$nonEmptyStringConstants = [
922950
'CURLOPT_ABSTRACT_UNIX_SOCKET',
951+
'CURLOPT_ALTSVC',
952+
'CURLOPT_AWS_SIGV4',
923953
'CURLOPT_CAINFO',
924954
'CURLOPT_CAPATH',
925955
'CURLOPT_COOKIE',
926956
'CURLOPT_COOKIEJAR',
927957
'CURLOPT_COOKIELIST',
928-
'CURLOPT_CUSTOMREQUEST',
929958
'CURLOPT_DEFAULT_PROTOCOL',
930-
'CURLOPT_DNS_INTERFACE',
931-
'CURLOPT_DNS_LOCAL_IP4',
932-
'CURLOPT_DNS_LOCAL_IP6',
959+
'CURLOPT_DNS_SERVERS',
933960
'CURLOPT_EGDSOCKET',
934-
'CURLOPT_FTPPORT',
961+
'CURLOPT_FTP_ALTERNATIVE_TO_USER',
935962
'CURLOPT_INTERFACE',
936963
'CURLOPT_KEYPASSWD',
937964
'CURLOPT_KRB4LEVEL',
938965
'CURLOPT_LOGIN_OPTIONS',
966+
'CURLOPT_MAIL_AUTH',
967+
'CURLOPT_MAIL_FROM',
968+
'CURLOPT_NOPROXY',
969+
'CURLOPT_PASSWORD',
939970
'CURLOPT_PINNEDPUBLICKEY',
940-
'CURLOPT_PROXY_SERVICE_NAME',
971+
'CURLOPT_PROTOCOLS_STR',
941972
'CURLOPT_PROXY_CAINFO',
942973
'CURLOPT_PROXY_CAPATH',
943974
'CURLOPT_PROXY_CRLFILE',
975+
'CURLOPT_PROXY_ISSUERCERT',
944976
'CURLOPT_PROXY_KEYPASSWD',
945977
'CURLOPT_PROXY_PINNEDPUBLICKEY',
978+
'CURLOPT_PROXY_SERVICE_NAME',
979+
'CURLOPT_PROXY_SSL_CIPHER_LIST',
946980
'CURLOPT_PROXY_SSLCERT',
947981
'CURLOPT_PROXY_SSLCERTTYPE',
948-
'CURLOPT_PROXY_SSL_CIPHER_LIST',
949-
'CURLOPT_PROXY_TLS13_CIPHERS',
950982
'CURLOPT_PROXY_SSLKEY',
951983
'CURLOPT_PROXY_SSLKEYTYPE',
984+
'CURLOPT_PROXY_TLS13_CIPHERS',
952985
'CURLOPT_PROXY_TLSAUTH_PASSWORD',
953986
'CURLOPT_PROXY_TLSAUTH_TYPE',
954987
'CURLOPT_PROXY_TLSAUTH_USERNAME',
988+
'CURLOPT_PROXYPASSWORD',
989+
'CURLOPT_PROXYUSERNAME',
955990
'CURLOPT_PROXYUSERPWD',
956991
'CURLOPT_RANDOM_FILE',
957-
'CURLOPT_RANGE',
992+
'CURLOPT_REDIR_PROTOCOLS_STR',
958993
'CURLOPT_REFERER',
994+
'CURLOPT_REQUEST_TARGET',
995+
'CURLOPT_RTSP_STREAM_URI',
996+
'CURLOPT_RTSP_TRANSPORT',
997+
'CURLOPT_SASL_AUTHZID',
959998
'CURLOPT_SERVICE_NAME',
999+
'CURLOPT_SOCKS5_GSSAPI_SERVICE',
9601000
'CURLOPT_SSH_HOST_PUBLIC_KEY_MD5',
961-
'CURLOPT_SSH_PUBLIC_KEYFILE',
1001+
'CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256',
9621002
'CURLOPT_SSH_PRIVATE_KEYFILE',
1003+
'CURLOPT_SSH_PUBLIC_KEYFILE',
9631004
'CURLOPT_SSL_CIPHER_LIST',
1005+
'CURLOPT_SSL_EC_CURVES',
9641006
'CURLOPT_SSLCERT',
9651007
'CURLOPT_SSLCERTPASSWD',
9661008
'CURLOPT_SSLCERTTYPE',
@@ -970,13 +1012,14 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
9701012
'CURLOPT_SSLKEYPASSWD',
9711013
'CURLOPT_SSLKEYTYPE',
9721014
'CURLOPT_TLS13_CIPHERS',
973-
'CURLOPT_UNIX_SOCKET_PATH',
1015+
'CURLOPT_TLSAUTH_PASSWORD',
1016+
'CURLOPT_TLSAUTH_TYPE',
1017+
'CURLOPT_TLSAUTH_USERNAME',
1018+
'CURLOPT_TRANSFER_ENCODING',
9741019
'CURLOPT_URL',
9751020
'CURLOPT_USERAGENT',
9761021
'CURLOPT_USERNAME',
977-
'CURLOPT_PASSWORD',
9781022
'CURLOPT_USERPWD',
979-
'CURLOPT_XOAUTH2_BEARER',
9801023
];
9811024
foreach ($nonEmptyStringConstants as $constName) {
9821025
if (defined($constName) && constant($constName) === $curlOpt) {

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,33 +1316,45 @@ public function testCurlSetOpt(): void
13161316
'Parameter #3 $value of function curl_setopt expects array<int, string>, int given.',
13171317
17,
13181318
],
1319+
[
1320+
'Parameter #3 $value of function curl_setopt expects non-empty-string, null given.',
1321+
18,
1322+
],
13191323
[
13201324
'Parameter #3 $value of function curl_setopt expects bool, int given.',
1321-
19,
1325+
20,
13221326
],
13231327
[
13241328
'Parameter #3 $value of function curl_setopt expects bool, string given.',
1325-
20,
1329+
21,
13261330
],
13271331
[
13281332
'Parameter #3 $value of function curl_setopt expects int, string given.',
1329-
22,
1333+
23,
13301334
],
13311335
[
13321336
'Parameter #3 $value of function curl_setopt expects array, string given.',
1333-
24,
1337+
25,
13341338
],
13351339
[
13361340
'Parameter #3 $value of function curl_setopt expects resource, string given.',
1337-
26,
1341+
27,
13381342
],
13391343
[
13401344
'Parameter #3 $value of function curl_setopt expects array|string, int given.',
1341-
28,
1345+
29,
1346+
],
1347+
[
1348+
'Parameter #3 $value of function curl_setopt expects non-empty-string, \'\' given.',
1349+
31,
1350+
],
1351+
[
1352+
'Parameter #3 $value of function curl_setopt expects non-empty-string|null, \'\' given.',
1353+
32,
13421354
],
13431355
[
13441356
'Parameter #3 $value of function curl_setopt expects array<int, string>, array<string, string> given.',
1345-
67,
1357+
73,
13461358
],
13471359
]);
13481360
}

tests/PHPStan/Rules/Functions/data/curl_setopt.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function errors(int $i, string $s) {
1515
// expecting string
1616
curl_setopt($curl, CURLOPT_URL, $i);
1717
curl_setopt($curl, CURLOPT_HTTPHEADER, $i);
18+
curl_setopt($curl, CURLOPT_ABSTRACT_UNIX_SOCKET, null);
1819
// expecting bool
1920
curl_setopt($curl, CURLOPT_AUTOREFERER, $i);
2021
curl_setopt($curl, CURLOPT_RETURNTRANSFER, $s);
@@ -26,6 +27,9 @@ public function errors(int $i, string $s) {
2627
curl_setopt($curl, CURLOPT_FILE, $s);
2728
// expecting string or array
2829
curl_setopt($curl, CURLOPT_POSTFIELDS, $i);
30+
// expecting non empty string
31+
curl_setopt($curl, CURLOPT_URL, '');
32+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, '');
2933
}
3034

3135
/**
@@ -41,6 +45,8 @@ public function allGood(string $url, array $header) {
4145
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
4246
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
4347
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
48+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
49+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, null);
4450

4551
$fp = fopen("example_homepage.txt", "w");
4652
if ($fp === false) {

0 commit comments

Comments
 (0)