Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,19 @@ public function isCurloptUrlCheckingFileSchemeWithOpenBasedir(): bool
return $this->versionId < 80000;
}

/**
* whether curl handles are represented as 'resource' or CurlShareHandle
*/
public function supportsCurlShareHandle(): bool
{
return $this->versionId >= 80000;
}

public function supportsCurlSharePersistentHandle(): bool
{
return $this->versionId >= 80500;
}

public function highlightStringDoesNotReturnFalse(): bool
{
return $this->versionId >= 80400;
Expand Down
16 changes: 16 additions & 0 deletions src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use function sprintf;
use const ARRAY_FILTER_USE_BOTH;
use const ARRAY_FILTER_USE_KEY;
use const CURLOPT_SHARE;
use const CURLOPT_SSL_VERIFYHOST;

/**
Expand Down Expand Up @@ -1212,6 +1213,21 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
}
}

if ($curlOpt === CURLOPT_SHARE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor issue: this crashes due to Undefined constant "CURLOPT_SHARE" when analysing curl_setopt without curl extension. It seems that the rest of the method uses defined($constName) && constant($constName) === $curlOpt.

E.g. just this is enough curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); to trigger a crash if the curl extension is disabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please send a fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$phpversion = PhpVersionStaticAccessor::getInstance();

if ($phpversion->supportsCurlShareHandle()) {
$shareType = new ObjectType('CurlShareHandle');
} else {
$shareType = new ResourceType();
}
if ($phpversion->supportsCurlSharePersistentHandle()) {
$shareType = TypeCombinator::union($shareType, new ObjectType('CurlSharePersistentHandle'));
}

return $shareType;
}

// unknown constant
return null;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,25 @@ public function testCurlSetOpt(): void
]);
}

public function testCurlSetOptInvalidShare(): void
{
if (PHP_VERSION_ID < 80000) {
$errors = [
['Parameter #3 $value of function curl_setopt expects resource, string given.', 8],
];
} elseif (PHP_VERSION_ID < 80500) {
$errors = [
['Parameter #3 $value of function curl_setopt expects CurlShareHandle, string given.', 8],
];
} else {
$errors = [
['Parameter #3 $value of function curl_setopt expects CurlShareHandle|CurlSharePersistentHandle, string given.', 8],
];
}

$this->analyse([__DIR__ . '/data/curl_setopt_share.php'], $errors);
}

#[RequiresPhp('>= 8.1')]
public function testCurlSetOptArray(): void
{
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Functions/data/curl_setopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,23 @@ public function unionType() {

curl_setopt($curl, $var, $value);
}

public function curlShare() {
$curl = curl_init();

$share = curl_share_init();
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
curl_setopt($curl, CURLOPT_SHARE, $share);

if (function_exists('curl_share_init_persistent')) {
$share = curl_share_init_persistent([
CURL_LOCK_DATA_DNS,
CURL_LOCK_DATA_CONNECT,
CURL_LOCK_DATA_SSL_SESSION,
]);
curl_setopt($curl, CURLOPT_SHARE, $share);
}
}
}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Functions/data/curl_setopt_share.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace CurlSetOptShare;

function curlShare()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SHARE, 'this is wrong');
}
Loading