diff --git a/src/Reflection/ParametersAcceptorSelector.php b/src/Reflection/ParametersAcceptorSelector.php index 2a895c1494..8c457a2bef 100644 --- a/src/Reflection/ParametersAcceptorSelector.php +++ b/src/Reflection/ParametersAcceptorSelector.php @@ -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_POSTFIELDS) { + return new UnionType([new StringType(), new ArrayType(new MixedType(), new MixedType())]); + } + $boolConstants = [ 'CURLOPT_AUTOREFERER', 'CURLOPT_COOKIESESSION', @@ -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', diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index d221be4a7d..43a315f838 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -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, + ], ]); } diff --git a/tests/PHPStan/Rules/Functions/data/curl_setopt.php b/tests/PHPStan/Rules/Functions/data/curl_setopt.php index 1a42bc2dd4..4c5dd9848a 100644 --- a/tests/PHPStan/Rules/Functions/data/curl_setopt.php +++ b/tests/PHPStan/Rules/Functions/data/curl_setopt.php @@ -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); } /** @@ -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¶2=val2'); } }