Skip to content

Commit

Permalink
try errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 22, 2023
1 parent 9f95da6 commit b4c82e1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 144 deletions.
26 changes: 25 additions & 1 deletion src/UnionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use Chevere\Parameter\Traits\ArrayParameterTrait;
use Chevere\Parameter\Traits\ParameterAssertArrayTypeTrait;
use Chevere\Parameter\Traits\ParameterTrait;
use Throwable;
use TypeError;
use function Chevere\Message\message;

final class UnionParameter implements UnionParameterInterface
{
Expand All @@ -42,7 +45,28 @@ final public function __construct(

public function __invoke(mixed $value): mixed
{
return assertUnion($this, $value);
$types = [];
$errors = [];
foreach ($this->parameters() as $item) {
try {
assertNamedArgument('', $item, $value);

return $value;
} catch (Throwable $e) {
$types[] = $item::class;
$errors[] = $e->getMessage();
}
}
$types = implode('|', $types);
$errors = implode('; ', $errors);

throw new TypeError(

Check warning on line 63 in src/UnionParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ } $types = implode('|', $types); $errors = implode('; ', $errors); - throw new TypeError((string) message("Argument provided doesn't match the union type `%types%`. Error(s): %errors%", types: $types, errors: $errors)); + new TypeError((string) message("Argument provided doesn't match the union type `%types%`. Error(s): %errors%", types: $types, errors: $errors)); } public function withAdded(ParameterInterface ...$parameter) : static {

Check warning on line 63 in src/UnionParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ } $types = implode('|', $types); $errors = implode('; ', $errors); - throw new TypeError((string) message("Argument provided doesn't match the union type `%types%`. Error(s): %errors%", types: $types, errors: $errors)); + new TypeError((string) message("Argument provided doesn't match the union type `%types%`. Error(s): %errors%", types: $types, errors: $errors)); } public function withAdded(ParameterInterface ...$parameter) : static {

Check warning on line 63 in src/UnionParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ } $types = implode('|', $types); $errors = implode('; ', $errors); - throw new TypeError((string) message("Argument provided doesn't match the union type `%types%`. Error(s): %errors%", types: $types, errors: $errors)); + new TypeError((string) message("Argument provided doesn't match the union type `%types%`. Error(s): %errors%", types: $types, errors: $errors)); } public function withAdded(ParameterInterface ...$parameter) : static {
(string) message(
"Argument provided doesn't match the union type `%types%`. Error(s): %errors%",
types: $types,
errors: $errors,
)
);
}

public function withAdded(ParameterInterface ...$parameter): static
Expand Down
24 changes: 0 additions & 24 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use ReflectionMethod;
use ReflectionParameter;
use Throwable;
use TypeError;
use function Chevere\Message\message;

function cast(mixed $argument): CastInterface
Expand Down Expand Up @@ -92,29 +91,6 @@ function arguments(
return new Arguments($parameters, $arguments);
}

function assertUnion(
UnionParameterInterface $parameter,
mixed $argument,
): mixed {
$types = [];
foreach ($parameter->parameters() as $item) {
try {
assertNamedArgument('', $item, $argument);

return $argument;
} catch (Throwable $e) {
$types[] = $item::class;
}
}

throw new TypeError(
(string) message(
"Argument provided doesn't match the union type `%type%`",
type: implode('|', $types)
)
);
}

function assertNamedArgument(
string $name,
ParameterInterface $parameter,
Expand Down
14 changes: 0 additions & 14 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use function Chevere\Parameter\arrayp;
use function Chevere\Parameter\assertArray;
use function Chevere\Parameter\assertNamedArgument;
use function Chevere\Parameter\assertUnion;
use function Chevere\Parameter\bool;
use function Chevere\Parameter\float;
use function Chevere\Parameter\generic;
Expand All @@ -37,7 +36,6 @@
use function Chevere\Parameter\string;
use function Chevere\Parameter\takeFrom;
use function Chevere\Parameter\takeKeys;
use function Chevere\Parameter\union;

final class FunctionsTest extends TestCase
{
Expand Down Expand Up @@ -211,18 +209,6 @@ public function testFunctionGenericParameter(): void
$this->assertSame('foo', $parameter->description());
}

public function testFunctionUnionParameter(): void
{
$parameter = union(
int(),
string(),
);
assertUnion($parameter, 'foo');
assertUnion($parameter, 123);
$this->expectException(TypeError::class);
assertUnion($parameter, []);
}

public function testAssertArrayExtraArguments(): void
{
$parameter = arrayp(
Expand Down
105 changes: 0 additions & 105 deletions tests/FunctionsUnionTest.php

This file was deleted.

0 comments on commit b4c82e1

Please sign in to comment.