Skip to content

Commit 126b60f

Browse files
matijn-madenicolas-grekas
authored andcommitted
[TypeInfo] Simple array should be array type
1 parent 8b36f41 commit 126b60f

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

Tests/TypeResolver/StringTypeResolverTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,16 @@ public static function resolveDataProvider(): iterable
7373
yield [Type::callable(), 'callable(string, int): mixed'];
7474

7575
// array
76-
yield [Type::list(Type::bool()), 'bool[]'];
76+
yield [Type::array(Type::bool()), 'bool[]'];
77+
yield [Type::array(Type::bool()), 'array<bool>'];
78+
yield [Type::array(Type::bool(), Type::int()), 'array<int, bool>'];
79+
yield [Type::array(Type::bool(), Type::arrayKey()), 'array<array-key, bool>'];
80+
yield [Type::array(Type::bool(), Type::arrayKey()), 'array<int|string, bool>'];
81+
yield [Type::array(Type::bool(), Type::arrayKey()), 'non-empty-array<int|string, bool>'];
82+
83+
// list
84+
yield [Type::list(Type::bool()), 'list<bool>'];
85+
yield [Type::list(Type::bool()), 'non-empty-list<bool>'];
7786

7887
// array shape
7988
yield [Type::arrayShape(['foo' => Type::true(), 1 => Type::false()]), 'array{foo: true, 1: false}'];
@@ -253,4 +262,28 @@ public function testCannotResolveValueOfInvalidType()
253262
$this->expectException(UnsupportedException::class);
254263
$this->resolver->resolve('value-of<int>');
255264
}
265+
266+
public function testCannotResolveListWithKeyType()
267+
{
268+
$this->expectException(UnsupportedException::class);
269+
$this->resolver->resolve('list<int, string>');
270+
}
271+
272+
public function testCannotResolveInvalidNonEmptyListWithKeyType()
273+
{
274+
$this->expectException(UnsupportedException::class);
275+
$this->resolver->resolve('non-empty-list<int, string>');
276+
}
277+
278+
public function testCannotResolveInvalidArrayKeyType()
279+
{
280+
$this->expectException(InvalidArgumentException::class);
281+
$this->resolver->resolve('array<mixed, string>');
282+
}
283+
284+
public function testCannotResolveInvalidUnionArrayKeyType()
285+
{
286+
$this->expectException(InvalidArgumentException::class);
287+
$this->resolver->resolve('array<int|mixed, string>');
288+
}
256289
}

Type/CollectionType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public function __construct(
4949
if (!$keyType instanceof BuiltinType || TypeIdentifier::INT !== $keyType->getTypeIdentifier()) {
5050
throw new InvalidArgumentException(\sprintf('"%s" is not a valid list key type.', (string) $keyType));
5151
}
52+
} elseif ($type instanceof GenericType && $type->getWrappedType() instanceof BuiltinType && TypeIdentifier::ARRAY === $type->getWrappedType()->getTypeIdentifier()) {
53+
$keyType = $this->getCollectionKeyType();
54+
55+
$keyTypes = $keyType instanceof UnionType ? $keyType->getTypes() : [$keyType];
56+
57+
foreach ($keyTypes as $type) {
58+
if (!$type instanceof BuiltinType || !\in_array($type->getTypeIdentifier(), [TypeIdentifier::INT, TypeIdentifier::STRING], true)) {
59+
throw new InvalidArgumentException(\sprintf('"%s" is not a valid array key type.', (string) $keyType));
60+
}
61+
}
5262
}
5363
}
5464

TypeResolver/StringTypeResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
100100
}
101101

102102
if ($node instanceof ArrayTypeNode) {
103-
return Type::list($this->getTypeFromNode($node->type, $typeContext));
103+
return Type::array($this->getTypeFromNode($node->type, $typeContext));
104104
}
105105

106106
if ($node instanceof ArrayShapeNode) {
@@ -263,6 +263,10 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
263263
if (1 === \count($variableTypes)) {
264264
return new CollectionType(Type::generic($type, $keyType, $variableTypes[0]), $asList);
265265
} elseif (2 === \count($variableTypes)) {
266+
if ($asList) {
267+
throw new \DomainException(\sprintf('"%s" type cannot have a key type defined.', $node->type));
268+
}
269+
266270
return Type::collection($type, $variableTypes[1], $variableTypes[0], $asList);
267271
}
268272
}

0 commit comments

Comments
 (0)