Skip to content

Commit

Permalink
Switch to TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Sep 3, 2023
1 parent 26ac664 commit 5dc5e01
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Internal/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ function cast(mixed $value): string|int|float|null
"object" => match (true) {
$value instanceof \BackedEnum => $value->value,
$value instanceof \Stringable => (string) $value,
default => throw new \ValueError(
default => throw new \TypeError(
"An object in parameter values must be a BackedEnum or implement Stringable; got instance of "
. \get_debug_type($value)
),
},
default => throw new \ValueError(\sprintf(
default => throw new \TypeError(\sprintf(
"Invalid value type '%s' in parameter values",
\get_debug_type($value),
)),
Expand All @@ -132,7 +132,7 @@ function encodeArrayItem(mixed $value): mixed
"object" => match (true) {
$value instanceof \BackedEnum => encodeArrayItem($value->value),
$value instanceof \Stringable => encodeArrayItem((string) $value),
default => throw new \ValueError(
default => throw new \TypeError(
"An object in parameter arrays must be a BackedEnum or implement Stringable; "
. "got instance of " . \get_debug_type($value)
),
Expand Down
8 changes: 4 additions & 4 deletions test/CastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,31 @@ public function testBackedEnumInArray(): void

public function testUnitEnum(): void
{
$this->expectException(\ValueError::class);
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('An object in parameter values must be');

cast(UnitEnum::Case);
}

public function testUnitEnumInArray(): void
{
$this->expectException(\ValueError::class);
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('An object in parameter arrays must be');

cast([UnitEnum::Case]);
}

public function testObjectWithoutToStringMethod(): void
{
$this->expectException(\ValueError::class);
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('An object in parameter values must be');

cast(new \stdClass);
}

public function testObjectWithoutToStringMethodInArray(): void
{
$this->expectException(\ValueError::class);
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('An object in parameter arrays must be');

cast([new \stdClass]);
Expand Down

0 comments on commit 5dc5e01

Please sign in to comment.