8000 [TypeInfo] Add `TypeFactoryTrait::fromValue` method by mtarld · Pull Request #59368 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TypeInfo] Add TypeFactoryTrait::fromValue method #59368

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
Jan 17, 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
1 change: 1 addition & 0 deletions src/Symfony/Component/TypeInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `Type::accepts()` method
* Add `TypeFactoryTrait::fromValue()` method

7.2
---
Expand Down
57 changes: 57 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,61 @@ public function testCreateNullable()
Type::nullable(Type::union(Type::int(), Type::string(), Type::null())),
);
}

/**
* @dataProvider createFromValueProvider
*/
public function testCreateFromValue(Type $expected, mixed $value)
{
$this->assertEquals($expected, Type::fromValue($value));
}

/**
* @return iterable<array{0: Type, 1: mixed}>
*/
public static function createFromValueProvider(): iterable
{
// builtin
yield [Type::null(), null];
yield [Type::true(), true];
yield [Type::false(), false];
yield [Type::int(), 1];
yield [Type::float(), 1.1];
yield [Type::string(), 'string'];
yield [Type::callable(), strtoupper(...)];
yield [Type::resource(), fopen('php://temp', 'r')];

// object
yield [Type::object(\DateTimeImmutable::class), new \DateTimeImmutable()];
yield [Type::object(), new \stdClass()];

// collection
$arrayAccess = new class implements \ArrayAccess {
public function offsetExists(mixed $offset): bool
{
return true;
}

public function offsetGet(mixed $offset): mixed
{
return null;
}

public function offsetSet(mixed $offset, mixed $value): void
{
}

public function offsetUnset(mixed $offset): void
{
}
};

yield [Type::list(Type::int()), [1, 2, 3]];
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(\Generator::class), Type::string(), Type::int()), (fn (): iterable => yield 'string')()];
yield [Type::collection(Type::object($arrayAccess::class)), $arrayAccess];
}
}
95 changes: 95 additions & 0 deletions src/Symfony/Component/TypeInfo/TypeFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,99 @@ public static function nullable(Type $type): Type

return new NullableType($type);
}

public static function fromValue(mixed $value): Type
{
$type = match ($value) {
null => self::null(),
true => self::true(),
false => self::false(),
default => null,
};

if (null !== $type) {
return $type;
}

if (\is_callable($value)) {
return Type::callable();
}

if (\is_resource($value)) {
return Type::resource();
}

$type = match (get_debug_type($value)) {
TypeIdentifier::INT->value => self::int(),
TypeIdentifier::FLOAT->value => self::float(),
TypeIdentifier::STRING->value => self::string(),
default => null,
};

if (null !== $type) {
return $type;
}

$type = match (true) {
\is_object($value) => \stdClass::class === $value::class ? self::object() : self::object($value::class),
\is_array($value) => self::builtin(TypeIdentifier::ARRAY),
default => null,
};

if (null === $type) {
return Type::mixed();
}

if (is_iterable($value)) {
/** @var list<BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::STRING>> $keyTypes */
$keyTypes = [];

/** @var list<Type|null> $valueTypes */
$valueTypes = [];

$i = 0;

foreach ($value as $k => $v) {
$keyTypes[] = self::fromValue($k);
$keyTypes = array_unique($keyTypes);

$valueTypes[] = self::fromValue($v);
$valueTypes = array_unique($valueTypes);
}

if ([] !== $keyTypes) {
$keyTypes = array_values($keyTypes);
$keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0];

$valueType = null;
foreach ($valueTypes as &$v) {
if ($v->isIdentifiedBy(TypeIdentifier::MIXED)) {
$valueType = Type::mixed();

break;
}

if ($v->isIdentifiedBy(TypeIdentifier::TRUE, TypeIdentifier::FALSE)) {
$v = Type::bool();
}
}

if (!$valueType) {
$valueTypes = array_values(array_unique($valueTypes));
$valueType = \count($valueTypes) > 1 ? self::union(...$valueTypes) : $valueTypes[0];
}
} else {
$keyType = Type::union(Type::int(), Type::string());
$valueType = Type::mixed();
}

return self::collection($type, $valueType, $keyType, \is_array($value) && array_is_list($value));
}

if ($value instanceof \ArrayAccess) {
return self::collection($type);
}

return $type;
}
}
0