8000 [TypeInfo] add TypeFactoryTrait::arrayKey() by xabbuh · Pull Request #60087 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TypeInfo] add TypeFactoryTrait::arrayKey() #60087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/TypeInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
---

* Add `Type::accepts()` method
* Add `TypeFactoryTrait::fromValue()` method
* Add the `TypeFactoryTrait::fromValue()`, `TypeFactoryTrait::arrayShape()`, and `TypeFactoryTrait::arrayKey()` methods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can merge the accepts method as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accepts() was documented for Type as it's not in the trait. Does that really make sense to merge them or isn't it more readable this way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods are most likely to be accessed via the Type class. But we can keep them separate in the changelog.

* Deprecate constructing a `CollectionType` instance as a list that is not an array
* Deprecate the third `$asList` argument of `TypeFactoryTrait::iterable()`, use `TypeFactoryTrait::list()` instead
* Add type alias support in `TypeContext` and `StringTypeResolver`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testGetCollectionKeyType()
1 => ['type' => Type::bool(), 'optional' => false],
'foo' => ['type' => Type::bool(), 'optional' => false],
]);
$this->assertEquals(Type::union(Type::int(), Type::string()), $type->getCollectionKeyType());
$this->assertEquals(Type::arrayKey(), $type->getCollectionKeyType());
}

public function testGetCollectionValueType()
Expand Down Expand Up @@ -134,7 +134,7 @@ public function testToString()

$type = new ArrayShapeType(
shape: ['foo' => ['type' => Type::bool()]],
extraKeyType: Type::union(Type::int(), Type::string()),
extraKeyType: Type::arrayKey(),
extraValueType: Type::mixed(),
);
$this->assertSame("array{'foo': bool, ...}", (string) $type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testIsList()
public function testGetCollectionKeyType()
{
$type = new CollectionType(Type::builtin(TypeIdentifier::ARRAY));
$this->assertEquals(Type::union(Type::int(), Type::string()), $type->getCollectionKeyType());
$this->assertEquals(Type::arrayKey(), $type->getCollectionKeyType());

$type = new CollectionType(Type::generic(Type::builtin(TypeIdentifier::ARRAY), Type::bool()));
$this->assertEquals(Type::int(), $type->getCollectionKeyType());
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function testCreateArrayShape()
$this->assertEquals(new ArrayShapeType(['foo' => ['type' => Type::bool(), 'optional' => false]]), Type::arrayShape(['foo' => Type::bool()]));
$this->assertEquals(new ArrayShapeType(
shape: ['foo' => ['type' => Type::bool(), 'optional' => false]],
extraKeyType: Type::union(Type::int(), Type::string()),
extraKeyType: Type::arrayKey(),
extraValueType: Type::mixed(),
), Type::arrayShape(['foo' => Type::bool()], sealed: false));
$this->assertEquals(new ArrayShapeType(
Expand All @@ -222,6 +222,11 @@ public function testCreateArrayShape()
), Type::arrayShape(['foo' => Type::bool()], extraKeyType: Type::string(), extraValueType: Type::bool()));
}

public function testCreateArrayKey()
{
$this->assertEquals(new UnionType(Type::int(), Type::string()), Type::arrayKey());
}

/**
* @dataProvider createFromValueProvider
*/
Expand Down Expand Up @@ -275,7 +280,7 @@ public function offsetUnset(mixed $offset): void
yield [Type::dict(Type::bool()), ['a' => true, 'b' => false]];
yield [Type::array(Type::string()), [1 => 'foo', 'bar' => 'baz']];
yield [Type::array(Type::nullable(Type::bool()), Type::int()), [1 => true, 2 => null, 3 => false]];
yield [Type::collection(Type::object(\ArrayIterator::class), Type::mixed(), Type::union(Type::int(), Type::string())), new \ArrayIterator()];
yield [Type::collection(Type::object(\ArrayIterator::class), Type::mixed(), Type::arrayKey()), new \ArrayIterator()];
yield [Type::collection(Type::object(\Generator::class), Type::string(), Type::int()), (fn (): iterable => yield 'string')()];
yield [Type::collection(Type::object($arrayAccess::class)), $arrayAccess];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static function resolveDataProvider(): iterable
yield [Type::never(), 'never-return'];
yield [Type::never(), 'never-returns'];
yield [Type::never(), 'no-return'];
yield [Type::union(Type::int(), Type::string()), 'array-key'];
yield [Type::arrayKey(), 'array-key'];
yield [Type::union(Type::int(), Type::float(), Type::string(), Type::bool()), 'scalar'];
yield [Type::union(Type::int(), Type::float()), 'number'];
yield [Type::union(Type::int(), Type::float(), Type::string()), 'numeric'];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/TypeInfo/Type/ArrayShapeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
$keyTypes = array_values(array_unique($keyTypes));
$keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0];
} else {
$keyType = Type::union(Type::int(), Type::string());
$keyType = Type::arrayKey();
}

$valueType = $valueTypes ? CollectionType::mergeCollectionValueTypes($valueTypes) : Type::mixed();
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/TypeInfo/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function isList(): bool

public function getCollectionKeyType(): Type
{
$defaultCollectionKeyType = self::union(self::int(), self::string());
$defaultCollectionKeyType = self::arrayKey();

if ($this->type instanceof GenericType) {
return match (\count($this->type->getVariableTypes())) {
Expand Down
11 changes: 8 additions & 3 deletions src/Symfony/Component/TypeInfo/TypeFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static function never(): BuiltinType
public static function collection(BuiltinType|ObjectType|GenericType $type, ?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
{
if (!$type instanceof GenericType && (null !== $value || null !== $key)) {
$type = self::generic($type, $key ?? self::union(self::int(), self::string()), $value ?? self::mixed());
$type = self::generic($type, $key ?? self::arrayKey(), $value ?? self::mixed());
}

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

$extraKeyType ??= !$sealed ? Type::union(Type::int(), Type::string()) : null;
$extraKeyType ??= !$sealed ? Type::arrayKey() : null;
$extraValueType ??= !$sealed ? Type::mixed() : null;

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

public static function arrayKey(): UnionType
{
return self::union(self::int(), self::string());
}

/**
* @template T of class-string
*
Expand Down Expand Up @@ -434,7 +439,7 @@ public static function fromValue(mixed $value): Type
$keyTypes = array_values(array_unique($keyTypes));
$keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0];
} else {
$keyType = Type::union(Type::int(), Type::string());
$keyType = Type::arrayKey();
}

$valueType = $valueTypes ? CollectionType::mergeCollectionValueTypes($valueTypes) : Type::mixed();
Expand Down
69DF
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
'iterable' => Type::iterable(),
'mixed' => Type::mixed(),
'null' => Type::null(),
'array-key' => Type::union(Type::int(), Type::string()),
'array-key' => Type::arrayKey(),
'scalar' => Type::union(Type::int(), Type::float(), Type::string(), Type::bool()),
'number' => Type::union(Type::int(), Type::float()),
'numeric' => Type::union(Type::int(), Type::float(), Type::string()),
Expand Down
Loading
0