Skip to content

Commit 034c3c2

Browse files
authored
Add support for any array keys (#236)
* Add support for any array keys * Fix CS
1 parent e2de53a commit 034c3c2

File tree

3 files changed

+40
-41
lines changed

3 files changed

+40
-41
lines changed

src/TypeResolver.php

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

396398
case 'non-empty-array':
397399
$genericTypes = array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context));
@@ -612,33 +614,6 @@ private function resolveTypedObject(string $type, ?Context $context = null): Obj
612614
return new Object_($this->fqsenResolver->resolve($type, $context));
613615
}
614616

615-
/** @param TypeNode[] $typeNodes */
616-
private function createArray(array $typeNodes, Context $context): Array_
617-
{
618-
$types = array_reverse($this->createTypesByTypeNodes($typeNodes, $context));
619-
620-
if (isset($types[1]) === false) {
621-
return new Array_(...$types);
622-
}
623-
624-
if ($this->validArrayKeyType($types[1]) || $types[1] instanceof ArrayKey) {
625-
return new Array_(...$types);
626-
}
627-
628-
if ($types[1] instanceof Compound && $types[1]->getIterator()->count() === 2) {
629-
if ($this->validArrayKeyType($types[1]->get(0)) && $this->validArrayKeyType($types[1]->get(1))) {
630-
return new Array_(...$types);
631-
}
632-
}
633-
634-
throw new RuntimeException('An array can have only integers or strings as keys');
635-
}
636-
637-
private function validArrayKeyType(?Type $type): bool
638-
{
639-
return $type instanceof String_ || $type instanceof Integer;
640-
}
641-
642617
private function parse(TokenIterator $tokenIterator): TypeNode
643618
{
644619
try {

tests/unit/CollectionResolverTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,6 @@ public function testResolvingCollectionOfCollection(): void
194194
$this->assertEquals(new Object_(new Fqsen('\\DateTime')), $nestedGenericTypes[0]);
195195
}
196196

197-
/**
198-
* @covers ::__construct
199-
* @covers ::resolve
200-
* @covers ::createType
201-
*/
202-
public function testBadArrayCollectionKey(): void
203-
{
204-
$this->expectException(RuntimeException::class);
205-
$this->expectExceptionMessage('An array can have only integers or strings as keys');
206-
$fixture = new TypeResolver();
207-
$fixture->resolve('array<object,string>', new Context(''));
208-
}
209-
210197
/**
211198
* @covers ::__construct
212199
* @covers ::resolve

tests/unit/TypeResolverTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,43 @@ public function genericsProvider(): array
997997
new Integer()
998998
),
999999
],
1000+
[
1001+
'array<key-of<Foo\\Bar::SOME_CONSTANT>, string>',
1002+
new Array_(
1003+
new String_(),
1004+
new KeyOf(new ConstExpression(
1005+
new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')),
1006+
'SOME_CONSTANT'
1007+
))
1008+
),
1009+
],
1010+
[
1011+
'array<value-of<Foo\\Bar::SOME_CONSTANT>, string>',
1012+
new Array_(
1013+
new String_(),
1014+
new ValueOf(new ConstExpression(
1015+
new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')),
1016+
'SOME_CONSTANT'
1017+
))
1018+
),
1019+
],
1020+
[
1021+
'array<Foo\\Bar::*, string>',
1022+
new Array_(
1023+
new String_(),
1024+
new ConstExpression(new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')), '*')
1025+
),
1026+
],
1027+
[
1028+
'array<self::SOME_CONSTANT_FIRST|self::SOME_CONSTANT_SECOND, string>',
1029+
new Array_(
1030+
new String_(),
1031+
new Compound([
1032+
new ConstExpression(new Self_(), 'SOME_CONSTANT_FIRST'),
1033+
new ConstExpression(new Self_(), 'SOME_CONSTANT_SECOND'),
1034+
])
1035+
),
1036+
],
10001037
[
10011038
'array<string|int, Foo\\Bar>',
10021039
new Array_(

0 commit comments

Comments
 (0)