8000 add TypeFactoryTrait::arrayKey() · symfony/symfony@8b4d5a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b4d5a2

Browse files
committed
add TypeFactoryTrait::arrayKey()
1 parent 091ae5b commit 8b4d5a2

File tree

9 files changed

+23
-13
lines changed

9 files changed

+23
-13
lines changed

src/Symfony/Component/TypeInfo/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CHANGELOG
55
---
66

77
* Add `Type::accepts()` method
8-
* Add `TypeFactoryTrait::fromValue()` method
8+
* Add the `TypeFactoryTrait::fromValue()`, `TypeFactoryTrait::arrayShape()`, and `TypeFactoryTrait::arrayKey()` methods
99
* Deprecate constructing a `CollectionType` instance as a list that is not an array
1010
* Deprecate the third `$asList` argument of `TypeFactoryTrait::iterable()`, use `TypeFactoryTrait::list()` instead
1111
* Add type alias support in `TypeContext` and `StringTypeResolver`

src/Symfony/Component/TypeInfo/Tests/Type/ArrayShapeTypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testGetCollectionKeyType()
5959
1 => ['type' => Type::bool(), 'optional' => false],
6060
'foo' => ['type' => Type::bool(), 'optional' => false],
6161
]);
62-
$this->assertEquals(Type::union(Type::int(), Type::string()), $type->getCollectionKeyType());
62+
$this->assertEquals(Type::arrayKey(), $type->getCollectionKeyType());
6363
}
6464

6565
public function testGetCollectionValueType()
@@ -134,7 +134,7 @@ public function testToString()
134134

135135
$type = new ArrayShapeType(
136136
shape: ['foo' => ['type' => Type::bool()]],
137-
extraKeyType: Type::union(Type::int(), Type::string()),
137+
extraKeyType: Type::arrayKey(),
138138
extraValueType: Type::mixed(),
139139
);
140140
$this->assertSame("array{'foo': bool, ...}", (string) $type);

src/Symfony/Component/TypeInfo/Tests/Type/CollectionTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testIsList()
5050
public function testGetCollectionKeyType()
5151
{
5252
$type = new CollectionType(Type::builtin(TypeIdentifier::ARRAY));
53-
$this->assertEquals(Type::union(Type::int(), Type::string()), $type->getCollectionKeyType());
53+
$this->assertEquals(Type::arrayKey(), $type->getCollectionKeyType());
5454

5555
$type = new CollectionType(Type::generic(Type::builtin(TypeIdentifier::ARRAY), Type::bool()));
5656
$this->assertEquals(Type::int(), $type->getCollectionKeyType());

src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function testCreateArrayShape()
212212
$this->assertEquals(new ArrayShapeType(['foo' => ['type' => Type::bool(), 'optional' => false]]), Type::arrayShape(['foo' => Type::bool()]));
213213
$this->assertEquals(new ArrayShapeType(
214214
shape: ['foo' => ['type' => Type::bool(), 'optional' => false]],
215-
extraKeyType: Type::union(Type::int(), Type::string()),
215+
extraKeyType: Type::arrayKey(),
216216
extraValueType: Type::mixed(),
217217
), Type::arrayShape(['foo' => Type::bool()], sealed: false));
218218
$this->assertEquals(new ArrayShapeType(
@@ -222,6 +222,11 @@ public function testCreateArrayShape()
222222
), Type::arrayShape(['foo' => Type::bool()], extraKeyType: Type::string(), extraValueType: Type::bool()));
223223
}
224224

225+
public function testCreateArrayKey()
226+
{
227+
$this->assertEquals(new UnionType(Type::int(), Type::string()), Type::arrayKey());
228+
}
229+
225230
/**
226231
* @dataProvider createFromValueProvider
227232
*/
@@ -275,7 +280,7 @@ public function offsetUnset(mixed $offset): void
275280
yield [Type::dict(Type::bool()), ['a' => true, 'b' => false]];
276281
yield [Type::array(Type::string()), [1 => 'foo', 'bar' => 'baz']];
277282
yield [Type::array(Type::nullable(Type::bool()), Type::int()), [1 => true, 2 => null, 3 => false]];
278-
yield [Type::collection(Type::object(\ArrayIterator::class), Type::mixed(), Type::union(Type::int(), Type::string())), new \ArrayIterator()];
283+
yield [Type::collection(Type::object(\ArrayIterator::class), Type::mixed(), Type::arrayKey()), new \ArrayIterator()];
279284
yield [Type::collection(Type::object(\Generator::class), Type::string(), Type::int()), (fn (): iterable => yield 'string')()];
280285
yield [Type::collection(Type::object($arrayAccess::class)), $arrayAccess];
281286
}

src/Symfony/Component/TypeInfo/Tests/TypeResolver/StringTypeResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static function resolveDataProvider(): iterable
137137
yield [Type::never(), 'never-return'];
138138
yield [Type::never(), 'never-returns'];
139139
yield [Type::never(), 'no-return'];
140-
yield [Type::union(Type::int(), Type::string()), 'array-key'];
140+
yield [Type::arrayKey(), 'array-key'];
141141
yield [Type::union(Type::int(), Type::float(), Type::string(), Type::bool()), 'scalar'];
142142
yield [Type::union(Type::int(), Type::float()), 'number'];
143143
yield [Type::union(Type::int(), Type::float(), Type::string()), 'numeric'];

src/Symfony/Component/TypeInfo/Type/ArrayShapeType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(
4949
$keyTypes = array_values(array_unique($keyTypes));
5050
$keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0];
5151
} else {
52-
$keyType = Type::union(Type::int(), Type::string());
52+
$keyType = Type::arrayKey();
5353
}
5454

5555
$valueType = $valueTypes ? CollectionType::mergeCollectionValueTypes($valueTypes) : Type::mixed();

src/Symfony/Component/TypeInfo/Type/CollectionType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function isList(): bool
117117

118118
public function getCollectionKeyType(): Type
119119
{
120-
$defaultCollectionKeyType = self::union(self::int(), self::string());
120+
$defaultCollectionKeyType = self::arrayKey();
121121

122122
if ($this->type instanceof GenericType) {
123123
return match (\count($this->type->getVariableTypes())) {

src/Symfony/Component/TypeInfo/TypeFactoryTrait.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static function never(): BuiltinType
153153
public static function collection(BuiltinType|ObjectType|GenericType $type, ?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
154154
{
155155
if (!$type instanceof GenericType && (null !== $value || null !== $key)) {
156-
$type = self::generic($type, $key ?? self::union(self::int(), self::string()), $value ?? self::mixed());
156+
$type = self::generic($type, $key ?? self::arrayKey(), $value ?? self::mixed());
157157
}
158158

159159
return new CollectionType($type, $asList);
@@ -210,12 +210,17 @@ public static function arrayShape(array $shape, bool $sealed = true, ?Type $extr
210210
$sealed = false;
211211
}
212212

213-
$extraKeyType ??= !$sealed ? Type::union(Type::int(), Type::string()) : null;
213+
$extraKeyType ??= !$sealed ? Type::arrayKey() : null;
214214
$extraValueType ??= !$sealed ? Type::mixed() : null;
215215

216216
return new ArrayShapeType($shape, $extraKeyType, $extraValueType);
217217
}
218218

219+
public static function arrayKey(): UnionType
220+
{
221+
return self::union(self::int(), self::string());
222+
}
223+
219224
/**
220225
* @template T of class-string
221226
*
@@ -434,7 +439,7 @@ public static function fromValue(mixed $value): Type
434439
$keyTypes = array_values(array_unique($keyTypes));
435440
$keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0];
436441
} else {
437-
$keyType = Type::union(Type::int(), Type::string());
442+
$keyType = Type::arrayKey();
438443
}
439444

440445
$valueType = $valueTypes ? CollectionType::mergeCollectionValueTypes($valueTypes) : Type::mixed();

src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
171171
'iterable' => Type::iterable(),
172172
'mixed' => Type::mixed(),
173173
'null' => Type::null(),
174-
'array-key' => Type::union(Type::int(), Type::string()),
174+
'array-key' => Type::arrayKey(),
175175
'scalar' => Type::union(Type::int(), Type::float(), Type::string(), Type::bool()),
176176
'number' => Type::union(Type::int(), Type::float()),
177177
'numeric' => Type::union(Type::int(), Type::float(), Type::string()),

0 commit comments

Comments
 (0)
0