Skip to content

Commit 3ba8784

Browse files
phpstan-botclaude
authored andcommitted
Skip keys absent from the other array instead of falling back to getIterableValueType()
When intersecting two ConstantArrayTypes and hasOffsetValueType() returns no for a key, that key cannot exist in the intersection. Previously it fell back to getIterableValueType() which could leak unrelated value types. Now those keys are simply skipped via continue. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7af188e commit 3ba8784

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/Type/TypeCombinator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,12 +1516,12 @@ public static function intersect(Type ...$types): Type
15161516
$valueTypes = $types[$i]->getValueTypes();
15171517
foreach ($types[$i]->getKeyTypes() as $k => $keyType) {
15181518
$hasOffset = $types[$j]->hasOffsetValueType($keyType);
1519-
$otherValueType = !$hasOffset->no()
1520-
? $types[$j]->getOffsetValueType($keyType)
1521-
: $types[$j]->getIterableValueType();
1519+
if ($hasOffset->no()) {
1520+
continue;
1521+
}
15221522
$newArray->setOffsetValueType(
15231523
self::intersect($keyType, $types[$j]->getIterableKeyType()),
1524-
self::intersect($valueTypes[$k], $otherValueType),
1524+
self::intersect($valueTypes[$k], $types[$j]->getOffsetValueType($keyType)),
15251525
$types[$i]->isOptionalKey($k) && !$hasOffset->yes(),
15261526
);
15271527
}
@@ -1536,12 +1536,12 @@ public static function intersect(Type ...$types): Type
15361536
$valueTypes = $types[$j]->getValueTypes();
15371537
foreach ($types[$j]->getKeyTypes() as $k => $keyType) {
15381538
$hasOffset = $types[$i]->hasOffsetValueType($keyType);
1539-
$otherValueType = !$hasOffset->no()
1540-
? $types[$i]->getOffsetValueType($keyType)
1541-
: $types[$i]->getIterableValueType();
1539+
if ($hasOffset->no()) {
1540+
continue;
1541+
}
15421542
$newArray->setOffsetValueType(
15431543
self::intersect($keyType, $types[$i]->getIterableKeyType()),
1544-
self::intersect($valueTypes[$k], $otherValueType),
1544+
self::intersect($valueTypes[$k], $types[$i]->getOffsetValueType($keyType)),
15451545
$types[$j]->isOptionalKey($k) && !$hasOffset->yes(),
15461546
);
15471547
}

0 commit comments

Comments
 (0)