Skip to content

Commit 1cba987

Browse files
herndlmondrejmirtes
authored andcommitted
Fix Array_ expr type resolving with unpacked constant array items
1 parent 1ac08a1 commit 1cba987

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,9 @@ public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback): Type
452452

453453
foreach ($valueType->getValueTypes() as $i => $innerValueType) {
454454
if ($hasStringKey && $this->phpVersion->supportsArrayUnpackingWithStringKeys()) {
455-
$arrayBuilder->setOffsetValueType($valueType->getKeyTypes()[$i], $innerValueType);
455+
$arrayBuilder->setOffsetValueType($valueType->getKeyTypes()[$i], $innerValueType, $valueType->isOptionalKey($i));
456456
} else {
457-
$arrayBuilder->setOffsetValueType(null, $innerValueType);
457+
$arrayBuilder->setOffsetValueType(null, $innerValueType, $valueType->isOptionalKey($i));
458458
}
459459
}
460460
} else {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ public function dataFileAsserts(): iterable
970970
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-7469.php');
971971
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Variables/data/bug-3391.php');
972972
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6901.php');
973+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7776.php');
973974
}
974975

975976
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7776;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param array{page?: int, search?: string} $settings
9+
*/
10+
function test(array $settings = []): bool {
11+
$copy = [...$settings];
12+
assertType('array{page?: int, search?: string}', $copy);
13+
assertType('array{page?: int, search?: string}', $settings);
14+
return isset($copy['search']);
15+
}
16+
17+
test();

0 commit comments

Comments
 (0)