Skip to content

Commit 34892b3

Browse files
fix: revert enum parsing change that lead to unconditional failure
1 parent 3840f19 commit 34892b3

2 files changed

Lines changed: 3 additions & 79 deletions

File tree

src/Core/Conversion/EnumOf.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ final class EnumOf implements Converter
1919

2020
/**
2121
* @param list<bool|float|int|string|null> $members
22-
* @param class-string<\BackedEnum>|null $class
2322
*/
24-
public function __construct(
25-
private readonly array $members,
26-
private readonly ?string $class = null,
27-
) {
23+
public function __construct(private readonly array $members)
24+
{
2825
$type = 'NULL';
2926
foreach ($this->members as $member) {
3027
$type = gettype($member);
@@ -36,24 +33,13 @@ public function __construct(
3633
public static function fromBackedEnum(string $enum): self
3734
{
3835
// @phpstan-ignore-next-line argument.type
39-
return self::$cache[$enum] ??= new self(
40-
array_column($enum::cases(), column_key: 'value'),
41-
class: $enum,
42-
);
36+
return self::$cache[$enum] ??= new self(array_column($enum::cases(), column_key: 'value'));
4337
}
4438

4539
public function coerce(mixed $value, CoerceState $state): mixed
4640
{
4741
$this->tally($value, state: $state);
4842

49-
if ($value instanceof \BackedEnum) {
50-
return $value;
51-
}
52-
53-
if (null !== $this->class && (is_int($value) || is_string($value))) {
54-
return ($this->class)::tryFrom($value) ?? $value;
55-
}
56-
5743
return $value;
5844
}
5945

tests/Core/ModelTest.php

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,6 @@ public function __construct(
4747
}
4848
}
4949

50-
enum TicketPriority: string
51-
{
52-
case Low = 'low';
53-
case High = 'high';
54-
}
55-
56-
class Ticket implements BaseModel
57-
{
58-
/** @use SdkModel<array<string, mixed>> */
59-
use SdkModel;
60-
61-
#[Required(enum: TicketPriority::class)]
62-
public TicketPriority $priority;
63-
64-
/** @var list<TicketPriority> */
65-
#[Required(list: TicketPriority::class)]
66-
public array $labels;
67-
68-
public function __construct()
69-
{
70-
$this->initialize();
71-
}
72-
}
73-
7450
/**
7551
* @internal
7652
*
@@ -165,42 +141,4 @@ public function testSerializeModelWithExplicitNull(): void
165141
json_encode($model)
166142
);
167143
}
168-
169-
#[Test]
170-
public function testScalarEnumCoercesToInstance(): void
171-
{
172-
$model = Ticket::fromArray(['priority' => 'low', 'labels' => []]);
173-
$this->assertSame(TicketPriority::Low, $model->priority);
174-
}
175-
176-
#[Test]
177-
public function testListOfEnumCoercesElementsToInstances(): void
178-
{
179-
$model = Ticket::fromArray(['priority' => 'low', 'labels' => ['low', 'high']]);
180-
$this->assertCount(2, $model->labels);
181-
$this->assertSame(TicketPriority::Low, $model->labels[0]);
182-
$this->assertSame(TicketPriority::High, $model->labels[1]);
183-
}
184-
185-
#[Test]
186-
public function testEnumInstancePassesThrough(): void
187-
{
188-
$model = Ticket::fromArray(['priority' => TicketPriority::High, 'labels' => []]);
189-
$this->assertSame(TicketPriority::High, $model->priority);
190-
}
191-
192-
#[Test]
193-
public function testInvalidEnumScalarFallsBackToData(): void
194-
{
195-
$model = Ticket::fromArray(['priority' => 'urgent', 'labels' => []]);
196-
$this->assertSame('urgent', $model['priority']);
197-
}
198-
199-
#[Test]
200-
public function testEnumWireFormatStableAcrossConstruction(): void
201-
{
202-
$fromScalar = Ticket::fromArray(['priority' => 'low', 'labels' => ['high']]);
203-
$fromInstance = Ticket::fromArray(['priority' => TicketPriority::Low, 'labels' => [TicketPriority::High]]);
204-
$this->assertSame(json_encode($fromScalar), json_encode($fromInstance));
205-
}
206144
}

0 commit comments

Comments
 (0)