Skip to content

Commit 65eb3eb

Browse files
committed
bug #60956 [TypeInfo] Fix Type::fromValue incorrectly setting object type instead of enum (norkunas)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [TypeInfo] Fix `Type::fromValue` incorrectly setting object type instead of enum | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | Fix #... | License | MIT Tried to apply #60820 (comment) comment, but found some failing cases Commits ------- a0cdd3f [TypeInfo] Fix `Type::fromValue` incorrectly setting object type instead of enum
2 parents e06b10c + a0cdd3f commit 65eb3eb

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ public static function createFromValueProvider(): iterable
254254
yield [Type::object(\DateTimeImmutable::class), new \DateTimeImmutable()];
255255
yield [Type::object(), new \stdClass()];
256256
yield [Type::list(Type::object()), [new \stdClass(), new \DateTimeImmutable()]];
257+
yield [Type::enum(DummyEnum::class), DummyEnum::ONE];
258+
yield [Type::enum(DummyBackedEnum::class), DummyBackedEnum::ONE];
257259

258260
// collection
259261
$arrayAccess = new class implements \ArrayAccess {

src/Symfony/Component/TypeInfo/TypeFactoryTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ public static function fromValue(mixed $value): Type
412412
}
413413

414414
$type = match (true) {
415+
\is_object($value) && is_subclass_of($value::class, \UnitEnum::class) => Type::enum($value::class),
415416
\is_object($value) => \stdClass::class === $value::class ? self::object() : self::object($value::class),
416417
\is_array($value) => self::builtin(TypeIdentifier::ARRAY),
417418
default => null,
@@ -428,8 +429,6 @@ public static function fromValue(mixed $value): Type
428429
/** @var list<Type> $valueTypes */
429430
$valueTypes = [];
430431

431-
$i = 0;
432-
433432
foreach ($value as $k => $v) {
434433
$keyTypes[] = self::fromValue($k);
435434
$valueTypes[] = self::fromValue($v);

0 commit comments

Comments
 (0)