Skip to content

Commit

Permalink
typos
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 4, 2024
1 parent 1f46ed5 commit f2ddfca
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 76 deletions.
9 changes: 1 addition & 8 deletions src/Attributes/ArrayAttr.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
use Chevere\Parameter\Interfaces\ArrayParameterInterface;
use Chevere\Parameter\Interfaces\ParameterAttributeInterface;
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ParametersAccessInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use function Chevere\Parameter\arrayp;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::TARGET_CLASS_CONSTANT)]
class ArrayAttr implements ParameterAttributeInterface, ParametersAccessInterface
class ArrayAttr implements ParameterAttributeInterface
{
public readonly ArrayParameterInterface $parameter;

Expand Down Expand Up @@ -52,9 +50,4 @@ public function parameter(): ParameterInterface
{
return $this->parameter;
}

public function parameters(): ParametersInterface
{
return $this->parameter->parameters();
}
}
107 changes: 52 additions & 55 deletions src/Attributes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ function floatAttr(string $name): FloatAttr
return parameterAttr($name, $caller);
}

function boolAttr(string $name): BoolAttr
{
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];

// @phpstan-ignore-next-line
return parameterAttr($name, $caller);
}

function nullAttr(string $name): NullAttr
{
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];

// @phpstan-ignore-next-line
return parameterAttr($name, $caller);
}

function arrayAttr(string $name): ArrayAttr
{
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];
Expand All @@ -71,6 +87,42 @@ function iteratorAttr(string $name): IterableAttr
return parameterAttr($name, $caller);
}

function returnAttr(): ReturnAttr
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$caller = $trace[1];
$class = $caller['class'] ?? null;
$method = $caller['function'];
$magicReturn = "{$class}::return";
$reflection = $class
? new ReflectionMethod($class, $method)
: new ReflectionFunction($method);
$attribute = $reflection->getAttributes(ReturnAttr::class)[0] ?? null;
if ($attribute === null) {
if (! is_callable($magicReturn)) {
throw new LogicException(
(string) message(
'No applicable return rules to validate',
)
);
}
$parameter = $magicReturn();
if (! $parameter instanceof ParameterInterface) {
throw new LogicException(
(string) message(
'Callable return must return a %type% instance',
type: ParameterInterface::class
)
);
}
} else {
$attribute = $attribute->newInstance();
}

/** @var ReturnAttr $attribute */
return $attribute;
}

/**
* Get Arguments for an array parameter.
*/
Expand Down Expand Up @@ -152,58 +204,3 @@ function valid(?string $name = null): void
);
}
}

/**
* Validates `$var` against the return attribute.
*
* @return mixed The validated `$var`.
*/
function validReturn(mixed $var): mixed
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$caller = $trace[1];
$class = $caller['class'] ?? null;
$method = $caller['function'];
$magicReturn = "{$class}::return";
$reflection = $class
? new ReflectionMethod($class, $method)
: new ReflectionFunction($method);
$attribute = $reflection->getAttributes(ReturnAttr::class)[0] ?? null;
if ($attribute === null) {
if (! is_callable($magicReturn)) {
throw new LogicException(
(string) message(
'No applicable return rules to validate',
)
);
}
$parameter = $magicReturn();
if (! $parameter instanceof ParameterInterface) {
throw new LogicException(
(string) message(
'Callable return must return a %type% instance',
type: ParameterInterface::class
)
);
}
} else {
$attribute = $attribute->newInstance();
/** @var ReturnAttr $attribute */
$parameter = $attribute->parameter();
}

try {
return $parameter->__invoke($var);
} catch (Throwable $e) {
$invoker = $trace[0];

throw new $e(
(string) message(
'%message% → %invokedAt%',
message: $e->getMessage(),
// @phpstan-ignore-next-line
invokedAt: $invoker['file'] . ':' . $invoker['line'],
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class ParameterAttributesTest extends TestCase
final class UsesParameterAttributesTest extends TestCase
{
public function dataProviderWillSuccess(): array
{
Expand All @@ -29,6 +29,10 @@ public function dataProviderWillSuccess(): array
'id' => 1,
],
'tags' => ['Chevere', 'Chevere', 'Chevere', 'Uh'],
'flag' => false,
'amount' => 0,
'null' => null,
'enum' => 'test',
],
];
}
Expand All @@ -43,6 +47,10 @@ public function dataProviderWillFail(): array
'id' => 1,
],
'tags' => ['people'],
'flag' => false,
'amount' => 0,
'null' => null,
'enum' => 'test',
'error' => "Argument value provided `Peoples Hernandez` doesn't match the regex `/^[A-Za-z]+$/`",
],
[
Expand All @@ -52,6 +60,10 @@ public function dataProviderWillFail(): array
'id' => 1,
],
'tags' => ['zero'],
'flag' => false,
'amount' => 0,
'null' => null,
'enum' => 'test',
'error' => 'Argument value provided `0` is less than `1`',
],
[
Expand All @@ -61,6 +73,10 @@ public function dataProviderWillFail(): array
'id' => 1,
],
'tags' => ['dalmata'],
'flag' => false,
'amount' => 0,
'null' => null,
'enum' => 'test',
'error' => 'Argument value provided `101` is greater than `100`',
],
[
Expand All @@ -70,6 +86,10 @@ public function dataProviderWillFail(): array
'id' => 0,
],
'tags' => ['zeroid'],
'flag' => false,
'amount' => 0,
'null' => null,
'enum' => 'test',
'error' => '[id]: Argument value provided `0` is less than `1`',
],
[
Expand All @@ -79,8 +99,38 @@ public function dataProviderWillFail(): array
'id' => 42,
],
'tags' => [123],
'flag' => false,
'amount' => 0,
'null' => null,
'enum' => 'test',
'error' => 'Argument #1 ($value) must be of type Stringable|string, int given',
],
[
'name' => 'negativeAmount',
'age' => 24,
'cols' => [
'id' => 42,
],
'tags' => ['test'],
'flag' => false,
'amount' => -10.5,
'null' => null,
'enum' => 'test',
'error' => 'Argument value provided `-10.5` is less than `0`',
],
[
'name' => 'wrongEnum',
'age' => 24,
'cols' => [
'id' => 42,
],
'tags' => ['test'],
'flag' => false,
'amount' => 100.5,
'null' => null,
'enum' => 'try',
'error' => "Argument value provided `try` doesn't match the regex `/\b(test|value)\b/`",
],
];
}

Expand All @@ -90,14 +140,19 @@ public function dataProviderWillFail(): array
public function testWillFail(
string $name,
int $age,
array $array,
iterable $iterable,
array $cols,
iterable $tags,
bool $flag,
float $amount,
mixed $null,
string $enum,
string $error
): void {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($error);

new UsesParameterAttributes($name, $age, $array, $iterable);
$args = func_get_args();
array_pop($args);
new UsesParameterAttributes(...$args);
}

/**
Expand All @@ -106,15 +161,19 @@ public function testWillFail(
public function testWillSuccess(
string $name,
int $age,
array $array,
iterable $iterable
array $cols,
iterable $tags,
bool $flag,
float $amount,
mixed $null,
string $enum,
): void {
$this->expectNotToPerformAssertions();

new UsesParameterAttributes($name, $age, $array, $iterable);
new UsesParameterAttributes(...func_get_args());
}

public function testThisWea(): void
public function testReturnAttr(): void
{
$arguments = $this->dataProviderWillSuccess()[0];
$object = new UsesParameterAttributes(...$arguments);
Expand Down
20 changes: 16 additions & 4 deletions tests/src/UsesParameterAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@
use Chevere\Parameter\Interfaces\ParameterInterface;
use function Chevere\Parameter\Attributes\arrayArguments;
use function Chevere\Parameter\Attributes\arrayAttr;
use function Chevere\Parameter\Attributes\boolAttr;
use function Chevere\Parameter\Attributes\enumAttr;
use function Chevere\Parameter\Attributes\floatAttr;
use function Chevere\Parameter\Attributes\intAttr;
use function Chevere\Parameter\Attributes\iteratorAttr;
use function Chevere\Parameter\Attributes\nullAttr;
use function Chevere\Parameter\Attributes\returnAttr;
use function Chevere\Parameter\Attributes\stringAttr;
use function Chevere\Parameter\Attributes\valid;
use function Chevere\Parameter\Attributes\validReturn;
use function Chevere\Parameter\int;

final class UsesParameterAttributes
Expand Down Expand Up @@ -62,19 +66,27 @@ public function __construct(
#[EnumAttr('test', 'value')]
string $enum = 'value',
) {
// Validate all
valid();
// Pick validation
valid('name');
valid('age');
valid('cols');
valid('tags');
valid('flag');
valid('amount');
valid();
// Get attribute, validate and return
$name = stringAttr('name')($name);
$age = intAttr('age')($age);
$cols = arrayAttr('cols')($cols);
$id = arrayArguments('cols')->required('id')->int();
$tags = iteratorAttr('tags')($tags);
validReturn($id);
$flag = boolAttr('flag')($flag);
$amount = floatAttr('amount')($amount);
$null = nullAttr('null')($null);
$enum = enumAttr('enum')($enum);
// Validate return attr
returnAttr()($id);
}

public static function callable(): ParameterInterface
Expand All @@ -92,6 +104,6 @@ public static function return(): ParameterInterface
)]
public function run(int $int): int
{
return validReturn($int);
return returnAttr()($int);
}
}

0 comments on commit f2ddfca

Please sign in to comment.