Skip to content
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: 2 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ public static function createFromValueProvider(): iterable
yield [Type::object(\DateTimeImmutable::class), new \DateTimeImmutable()];
yield [Type::object(), new \stdClass()];
yield [Type::list(Type::object()), [new \stdClass(), new \DateTimeImmutable()]];
yield [Type::enum(DummyEnum::class), DummyEnum::ONE];
yield [Type::enum(DummyBackedEnum::class), DummyBackedEnum::ONE];

// collection
$arrayAccess = new class implements \ArrayAccess {
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Component/TypeInfo/TypeFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ public static function fromValue(mixed $value): Type
}

$type = match (true) {
\is_object($value) && is_subclass_of($value::class, \UnitEnum::class) => Type::enum($value::class),
Copy link
Member

Choose a reason for hiding this comment

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

$value instanceof \UnitEnum would be better (instanceof has more built-in optimizations than is_subclass_of() btw)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok will change it. I just copied it from TypeFactoryTrait::enum method

\is_object($value) => \stdClass::class === $value::class ? self::object() : self::object($value::class),
\is_array($value) => self::builtin(TypeIdentifier::ARRAY),
default => null,
Expand All @@ -428,8 +429,6 @@ public static function fromValue(mixed $value): Type
/** @var list<Type> $valueTypes */
$valueTypes = [];

$i = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice one 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops 😅


foreach ($value as $k => $v) {
$keyTypes[] = self::fromValue($k);
$valueTypes[] = self::fromValue($v);
Expand Down
Loading