Skip to content

Commit

Permalink
improve action interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 9, 2025
1 parent d2fe51d commit 758aeb4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/Interfaces/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Chevere\Action\Interfaces;

use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;

/**
* Describes the component in charge of defining a single logic action.
Expand All @@ -26,6 +27,11 @@ interface ActionInterface
*/
public function __invoke(mixed ...$argument): mixed;

/**
* Provides access to the parameters defined at the main method.
*/
public static function parameters(): ParametersInterface;

/**
* Defines expected return parameter validation for main method.
*/
Expand Down
24 changes: 17 additions & 7 deletions src/Traits/ActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
*/
trait ActionTrait
{
protected ?ParametersInterface $parameters = null;

protected ?ParameterInterface $return = null;

final public function __invoke(mixed ...$argument): mixed
{
try {
Expand All @@ -40,7 +36,7 @@ final public function __invoke(mixed ...$argument): mixed
} catch (Throwable $e) {
// @infection-ignore-all
throw new ActionException(
...$this->getExceptionArguments($e),
...$this::getExceptionArguments($e),
);
}
$result = $this->main(...$arguments->toArray());
Expand All @@ -50,7 +46,7 @@ final public function __invoke(mixed ...$argument): mixed
} catch (Throwable $e) {
// @infection-ignore-all
throw new ActionException(
...$this->getExceptionArguments($e),
...$this::getExceptionArguments($e),
);
}

Expand All @@ -67,6 +63,20 @@ public static function mainMethod(): string
return 'main';
}

final public static function parameters(): ParametersInterface
{
try {
$reflection = static::assert();

return $reflection->parameters();
} catch (Throwable $e) {
// @infection-ignore-all
throw new ActionException(
...self::getExceptionArguments($e),
);
}
}

final public static function assert(): ReflectionActionInterface
{
$reflection = new ReflectionAction(static::class);
Expand Down Expand Up @@ -94,7 +104,7 @@ protected function assertRuntime(ReflectionActionInterface $reflection): void
}

// @phpstan-ignore-next-line
private function getExceptionArguments(Throwable $e): array
private static function getExceptionArguments(Throwable $e): array
{
// @infection-ignore-all
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];
Expand Down
24 changes: 15 additions & 9 deletions tests/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Chevere\Tests;

use Chevere\Action\Exceptions\ActionException;
use Chevere\Tests\src\ActionTestAction;
use Chevere\Parameter\Interfaces\StringParameterInterface;
use Chevere\Tests\src\ActionTestArrayAccessReturnType;
use Chevere\Tests\src\ActionTestAssertRuntimeAction;
use Chevere\Tests\src\ActionTestAssertStatic;
Expand All @@ -32,7 +32,6 @@
use Chevere\Tests\src\ActionTestUnionReturnMissingType;
use Chevere\Tests\src\ActionTestUnionReturnType;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

final class ActionTest extends TestCase
{
Expand All @@ -48,6 +47,13 @@ public function testMissingMainMethod(): void
$action->__invoke();
}

public function testParameters(): void
{
$parameters = ActionTestController::parameters();
$parameter = $parameters->get('name');
$this->assertInstanceOf(StringParameterInterface::class, $parameter);
}

public function testWithArguments(): void
{
$expected = 'PeoplesHernandez';
Expand Down Expand Up @@ -155,14 +161,14 @@ public function testNullParameterNoReturn(): void
$action->__invoke();
}

public function testParametersNullAssign(): void
public function testAttributeParameters(): void
{
$action = new ActionTestAction();
$reflection = new ReflectionProperty($action, 'parameters');
$this->assertTrue($reflection->isInitialized($action));
$this->assertNull($reflection->getValue($action));
$action->__invoke();
$reflection->getValue($action);
$parameters = ActionTestAttributes::parameters();
$parameter = $parameters->required('value')->string();
$this->assertSame(
'/^ab$/',
$parameter->regex()->__toString()
);
}

public function testAttributeValidation(): void
Expand Down

0 comments on commit 758aeb4

Please sign in to comment.