Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 99b3d82

Browse files
authored
Improve ConstantArrayType inference
1 parent b94e179 commit 99b3d82

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

‎src/Type/ArrayType.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,22 @@ 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+
$newItemType = $this->itemType;
376+
foreach ($valueType->getConstantArrays() as $constArray) {
377+
foreach ($constArray->getKeyTypes() as $keyType) {
378+
$newItemType = $newItemType->setExistingOffsetValueType($keyType, $constArray->getOffsetValueType($keyType));
379+
}
380+
}
381+
382+
if ($newItemType !== $this->itemType) {
383+
return new self(
384+
$this->keyType,
385+
$newItemType,
386+
);
387+
}
388+
}
389+
374390
return new self(
375391
$this->keyType,
376392
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)

AltStyle によって変換されたページ (->オリジナル) /