From 31a81a814b9f0ddda7c734c4d6ab1adace0f958c Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 30 Sep 2022 16:39:32 +0200 Subject: [PATCH 1/2] Fix type for CURLOPT_POSTFIELDS --- src/Reflection/ParametersAcceptorSelector.php | 5 ++++- tests/PHPStan/Rules/Functions/data/curl_setopt.php | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Reflection/ParametersAcceptorSelector.php b/src/Reflection/ParametersAcceptorSelector.php index 2a895c1494..faef543dc5 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_SSL_VERIFYHOST) { + 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/data/curl_setopt.php b/tests/PHPStan/Rules/Functions/data/curl_setopt.php index 1a42bc2dd4..9221cc9a0d 100644 --- a/tests/PHPStan/Rules/Functions/data/curl_setopt.php +++ b/tests/PHPStan/Rules/Functions/data/curl_setopt.php @@ -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¶2=val2'); } } From 322d19dd83c8e2234d46bd16e32e7480d61dfb44 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 30 Sep 2022 23:42:41 +0200 Subject: [PATCH 2/2] Review --- src/Reflection/ParametersAcceptorSelector.php | 2 +- .../Rules/Functions/CallToFunctionParametersRuleTest.php | 4 ++++ tests/PHPStan/Rules/Functions/data/curl_setopt.php | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Reflection/ParametersAcceptorSelector.php b/src/Reflection/ParametersAcceptorSelector.php index faef543dc5..8c457a2bef 100644 --- a/src/Reflection/ParametersAcceptorSelector.php +++ b/src/Reflection/ParametersAcceptorSelector.php @@ -515,7 +515,7 @@ private static function getCurlOptValueType(int $curlOpt): ?Type return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(2)]); } - if (defined('CURLOPT_POSTFIELDS') && $curlOpt === CURLOPT_SSL_VERIFYHOST) { + if (defined('CURLOPT_POSTFIELDS') && $curlOpt === CURLOPT_POSTFIELDS) { return new UnionType([new StringType(), new ArrayType(new MixedType(), new MixedType())]); } 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 9221cc9a0d..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); } /**