Skip to content

Commit 674109b

Browse files
staabmondrejmirtes
authored andcommitted
prevent crash
1 parent dc9e2e9 commit 674109b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Type\Type;
2121
use PHPStan\Type\TypeCombinator;
2222
use PHPStan\Type\UnionType;
23+
use function array_key_exists;
2324
use function count;
2425
use function is_int;
2526
use function is_string;
@@ -77,6 +78,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
7778

7879
$builder = ConstantArrayTypeBuilder::createEmpty();
7980
foreach ($keyTypes as $i => $keyType) {
81+
if (!array_key_exists($i, $valueTypes)) {
82+
$results = [];
83+
break 2;
84+
}
8085
$valueType = $valueTypes[$i];
8186
$builder->setOffsetValueType($keyType, $valueType);
8287
}

tests/PHPStan/Analyser/nsrt/array-combine-php8.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ function withUnionConstArraysDifferentArraysCount(): void
113113
assertType("non-empty-array<1|'2'|'3', 'apple'|'avocado'|'banana'|'pear'>", array_combine($a, $b));
114114
}
115115

116+
function withUnionConstArraysAndInvalidKeys(bool $bool): void
117+
{
118+
if (rand(0, 1)) {
119+
$a = [$bool];
120+
$b = ['avocado'];
121+
} else {
122+
$a = ["2", $bool];
123+
$b = ['apple', 'banana'];
124+
}
125+
126+
assertType("non-empty-array<''|'1'|'2', 'apple'|'avocado'|'banana'>", array_combine($a, $b));
127+
}
128+
116129
/**
117130
* @param non-empty-array<int, 'foo'|'bar'|'baz'> $a
118131
* @param non-empty-array<int, 'apple'|'avocado'|'banana'> $b

0 commit comments

Comments
 (0)