Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 4, 2024
1 parent 515b67f commit 918e48d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
28 changes: 17 additions & 11 deletions src/Attributes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Chevere\Parameter\Attributes;

use Chevere\Parameter\Exceptions\ParameterException;
use Chevere\Parameter\Interfaces\ArgumentsInterface;
use Chevere\Parameter\Interfaces\ParameterInterface;
use LogicException;
Expand Down Expand Up @@ -185,23 +186,28 @@ function valid(?string $name = null): void

return;
}
if ($parameters->optionalKeys()->contains($name)
&& ! array_key_exists($name, $arguments)
) {
return;
}

try {
$parameter = $parameters->get($name);
if ($parameters->optionalKeys()->contains($name) && ! array_key_exists($name, $arguments)) {
return;
if (! $parameters->has($name)) {
throw new LogicException(
(string) message(
'Parameter `%name%` not found',
name: $name,
)
);
}
$parameter = $parameters->get($name);
$parameter->__invoke($arguments[$name]);
} catch (Throwable $e) {
$invoker = $trace[0];
$file = $invoker['file'] ?? 'na';
$line = $invoker['line'] ?? 0;

throw new $e(
(string) message(
'%message% → %invokedAt%',
message: $e->getMessage(),
// @phpstan-ignore-next-line
invokedAt: $invoker['file'] . ':' . $invoker['line'],
)
);
throw new ParameterException($e->getMessage(), $e, $file, $line);
}
}
27 changes: 8 additions & 19 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,11 @@ function reflectedParameterAttribute(
return $attribute->newInstance();
}

function validated(
ReflectionFunction|ReflectionMethod $reflection,
mixed ...$args
): mixed {
function validated(callable $callable, mixed ...$args): mixed
{
// @phpstan-ignore-next-line
$reflection = new ReflectionFunction($callable);

try {
$parameters = reflectionToParameters($reflection);
$return = reflectionToReturnParameter($reflection);
Expand All @@ -341,11 +342,7 @@ function validated(
...getExceptionArguments($e, $reflection),
);
}
if ($reflection instanceof ReflectionMethod) {
$result = $reflection->invoke(null, ...$args);
} else {
$result = $reflection->invoke(...$args);
}
$result = $callable(...$args);

try {
$return->__invoke($result);
Expand All @@ -362,23 +359,15 @@ function validated(
/**
* @return array{0: string, 1: Throwable, 2: string, 3: int}
*/
function getExceptionArguments(Throwable $e, ReflectionFunction|ReflectionMethod $reflection): array
function getExceptionArguments(Throwable $e, ReflectionFunction $reflection): array
{
// @infection-ignore-all
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];
$class = $reflection instanceof ReflectionMethod
? $reflection->getDeclaringClass()->getName()
: null;
$function = $reflection->getName();
$actor = match (true) {
$class === null => $function,
default => $class . '::' . $function,
};

$message = (string) message(
'`%actor%` %exception% → %message%',
exception: $e::class,
actor: $actor,
actor: $function,
message: $e->getMessage(),
);

Expand Down
12 changes: 4 additions & 8 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use LogicException;
use OutOfBoundsException;
use PHPUnit\Framework\TestCase;
use ReflectionFunction;
use stdClass;
use TypeError;
use function Chevere\Parameter\arguments;
Expand Down Expand Up @@ -379,35 +378,32 @@ public function testParameterAttr(): void
public function testValidatedParameterError(): void
{
$function = 'Chevere\Tests\src\validates';
$reflection = new ReflectionFunction($function);
$base = 0;
$times = 1;
$name = 'Test';
$this->expectException(ParameterException::class);
$this->expectExceptionMessage("`{$function}` InvalidArgumentException → [base]: Argument value provided `0` is less than `1`");
validated($reflection, $base, $times, $name);
validated($function, $base, $times, $name);
}

public function testValidatedReturnError(): void
{
$function = 'Chevere\Tests\src\validates';
$reflection = new ReflectionFunction($function);
$base = 99;
$times = 1;
$name = 'Test';
$this->expectException(ReturnException::class);
$this->expectExceptionMessage("`{$function}` InvalidArgumentException → Argument value provided `99` is less than `100`");
validated($reflection, $base, $times, $name);
validated($function, $base, $times, $name);
}

public function testValidated(): void
public function testValidatedFunction(): void
{
$function = 'Chevere\Tests\src\validates';
$reflection = new ReflectionFunction($function);
$base = 100;
$times = 1;
$name = 'Test';
$this->expectNotToPerformAssertions();
validated($reflection, $base, $times, $name);
validated($function, $base, $times, $name);
}
}
24 changes: 22 additions & 2 deletions tests/NullParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,34 @@ public function testConstruct(): void
{
$parameter = new NullParameter();
$this->assertSame(null, $parameter->default());
$compatible = new NullParameter();
$parameter->assertCompatible($compatible);
$this->assertSame([
'type' => 'null',
'description' => '',
'default' => null,
], $parameter->schema());
$parameter(null);
}

public function testWithDefault(): void
{
$parameter = new NullParameter();
$with = $parameter->withDefault(null);
$this->assertNotSame($parameter, $with);
$parameter->assertCompatible($with);
$with(null);
}

public function testCompatible(): void
{
$this->expectNotToPerformAssertions();
$parameter = new NullParameter();
$compatible = new NullParameter();
$parameter->assertCompatible($compatible);
}

public function testError(): void
{
$parameter = new NullParameter();
$this->expectException(TypeError::class);
$this->expectExceptionMessage(
<<<PLAIN
Expand Down
10 changes: 10 additions & 0 deletions tests/src/NoUsesAttr.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Chevere\Tests\src;

use Chevere\Parameter\Exceptions\ParameterException;
use Chevere\Parameter\Interfaces\ParameterAttributeInterface;
use Throwable;
use function Chevere\Parameter\Attributes\arrayArguments;
Expand Down Expand Up @@ -51,6 +52,15 @@ public function __construct(
valid('tags');
valid('flag');
valid('amount');

try {
valid('404');
} catch (ParameterException $e) {
assertSame(
'Parameter `404` not found',
$e->getMessage()
);
}
// Get attribute, validate and return
try {
$name = stringAttr('name')($name);
Expand Down

0 comments on commit 918e48d

Please sign in to comment.