Skip to content

Commit 268309d

Browse files
authored
Merge branch refs/heads/1.12.x into 2.0.x
2 parents 676c934 + c4ba434 commit 268309d

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

Diff for: .github/workflows/e2e-tests.yml

+3
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ jobs:
279279
cd e2e/discussion-11362
280280
composer install
281281
../../bin/phpstan
282+
- script: |
283+
cd e2e/bug-11819
284+
../../bin/phpstan
282285
283286
steps:
284287
- name: "Checkout"

Diff for: e2e/bug-11819/phpstan.neon

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- test.php

Diff for: e2e/bug-11819/test.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types = 1);
2+
3+
const TYPES = [1, 2, 3];
4+
5+
$types = array_combine(TYPES, array_fill(0, \count(TYPES), false));
6+
7+
$test = rand(1, 4);
8+
9+
if (isset($types[$test])) {
10+
$types[$test] = true;
11+
}

Diff for: src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Type\Accessory\NonEmptyArrayType;
1111
use PHPStan\Type\ArrayType;
1212
use PHPStan\Type\Constant\ConstantArrayType;
13+
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1314
use PHPStan\Type\Constant\ConstantBooleanType;
1415
use PHPStan\Type\Constant\ConstantIntegerType;
1516
use PHPStan\Type\Constant\ConstantStringType;
@@ -62,11 +63,13 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
6263

6364
$keyTypes = $this->sanitizeConstantArrayKeyTypes($keyTypes);
6465
if ($keyTypes !== null) {
65-
return new ConstantArrayType(
66-
$keyTypes,
67-
$valueTypes,
68-
$keysParamType->getNextAutoIndexes(),
69-
);
66+
$builder = ConstantArrayTypeBuilder::createEmpty();
67+
foreach ($keyTypes as $i => $keyType) {
68+
$valueType = $valueTypes[$i];
69+
$builder->setOffsetValueType($keyType, $valueType);
70+
}
71+
72+
return $builder->getArray();
7073
}
7174
}
7275

Diff for: tests/PHPStan/Analyser/nsrt/array-combine-php8.php

+8
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ function withDifferentNumberOfElements(): void
8383
{
8484
assertType('*NEVER*', array_combine(['foo'], ['bar', 'baz']));
8585
}
86+
87+
function bug11819(): void
88+
{
89+
$keys = [1, 2, 3];
90+
$types = array_combine($keys, array_fill(0, \count($keys), false));
91+
$types[] = 'foo';
92+
assertType('array{1: false, 2: false, 3: false, 4: \'foo\'}', $types);
93+
}

0 commit comments

Comments
 (0)