Skip to content

Commit 5e1657b

Browse files
authored
Narrow CURLOPT_SHARE accepting type
1 parent dc9bf7f commit 5e1657b

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

src/Php/PhpVersion.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,19 @@ public function isCurloptUrlCheckingFileSchemeWithOpenBasedir(): bool
394394
return $this->versionId < 80000;
395395
}
396396

397+
/**
398+
* whether curl handles are represented as 'resource' or CurlShareHandle
399+
*/
400+
public function supportsCurlShareHandle(): bool
401+
{
402+
return $this->versionId >= 80000;
403+
}
404+
405+
public function supportsCurlSharePersistentHandle(): bool
406+
{
407+
return $this->versionId >= 80500;
408+
}
409+
397410
public function highlightStringDoesNotReturnFalse(): bool
398411
{
399412
return $this->versionId >= 80400;

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
use function sprintf;
5959
use const ARRAY_FILTER_USE_BOTH;
6060
use const ARRAY_FILTER_USE_KEY;
61+
use const CURLOPT_SHARE;
6162
use const CURLOPT_SSL_VERIFYHOST;
6263

6364
/**
@@ -1212,6 +1213,21 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
12121213
}
12131214
}
12141215

1216+
if ($curlOpt === CURLOPT_SHARE) {
1217+
$phpversion = PhpVersionStaticAccessor::getInstance();
1218+
1219+
if ($phpversion->supportsCurlShareHandle()) {
1220+
$shareType = new ObjectType('CurlShareHandle');
1221+
} else {
1222+
$shareType = new ResourceType();
1223+
}
1224+
if ($phpversion->supportsCurlSharePersistentHandle()) {
1225+
$shareType = TypeCombinator::union($shareType, new ObjectType('CurlSharePersistentHandle'));
1226+
}
1227+
1228+
return $shareType;
1229+
}
1230+
12151231
// unknown constant
12161232
return null;
12171233
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,25 @@ public function testCurlSetOpt(): void
14011401
]);
14021402
}
14031403

1404+
public function testCurlSetOptInvalidShare(): void
1405+
{
1406+
if (PHP_VERSION_ID < 80000) {
1407+
$errors = [
1408+
['Parameter #3 $value of function curl_setopt expects resource, string given.', 8],
1409+
];
1410+
} elseif (PHP_VERSION_ID < 80500) {
1411+
$errors = [
1412+
['Parameter #3 $value of function curl_setopt expects CurlShareHandle, string given.', 8],
1413+
];
1414+
} else {
1415+
$errors = [
1416+
['Parameter #3 $value of function curl_setopt expects CurlShareHandle|CurlSharePersistentHandle, string given.', 8],
1417+
];
1418+
}
1419+
1420+
$this->analyse([__DIR__ . '/data/curl_setopt_share.php'], $errors);
1421+
}
1422+
14041423
#[RequiresPhp('>= 8.1')]
14051424
public function testCurlSetOptArray(): void
14061425
{

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,23 @@ public function unionType() {
9595

9696
curl_setopt($curl, $var, $value);
9797
}
98+
99+
public function curlShare() {
100+
$curl = curl_init();
101+
102+
$share = curl_share_init();
103+
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
104+
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
105+
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
106+
curl_setopt($curl, CURLOPT_SHARE, $share);
107+
108+
if (function_exists('curl_share_init_persistent')) {
109+
$share = curl_share_init_persistent([
110+
CURL_LOCK_DATA_DNS,
111+
CURL_LOCK_DATA_CONNECT,
112+
CURL_LOCK_DATA_SSL_SESSION,
113+
]);
114+
curl_setopt($curl, CURLOPT_SHARE, $share);
115+
}
116+
}
98117
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace CurlSetOptShare;
4+
5+
function curlShare()
6+
{
7+
$curl = curl_init();
8+
curl_setopt($curl, CURLOPT_SHARE, 'this is wrong');
9+
}

0 commit comments

Comments
 (0)