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
5 changes: 4 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ public static function selectFromArgs(

$hasTypes = false;
$builder = ConstantArrayTypeBuilder::createEmpty();
foreach ($optArrayType->getIterableKeyType()->getConstantScalarValues() as $optValue) {
foreach ($optArrayType->getIterableKeyType()->getConstantScalarTypes() as $optType) {
$optValue = $optType->getValue();

if (!is_int($optValue)) {
$hasTypes = false;
break;
Expand All @@ -197,6 +199,7 @@ public static function selectFromArgs(
$builder->setOffsetValueType(
new ConstantIntegerType($optValue),
$optValueType,
!$optArrayType->hasOffsetValueType($optType)->yes(),
);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2476,4 +2476,28 @@ public function testClone(): void
]);
}

#[RequiresPhp('>= 8.1')]
public function testBug13862(): void
{
$this->analyse([__DIR__ . '/data/bug-13862.php'], []);
}

#[RequiresPhp('>= 8.1')]
public function testBug13862b(): void
{
$this->analyse([__DIR__ . '/data/bug-13862b.php'], []);
}

#[RequiresPhp('>= 8.1')]
public function testBug13862c(): void
{
$this->analyse([__DIR__ . '/data/bug-13862c.php'], [
[
'Parameter #2 $options of function curl_setopt_array expects array{10022?: non-empty-string}, array{}|array{10022: 123} given.',
12,
'Offset 10022 (non-empty-string) does not accept type 123.',
],
]);
}

}
66 changes: 66 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-13862.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Bug13862;

class foo
{

/**
* @param string $url
* @param string $curl_interface
* @param ?int $port
* @param ?string $post
* @return string
*/
public function getUrl(
string $url,
string $curl_interface = "",
?int $port = null,
?string $post = null
): string
{

if (empty($url))
return "";

$options = array(
CURLOPT_URL => $url,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 15,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_PORT => 443,
CURLOPT_ENCODING => "gzip, deflate",
CURLOPT_HTTPHEADER => array("Accept-Encoding: gzip, deflate")
);

if ($curl_interface != "")
$options[CURLOPT_INTERFACE] = $curl_interface;

if ($port !== null)
$options[CURLOPT_PORT] = $port;

if ($post !== null) {
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $post;
$options[CURLOPT_HTTPHEADER] = array(
"Content-Type: application/json;charset=\"UTF-8\"",
"Accept: application/json",
"Accept-Encoding: gzip, deflate",
"Content-Length: " . mb_strlen($post, "UTF-8")
);
}

$curl = curl_init();
curl_setopt_array($curl, $options);
$rc = curl_exec($curl);
if (is_bool($rc))
return "";
else
return $rc;

}

}

$foo = new foo();
$data = $foo->getUrl("https://example.tld");
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-13862b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Bug13862b;

$options = [];

if (rand()) {
$options[CURLOPT_COOKIE] = 'foo=bar';
}

$ch = curl_init();
curl_setopt_array($ch, $options);
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-13862c.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Bug13862c;

$options = [];

if (rand()) {
$options[CURLOPT_COOKIE] = 123;
}

$ch = curl_init();
curl_setopt_array($ch, $options);
Loading