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
17 changes: 12 additions & 5 deletions src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use function array_key_exists;
use function count;
use function implode;
use function in_array;
use function is_int;
use function is_string;
use function max;
Expand Down Expand Up @@ -128,30 +129,32 @@ public function check(
if ($arg->unpack) {
$arrays = $type->getConstantArrays();
if (count($arrays) > 0) {
$minKeys = null;
$maxKeys = null;
foreach ($arrays as $array) {
$countType = $array->getArraySize();
if ($countType instanceof ConstantIntegerType) {
$keysCount = $countType->getValue();
} elseif ($countType instanceof IntegerRangeType) {
$keysCount = $countType->getMin();
$keysCount = $countType->getMax();
if ($keysCount === null) {
throw new ShouldNotHappenException();
}
} else {
throw new ShouldNotHappenException();
}
if ($minKeys !== null && $keysCount >= $minKeys) {
if ($maxKeys !== null && $keysCount >= $maxKeys) {
continue;
}

$minKeys = $keysCount;
$maxKeys = $keysCount;
}

for ($j = 0; $j < $minKeys; $j++) {
for ($j = 0; $j < $maxKeys; $j++) {
$types = [];
$commonKey = null;
$isOptionalKey = false;
foreach ($arrays as $constantArray) {
$isOptionalKey = in_array($j, $constantArray->getOptionalKeys(), true);
$types[] = $constantArray->getValueTypes()[$j];
$keyType = $constantArray->getKeyTypes()[$j];
if ($commonKey === null) {
Expand All @@ -165,6 +168,10 @@ public function check(
$keyArgumentName = $commonKey;
$hasNamedArguments = true;
}
if ($isOptionalKey) {
continue;
}

$arguments[] = [
$arg->value,
TypeCombinator::union(...$types),
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,9 @@ public function testBug10248(): void
$this->analyse([__DIR__ . '/data/bug-10248.php'], []);
}

public function testBug11815(): void
{
$this->analyse([__DIR__ . '/data/bug-11815.php'], []);
}

}
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-11815.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php // lint >= 8.2

declare(strict_types = 1);

class Dimensions
{
public function __construct(
public int $width,
public int $height,
) {
}
}

class StoreProcessorResult
{
public function __construct(
public string $path,
public string $mimetype,
public Dimensions $dimensions,
public int $filesize,
public true|null $identical = null,
) {
}
}

/**
* @return array{path: string, identical?: true}
*/
function getPath(): array
{
$data = ['path' => 'some/path'];
if ((bool)rand(0, 1)) {
$data['identical'] = true;
}
return $data;
}

$data = getPath();
$data['dimensions'] = new Dimensions(100, 100);
$data['mimetype'] = 'image/png';
$data['filesize'] = 123456;

$dto = new StoreProcessorResult(...$data);
Loading