Skip to content

Commit 26e949b

Browse files
committed
Fix ConstantArrayType::hasOffsetValueType() for offset union type where all types are valid
1 parent d4bf4f5 commit 26e949b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,14 @@ public function findTypeAndMethodNames(): array
610610
public function hasOffsetValueType(Type $offsetType): TrinaryLogic
611611
{
612612
$offsetType = $offsetType->toArrayKey();
613+
if ($offsetType instanceof UnionType) {
614+
$results = [];
615+
foreach ($offsetType->getTypes() as $innerType) {
616+
$results[] = $this->hasOffsetValueType($innerType);
617+
}
618+
619+
return TrinaryLogic::extremeIdentity(...$results);
620+
}
613621

614622
$result = TrinaryLogic::createNo();
615623
foreach ($this->keyTypes as $i => $keyType) {

tests/PHPStan/Rules/Arrays/data/report-possibly-nonexistent-array-offset.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ public function testConstantArray2(array $a): void
3939
echo $a[0];
4040
}
4141
}
42+
43+
/**
44+
* @param array{0: '9', A: 'Z', a: 'z'} $a
45+
* @param '0'|'A'|'a' $dim
46+
*/
47+
public function testDimUnion(array $a, string $dim): void
48+
{
49+
echo $a[$dim];
50+
}
51+
4252
}

0 commit comments

Comments
 (0)