Skip to content

Commit 0a6e2fe

Browse files
committed
Add support for any array keys
1 parent 69644ac commit 0a6e2fe

3 files changed

Lines changed: 34 additions & 41 deletions

File tree

src/TypeResolver.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
390390
{
391391
switch (strtolower($type->type->name)) {
392392
case 'array':
393-
return $this->createArray($type->genericTypes, $context);
393+
$genericTypes = array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context));
394+
395+
return new Array_(...$genericTypes);
394396

395397
case 'non-empty-array':
396398
$genericTypes = array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context));
@@ -630,33 +632,6 @@ private function resolveTypedObject(string $type, ?Context $context = null): Obj
630632
return new Object_($this->fqsenResolver->resolve($type, $context));
631633
}
632634

633-
/** @param TypeNode[] $typeNodes */
634-
private function createArray(array $typeNodes, Context $context): Array_
635-
{
636-
$types = array_reverse($this->createTypesByTypeNodes($typeNodes, $context));
637-
638-
if (isset($types[1]) === false) {
639-
return new Array_(...$types);
640-
}
641-
642-
if ($this->validArrayKeyType($types[1]) || $types[1] instanceof ArrayKey) {
643-
return new Array_(...$types);
644-
}
645-
646-
if ($types[1] instanceof Compound && $types[1]->getIterator()->count() === 2) {
647-
if ($this->validArrayKeyType($types[1]->get(0)) && $this->validArrayKeyType($types[1]->get(1))) {
648-
return new Array_(...$types);
649-
}
650-
}
651-
652-
throw new RuntimeException('An array can have only integers or strings as keys');
653-
}
654-
655-
private function validArrayKeyType(?Type $type): bool
656-
{
657-
return $type instanceof String_ || $type instanceof Integer;
658-
}
659-
660635
private function parse(TokenIterator $tokenIterator): TypeNode
661636
{
662637
try {

tests/unit/CollectionResolverTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,6 @@ public function testResolvingCollectionOfCollection(): void
211211
$this->assertInstanceOf(Float_::class, $keyType->get(2));
212212
}
213213

214-
/**
215-
* @covers ::__construct
216-
* @covers ::resolve
217-
* @covers ::createType
218-
*/
219-
public function testBadArrayCollectionKey(): void
220-
{
221-
$this->expectException(RuntimeException::class);
222-
$this->expectExceptionMessage('An array can have only integers or strings as keys');
223-
$fixture = new TypeResolver();
224-
$fixture->resolve('array<object,string>', new Context(''));
225-
}
226-
227214
/**
228215
* @covers ::__construct
229216
* @covers ::resolve

tests/unit/TypeResolverTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,37 @@ public function genericsProvider(): array
10691069
new Integer()
10701070
),
10711071
],
1072+
[
1073+
'array<key-of<Foo\\Bar::SOME_CONSTANT>, string>',
1074+
new Array_(
1075+
new String_(),
1076+
new KeyOf(new ConstExpression(new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')), 'SOME_CONSTANT'))
1077+
),
1078+
],
1079+
[
1080+
'array<value-of<Foo\\Bar::SOME_CONSTANT>, string>',
1081+
new Array_(
1082+
new String_(),
1083+
new ValueOf(new ConstExpression(new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')), 'SOME_CONSTANT'))
1084+
),
1085+
],
1086+
[
1087+
'array<Foo\\Bar::*, string>',
1088+
new Array_(
1089+
new String_(),
1090+
new ConstExpression(new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')), '*')
1091+
),
1092+
],
1093+
[
1094+
'array<self::SOME_CONSTANT_FIRST|self::SOME_CONSTANT_SECOND, string>',
1095+
new Array_(
1096+
new String_(),
1097+
new Compound([
1098+
new ConstExpression(new Self_(), 'SOME_CONSTANT_FIRST'),
1099+
new ConstExpression(new Self_(), 'SOME_CONSTANT_SECOND'),
1100+
])
1101+
),
1102+
],
10721103
[
10731104
'array<string|int, Foo\\Bar>',
10741105
new Array_(

0 commit comments

Comments
 (0)