Skip to content

Commit

Permalink
Merge pull request #188 from phpDocumentor/bugfix/compound_array_key
Browse files Browse the repository at this point in the history
Fix compound array key support
  • Loading branch information
jaapio authored Mar 27, 2023
2 parents 1534aea + b36cd54 commit dfc078e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,24 @@ private function createArray(array $typeNodes, Context $context): Array_
return new Array_(...$types);
}

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

if ($types[1] instanceof Compound && $types[1]->getIterator()->count() === 2) {
if ($this->validArrayKeyType($types[1]->get(0)) && $this->validArrayKeyType($types[1]->get(1))) {
return new Array_(...$types);
}
}

throw new RuntimeException('An array can have only integers or strings as keys');
}

private function validArrayKeyType(?Type $type): bool
{
return $type instanceof String_ || $type instanceof Integer;
}

private function parse(TokenIterator $tokenIterator): TypeNode
{
try {
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,18 @@ public function genericsProvider(): array
new Integer()
),
],
[
'array<string|int, Foo\\Bar>',
new Array_(
new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')),
new Compound(
[
new String_(),
new Integer(),
]
)
),
],
[
'Collection<array-key, int>[]',
new Array_(
Expand Down

0 comments on commit dfc078e

Please sign in to comment.