Skip to content

Commit 3c27131

Browse files
committed
feature detect
1 parent a1ee27f commit 3c27131

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,11 +1215,18 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
12151215
}
12161216

12171217
if ($curlOpt === CURLOPT_SHARE) {
1218-
return new UnionType([
1219-
new ResourceType(), // PHP 7.x
1220-
new ObjectType('CurlShareHandle'), // since PHP 8.0
1221-
new ObjectType('CurlSharePersistentHandle'), // since PHP 8.5
1222-
]);
1218+
$phpversion = PhpVersionStaticAccessor::getInstance();
1219+
1220+
if ($phpversion->supportsCurlShareHandle()) {
1221+
$shareType = new ObjectType('CurlShareHandle');
1222+
} else {
1223+
$shareType = new ResourceType();
1224+
}
1225+
if ($phpversion->supportsCurlSharePersistentHandle()) {
1226+
$shareType = TypeCombinator::union($shareType, new ObjectType('CurlSharePersistentHandle'));
1227+
}
1228+
1229+
return $shareType;
12231230
}
12241231

12251232
// unknown constant

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,13 +1398,28 @@ public function testCurlSetOpt(): void
13981398
'Parameter #3 $value of function curl_setopt expects bool|int, int|string given.',
13991399
96,
14001400
],
1401-
[
1402-
'Parameter #3 $value of function curl_setopt expects CurlShareHandle|CurlSharePersistentHandle|resource, string given.',
1403-
101,
1404-
],
14051401
]);
14061402
}
14071403

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+
14081423
#[RequiresPhp('>= 8.1')]
14091424
public function testCurlSetOptArray(): void
14101425
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public function unionType() {
9898

9999
public function curlShare() {
100100
$curl = curl_init();
101-
curl_setopt($curl, CURLOPT_SHARE, 'this is wrong');
102101

103102
$share = curl_share_init();
104103
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
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)