Skip to content

Commit bb091ce

Browse files
Merge branch '7.4' into 8.0
* 7.4: [TypeInfo] Simple array should be array type Handle signals on text input [TwigBridge] Fix form constraint [Runtime] Reuse the already created Request object when the app needs it as argument returns a kernel [Config] Fix array shape generation for backed enums [Config] Define `TreeBuilder` default generic type Update validators.el.xlf Fix MoneyType: add missing step attribute when html5=true [JsonStreamer] fix invalid json output for list of self [Console] Preserve `--help` option when a command is not found [FrameworkBundle] Fix using `FailedMessages*Command` with `SigningSerializer` [Lock] Fix unserializing already serialized Key payloads [HttpClient] CachingHttpClient must run after UriTemplate and Scoping Only register PhpConfigReferenceDumpPass in dev env with debug flag enabled [Messenger] Fix PHP 8.5 deprecation for pgsqlGetNotify() in PostgreSQL transport chore: PHP CS Fixer - do not use deprecated sets in config verify spanish translations with state needs-review-translation [Security] Fix OIDC discovery when using multiple HttpClient instances [DependencyInjection] Allow manual bindings on parameters with #[Target]
2 parents 9de828e + ac5ab66 commit bb091ce

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
@@ -70,7 +70,16 @@ public static function resolveDataProvider(): iterable
7070
yield [Type::callable(), 'callable(string, int): mixed'];
7171

7272
// array
73-
yield [Type::list(Type::bool()), 'bool[]'];
73+
yield [Type::array(Type::bool()), 'bool[]'];
74+
yield [Type::array(Type::bool()), 'array<bool>'];
75+
yield [Type::array(Type::bool(), Type::int()), 'array<int, bool>'];
76+
yield [Type::array(Type::bool(), Type::arrayKey()), 'array<array-key, bool>'];
77+
yield [Type::array(Type::bool(), Type::arrayKey()), 'array<int|string, bool>'];
78+
yield [Type::array(Type::bool(), Type::arrayKey()), 'non-empty-array<int|string, bool>'];
79+
80+
// list
81+
yield [Type::list(Type::bool()), 'list<bool>'];
82+
yield [Type::list(Type::bool()), 'non-empty-list<bool>'];
7483

7584
// array shape
7685
yield [Type::arrayShape(['foo' => Type::true(), 1 => Type::false()]), 'array{foo: true, 1: false}'];
@@ -257,4 +266,28 @@ public function testCannotResolveValueOfInvalidType()
257266
$this->expectException(UnsupportedException::class);
258267
$this->resolver->resolve('value-of<int>');
259268
}
269+
270+
public function testCannotResolveListWithKeyType()
271+
{
272+
$this->expectException(UnsupportedException::class);
273+
$this->resolver->resolve('list<int, string>');
274+
}
275+
276+
public function testCannotResolveInvalidNonEmptyListWithKeyType()
277+
{
278+
$this->expectException(UnsupportedException::class);
279+
$this->resolver->resolve('non-empty-list<int, string>');
280+
}
281+
282+
public function testCannotResolveInvalidArrayKeyType()
283+
{
284+
$this->expectException(InvalidArgumentException::class);
285+
$this->resolver->resolve('array<mixed, string>');
286+
}
287+
288+
public function testCannotResolveInvalidUnionArrayKeyType()
289+
{
290+
$this->expectException(InvalidArgumentException::class);
291+
$this->resolver->resolve('array<int|mixed, string>');
292+
}
260293
}

Type/CollectionType.php

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

TypeResolver/StringTypeResolver.php

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

108108
if ($node instanceof ArrayTypeNode) {
109-
return Type::list($this->getTypeFromNode($node->type, $typeContext));
109+
return Type::array($this->getTypeFromNode($node->type, $typeContext));
110110
}
111111

112112
if ($node instanceof ArrayShapeNode) {
@@ -269,6 +269,10 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
269269
if (1 === \count($variableTypes)) {
270270
return new CollectionType(Type::generic($type, $keyType, $variableTypes[0]), $asList);
271271
} elseif (2 === \count($variableTypes)) {
272+
if ($asList) {
273+
throw new \DomainException(\sprintf('"%s" type cannot have a key type defined.', $node->type));
274+
}
275+
272276
return Type::collection($type, $variableTypes[1], $variableTypes[0], $asList);
273277
}
274278
}

0 commit comments

Comments
 (0)