Skip to content

Commit dc9e2e9

Browse files
staabmondrejmirtes
authored andcommitted
fix different constant arrays counts
1 parent 51275eb commit dc9e2e9

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5151

5252
$constantKeysArrays = $keysParamType->getConstantArrays();
5353
$constantValuesArrays = $valuesParamType->getConstantArrays();
54-
if ($constantKeysArrays !== [] && $constantValuesArrays !== []) {
55-
if (count($constantKeysArrays) !== count($constantValuesArrays)) {
56-
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
57-
return new NeverType();
58-
}
59-
return new ConstantBooleanType(false);
60-
}
61-
54+
if (
55+
$constantKeysArrays !== []
56+
&& $constantValuesArrays !== []
57+
&& count($constantKeysArrays) === count($constantValuesArrays)
58+
) {
6259
$results = [];
6360
foreach ($constantKeysArrays as $k => $constantKeysArray) {
6461
$constantValueArrays = $constantValuesArrays[$k];

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ function withUnionConstArraysDifferentKeyValueCount(): void
9696
assertType("*NEVER*", array_combine($a, $b));
9797
}
9898

99+
function withUnionConstArraysDifferentArraysCount(): void
100+
{
101+
if (rand(0, 1)) {
102+
$a = [1];
103+
$b = ['avocado'];
104+
} else {
105+
$a = ["2", "3"];
106+
if (rand(0, 1)) {
107+
$b = ['apple', 'banana'];
108+
} else {
109+
$b = ['banana', 'pear'];
110+
}
111+
}
112+
113+
assertType("non-empty-array<1|'2'|'3', 'apple'|'avocado'|'banana'|'pear'>", array_combine($a, $b));
114+
}
115+
99116
/**
100117
* @param non-empty-array<int, 'foo'|'bar'|'baz'> $a
101118
* @param non-empty-array<int, 'apple'|'avocado'|'banana'> $b

0 commit comments

Comments
 (0)