Skip to content

Commit dfc078e

Browse files
authored
Merge pull request #188 from phpDocumentor/bugfix/compound_array_key
Fix compound array key support
2 parents 1534aea + b36cd54 commit dfc078e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/TypeResolver.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,24 @@ private function createArray(array $typeNodes, Context $context): Array_
551551
return new Array_(...$types);
552552
}
553553

554-
if ($types[1] instanceof String_ || $types[1] instanceof Integer || $types[1] instanceof ArrayKey) {
554+
if ($this->validArrayKeyType($types[1]) || $types[1] instanceof ArrayKey) {
555555
return new Array_(...$types);
556556
}
557557

558+
if ($types[1] instanceof Compound && $types[1]->getIterator()->count() === 2) {
559+
if ($this->validArrayKeyType($types[1]->get(0)) && $this->validArrayKeyType($types[1]->get(1))) {
560+
return new Array_(...$types);
561+
}
562+
}
563+
558564
throw new RuntimeException('An array can have only integers or strings as keys');
559565
}
560566

567+
private function validArrayKeyType(?Type $type): bool
568+
{
569+
return $type instanceof String_ || $type instanceof Integer;
570+
}
571+
561572
private function parse(TokenIterator $tokenIterator): TypeNode
562573
{
563574
try {

tests/unit/TypeResolverTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,18 @@ public function genericsProvider(): array
960960
new Integer()
961961
),
962962
],
963+
[
964+
'array<string|int, Foo\\Bar>',
965+
new Array_(
966+
new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')),
967+
new Compound(
968+
[
969+
new String_(),
970+
new Integer(),
971+
]
972+
)
973+
),
974+
],
963975
[
964976
'Collection<array-key, int>[]',
965977
new Array_(

0 commit comments

Comments
 (0)