Skip to content

Commit b0d1fa4

Browse files
authored
Fix "Array does not have offset" in curl_setopt_array()
1 parent b81c9fc commit b0d1fa4

File tree

5 files changed

+118
-1
lines changed

5 files changed

+118
-1
lines changed

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ public static function selectFromArgs(
181181

182182
$hasTypes = false;
183183
$builder = ConstantArrayTypeBuilder::createEmpty();
184-
foreach ($optArrayType->getIterableKeyType()->getConstantScalarValues() as $optValue) {
184+
foreach ($optArrayType->getIterableKeyType()->getConstantScalarTypes() as $optType) {
185+
$optValue = $optType->getValue();
186+
185187
if (!is_int($optValue)) {
186188
$hasTypes = false;
187189
break;
@@ -197,6 +199,7 @@ public static function selectFromArgs(
197199
$builder->setOffsetValueType(
198200
new ConstantIntegerType($optValue),
199201
$optValueType,
202+
!$optArrayType->hasOffsetValueType($optType)->yes(),
200203
);
201204
}
202205

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,4 +2476,28 @@ public function testClone(): void
24762476
]);
24772477
}
24782478

2479+
#[RequiresPhp('>= 8.1')]
2480+
public function testBug13862(): void
2481+
{
2482+
$this->analyse([__DIR__ . '/data/bug-13862.php'], []);
2483+
}
2484+
2485+
#[RequiresPhp('>= 8.1')]
2486+
public function testBug13862b(): void
2487+
{
2488+
$this->analyse([__DIR__ . '/data/bug-13862b.php'], []);
2489+
}
2490+
2491+
#[RequiresPhp('>= 8.1')]
2492+
public function testBug13862c(): void
2493+
{
2494+
$this->analyse([__DIR__ . '/data/bug-13862c.php'], [
2495+
[
2496+
'Parameter #2 $options of function curl_setopt_array expects array{10022?: non-empty-string}, array{}|array{10022: 123} given.',
2497+
12,
2498+
'Offset 10022 (non-empty-string) does not accept type 123.',
2499+
],
2500+
]);
2501+
}
2502+
24792503
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Bug13862;
4+
5+
class foo
6+
{
7+
8+
/**
9+
* @param string $url
10+
* @param string $curl_interface
11+
* @param ?int $port
12+
* @param ?string $post
13+
* @return string
14+
*/
15+
public function getUrl(
16+
string $url,
17+
string $curl_interface = "",
18+
?int $port = null,
19+
?string $post = null
20+
): string
21+
{
22+
23+
if (empty($url))
24+
return "";
25+
26+
$options = array(
27+
CURLOPT_URL => $url,
28+
CURLOPT_CONNECTTIMEOUT => 5,
29+
CURLOPT_TIMEOUT => 15,
30+
CURLOPT_RETURNTRANSFER => true,
31+
CURLOPT_PORT => 443,
32+
CURLOPT_ENCODING => "gzip, deflate",
33+
CURLOPT_HTTPHEADER => array("Accept-Encoding: gzip, deflate")
34+
);
35+
36+
if ($curl_interface != "")
37+
$options[CURLOPT_INTERFACE] = $curl_interface;
38+
39+
if ($port !== null)
40+
$options[CURLOPT_PORT] = $port;
41+
42+
if ($post !== null) {
43+
$options[CURLOPT_POST] = true;
44+
$options[CURLOPT_POSTFIELDS] = $post;
45+
$options[CURLOPT_HTTPHEADER] = array(
46+
"Content-Type: application/json;charset=\"UTF-8\"",
47+
"Accept: application/json",
48+
"Accept-Encoding: gzip, deflate",
49+
"Content-Length: " . mb_strlen($post, "UTF-8")
50+
);
51+
}
52+
53+
$curl = curl_init();
54+
curl_setopt_array($curl, $options);
55+
$rc = curl_exec($curl);
56+
if (is_bool($rc))
57+
return "";
58+
else
59+
return $rc;
60+
61+
}
62+
63+
}
64+
65+
$foo = new foo();
66+
$data = $foo->getUrl("https://example.tld");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Bug13862b;
4+
5+
$options = [];
6+
7+
if (rand()) {
8+
$options[CURLOPT_COOKIE] = 'foo=bar';
9+
}
10+
11+
$ch = curl_init();
12+
curl_setopt_array($ch, $options);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Bug13862c;
4+
5+
$options = [];
6+
7+
if (rand()) {
8+
$options[CURLOPT_COOKIE] = 123;
9+
}
10+
11+
$ch = curl_init();
12+
curl_setopt_array($ch, $options);

0 commit comments

Comments
 (0)