Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
{
switch (strtolower($type->type->name)) {
case 'array':
return $this->createArray($type->genericTypes, $context);
$genericTypes = array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context));

return new Array_(...$genericTypes);

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

/** @param TypeNode[] $typeNodes */
private function createArray(array $typeNodes, Context $context): Array_
{
$types = array_reverse($this->createTypesByTypeNodes($typeNodes, $context));

if (isset($types[1]) === false) {
return new Array_(...$types);
}

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
13 changes: 0 additions & 13 deletions tests/unit/CollectionResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,6 @@ public function testResolvingCollectionOfCollection(): void
$this->assertEquals(new Object_(new Fqsen('\\DateTime')), $nestedGenericTypes[0]);
}

/**
* @covers ::__construct
* @covers ::resolve
* @covers ::createType
*/
public function testBadArrayCollectionKey(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('An array can have only integers or strings as keys');
$fixture = new TypeResolver();
$fixture->resolve('array<object,string>', new Context(''));
}

/**
* @covers ::__construct
* @covers ::resolve
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,43 @@ public function genericsProvider(): array
new Integer()
),
],
[
'array<key-of<Foo\\Bar::SOME_CONSTANT>, string>',
new Array_(
new String_(),
new KeyOf(new ConstExpression(
new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')),
'SOME_CONSTANT'
))
),
],
[
'array<value-of<Foo\\Bar::SOME_CONSTANT>, string>',
new Array_(
new String_(),
new ValueOf(new ConstExpression(
new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')),
'SOME_CONSTANT'
))
),
],
[
'array<Foo\\Bar::*, string>',
new Array_(
new String_(),
new ConstExpression(new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')), '*')
),
],
[
'array<self::SOME_CONSTANT_FIRST|self::SOME_CONSTANT_SECOND, string>',
new Array_(
new String_(),
new Compound([
new ConstExpression(new Self_(), 'SOME_CONSTANT_FIRST'),
new ConstExpression(new Self_(), 'SOME_CONSTANT_SECOND'),
])
),
],
[
'array<string|int, Foo\\Bar>',
new Array_(
Expand Down