Skip to content

Commit 0eb8bed

Browse files
committed
fix arrays
1 parent 039d9b7 commit 0eb8bed

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Type/ArrayType.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,23 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
371371

372372
public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
373373
{
374+
if ($this->itemType->isConstantArray()->yes() && $valueType->isConstantArray()->yes()) {
375+
$constArrays = $valueType->getConstantArrays();
376+
$newItemType = $this->itemType;
377+
foreach($constArrays as $constArray) {
378+
foreach($constArray->getKeyTypes() as $keyType) {
379+
$newItemType = $newItemType->setExistingOffsetValueType($keyType, $constArray->getOffsetValueType($keyType));
380+
}
381+
}
382+
383+
if ($newItemType !== $this->itemType) {
384+
return new self(
385+
$this->keyType,
386+
$newItemType
387+
);
388+
}
389+
}
390+
374391
return new self(
375392
$this->keyType,
376393
TypeCombinator::union($this->itemType, $valueType),

tests/PHPStan/Analyser/nsrt/bug-11846.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ function demo(): void
1313
$outerList[$id] = [];
1414
array_push($outerList[$id], []);
1515
}
16-
assertType('non-empty-array<1|2, array{}|array{array{}}>', $outerList);
16+
assertType('non-empty-array<1|2, array{array{}}>', $outerList);
1717

1818
foreach ($outerList as $key => $outerElement) {
1919
$result = false;
2020

21-
assertType('array{}|array{array{}}', $outerElement);
21+
assertType('array{array{}}', $outerElement);
2222
foreach ($outerElement as $innerElement) {
2323
$result = true;
2424
}
25-
assertType('bool', $result); // could be 'true'
25+
assertType('true', $result);
2626

2727
}
2828
}

0 commit comments

Comments
 (0)