PHPackages                             chevere/action - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. chevere/action

ActiveLibrary

chevere/action
==============

Type-safe invokable objects with attribute-based parameter and return value validation

3.0.1(2mo ago)34.5k↑61.1%6Apache-2.0PHPPHP ^8.1CI passing

Since Jan 11Pushed 1mo agoCompare

[ Source](https://github.com/chevere/action)[ Packagist](https://packagist.org/packages/chevere/action)[ Docs](https://chevere.org)[ RSS](/packages/chevere-action/feed)WikiDiscussions 3.0 Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (14)Used By (6)

Action
======

[](#action)

[![Chevere](chevere.svg)](chevere.svg)

[![Build](https://camo.githubusercontent.com/412f070ab6f4509208c4e08b3188de76420312651f95413cbb84dff6fb98d2a7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636865766572652f616374696f6e2f746573742e796d6c3f6272616e63683d332e30267374796c653d666c61742d737175617265)](https://github.com/chevere/action/actions)[![Code size](https://camo.githubusercontent.com/d6af6f2d751b2f85e97522dc90f022051694b4b3bbe04c482b7e2a6f8b21ea9b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f636865766572652f616374696f6e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/d6af6f2d751b2f85e97522dc90f022051694b4b3bbe04c482b7e2a6f8b21ea9b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f636865766572652f616374696f6e3f7374796c653d666c61742d737175617265)[![Apache-2.0](https://camo.githubusercontent.com/d75e5b29b53a0ec97d74cc31246df27727e5e8cd62674b0d3201e4c9848c6d07/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f636865766572652f616374696f6e3f7374796c653d666c61742d737175617265)](LICENSE)[![PHPStan](https://camo.githubusercontent.com/6016298b28550819030c76e9327f62501596a31fd76406695bae2f3d2a1f26a4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d626c756576696f6c65743f7374796c653d666c61742d737175617265)](https://phpstan.org/)[![Mutation testing badge](https://camo.githubusercontent.com/aaaa0db5719a4e654e9389d21d7d72ac8cf7d0e5eb64f1d3b938eb5de26f0847/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742d7371756172652675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d25324663686576657265253246616374696f6e253246332e30)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/action/3.0)

[![Quality Gate Status](https://camo.githubusercontent.com/01eec60ad4d4bf7ee6aac2635e8552e8179a2ed4e9a0c3dda7d963fadfa7ccba/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f616374696f6e266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=chevere_action)[![Maintainability Rating](https://camo.githubusercontent.com/42eac071b3c3e7082e1fc24da658c221579cbe3fb8af2208ba8ffad77810b287/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f616374696f6e266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_action)[![Reliability Rating](https://camo.githubusercontent.com/248d3841377da23df22e07bd6bd17e488750054f3c36f26c267184200eca2aaf/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f616374696f6e266d65747269633d72656c696162696c6974795f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_action)[![Security Rating](https://camo.githubusercontent.com/a0a515a83fd1e117aeb6bb2682183b0521c171d774eeb5d19b81a3b34550e4aa/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f616374696f6e266d65747269633d73656375726974795f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_action)[![Coverage](https://camo.githubusercontent.com/93783d21c2da0c0a0c8d809ad44296306fd0a78ff948fd55d95f21efc09c84e2/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f616374696f6e266d65747269633d636f766572616765)](https://sonarcloud.io/dashboard?id=chevere_action)[![Technical Debt](https://camo.githubusercontent.com/daec205754a9f08d26e81c599bb7f6d1069509650718e2c8a137ad340d71e742/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f616374696f6e266d65747269633d7371616c655f696e646578)](https://sonarcloud.io/dashboard?id=chevere_action)[![CodeFactor](https://camo.githubusercontent.com/2ce7a83401591bde33f7e73d5c3602980223c0124a14d2188ea6615cd4ecdf7c/68747470733a2f2f7777772e636f6465666163746f722e696f2f7265706f7369746f72792f6769746875622f636865766572652f616374696f6e2f6261646765)](https://www.codefactor.io/repository/github/chevere/action)

Summary
-------

[](#summary)

**Chevere Action** is a PHP library that encapsulates application operations as reusable, self-validating objects. Built on the [Parameter](https://github.com/chevere/parameter) library, it implements the Action Design Pattern to enforce strict input/output validation at the class level, reducing runtime errors and improving code reliability with minimal boilerplate.

Installing
----------

[](#installing)

Action is available through [Packagist](https://packagist.org/packages/chevere/action) and the repository source is at [chevere/action](https://github.com/chevere/action).

```
composer require chevere/action
```

Quick Start
-----------

[](#quick-start)

### Creating an Action

[](#creating-an-action)

Create an Action by implementing the `ActionInterface` either by using `ActionTrait` or extending the `Action` class:

#### Using ActionTrait

[](#using-actiontrait)

```
use Chevere\Action\Interfaces\ActionInterface;
use Chevere\Action\Traits\ActionTrait;

class GetUserAction implements ActionInterface
{
    use ActionTrait;

    public function __invoke(int $userId): array
    {
        return ['id' => $userId, 'name' => 'John'];
    }
}
```

#### Extending Action

[](#extending-action)

```
use Chevere\Action\Action;

class GetUserAction extends Action
{
    public function __invoke(int $userId): array
    {
        return ['id' => $userId, 'name' => 'John'];
    }
}
```

### Adding Validation

[](#adding-validation)

Enhance Actions with input and output validation using attributes from the Parameter library:

```
use Chevere\Action\Action;
use Chevere\Parameter\Attributes\_arrayp;
use Chevere\Parameter\Attributes\_int;
use Chevere\Parameter\Attributes\_string;
use Chevere\Parameter\Attributes\_return;

class GetUserAction extends Action
{
    #[_return(
        new _arrayp(
            userId: new _int(min: 1),
            name: new _string(),
        )
    )]
    public function __invoke(
        #[_int(min: 1)]
        int $userId
    ): array {
        $this->assertArguments($userId);

        $result = ['id' => $userId, 'name' => 'John'];

        return $this->assertReturn($result);
    }
}
```

### Invoking an Action

[](#invoking-an-action)

Invoke an Action as you would a function:

```
$action = new GetUserAction();
$result = $action(1);  // or $action->__invoke(1)
```

Advanced Usage
--------------

[](#advanced-usage)

### Parameter Validation Methods

[](#parameter-validation-methods)

Define validation rules using dedicated methods for more control and flexibility. This is especially useful when validation rules cannot be expressed as attribute attributes (literal values only).

#### Using `acceptParameters()`

[](#using-acceptparameters)

Define input validation rules centrally:

```
use Chevere\Action\Action;
use Chevere\Parameter\Interfaces\ParametersInterface;
use function Chevere\Parameter\parameters;
use function Chevere\Parameter\string;
use function Chevere\Parameter\int;

class CreatePostAction extends Action
{
    public function __invoke(
        string $title,
        string $content,
        int $authorId
    ): array {
        $this->assertArguments($title, $content, $authorId);
        return ['id' => 1];
    }

    public static function acceptParameters(): ParametersInterface
    {
        return parameters(
            title: string(minLength: 5, maxLength: 200),
            content: string(minLength: 10),
            authorId: int(min: 1),
        );
    }
}
```

#### Using `acceptReturn()`

[](#using-acceptreturn)

Define output validation rules:

```
use Chevere\Action\Action;
use Chevere\Parameter\Interfaces\ParameterInterface;
use function Chevere\Parameter\arrayOf;
use function Chevere\Parameter\int;

class GetUserAction extends Action
{
    public function __invoke(int $userId): array
    {
        $this->assertArguments($userId);
        $result = ['id' => $userId, 'name' => 'John'];
        return $this->assertReturn($result);
    }

    public static function acceptReturn(): ParameterInterface
    {
        return arrayOf(
            int(key: 'id', min: 1),
            string(key: 'name', minLength: 1),
        );
    }
}
```

### Assertion Methods

[](#assertion-methods)

Control exactly when and how validations occur:

#### `assertArguments()`

[](#assertarguments)

Validate input against defined rules. Can be called with explicit arguments or automatically from the caller context:

```
// Automatic extraction from caller context
public function __invoke($foo, $bar) {
    $this->assertArguments();
}

// Explicit arguments
public function __invoke($foo, $bar) {
    $this->assertArguments($foo, $bar);
}

// Using get_defined_vars()
public function __invoke($foo, $bar) {
    $this->assertArguments(...get_defined_vars());
}
```

#### `assertReturn()`

[](#assertreturn)

Validate the return value against defined rules:

```
public function __invoke(int $id): array
{
    $result = $this->fetchUser($id);
    return $this->assertReturn($result);
}
```

#### `assert()`

[](#assert)

Validate runtime rule coherence. Useful for checking internal state:

```
public function __invoke(): void
{
    $this->setupDependencies();
    $this->assert();
}
```

### Advanced Rules

[](#advanced-rules)

#### Static Rules with `acceptRulesStatic()`

[](#static-rules-with-acceptrulesstatic)

Enforce design rules at class definition time. Called before any assertions:

```
public static function acceptRulesStatic(): void
{
    $reflection = static::reflection();

    // Enforce that parameter $password is never used
    if ($reflection->parameters()->has('password')) {
        throw new LogicException('Password parameter not allowed');
    }
}
```

#### Runtime Rules with `acceptRulesRuntime()`

[](#runtime-rules-with-acceptrulesruntime)

Enforce rules based on instance state. Called before assertions at invocation time:

```
private array $config = [];

public function acceptRulesRuntime(): void
{
    if (empty($this->config)) {
        throw new RuntimeException('Configuration required before invocation');
    }
}
```

### Action Setup Method

[](#action-setup-method)

Use `setUp()` to initialize an Action before invocation:

```
class SendEmailAction extends Action
{
    private SmtpConfig $smtpConfig;

    public function setUp(SmtpConfig $config): void
    {
        $this->smtpConfig = $config;
    }

    public function __invoke(string $email): bool
    {
        $this->acceptRulesRuntime();
        return mail($email, 'Hello', 'World');
    }

    public function acceptRulesRuntime(): void
    {
        if (!isset($this->smtpConfig)) {
            throw new RuntimeException('SMTP configuration required');
        }
    }
}

// Usage
$action = new SendEmailAction();
$action->setUp($smtpConfig);
$result = $action('user@example.com');
```

### ActionName

[](#actionname)

Store and manage Action instances with their setup arguments using `ActionName`:

```
use Chevere\Action\ActionName;

$actionName = new ActionName(
    SendEmailAction::class,
    $smtpConfig
);

// Later: reconstruct and invoke
$className = (string) $actionName;
$action = new $className();
$action->setUp(...$actionName->arguments());
$action('user@example.com');
```

Or use a factory method on your Action:

```
class SendEmailAction extends Action
{
    public static function configure(SmtpConfig $config): ActionNameInterface
    {
        return new ActionName(static::class, $config);
    }
}

// Usage
$configured = SendEmailAction::configure($smtpConfig);
$action = new (string)$configured();
$action->setUp(...$configured->arguments());
```

### Reflection Access

[](#reflection-access)

Access Action metadata using `reflection()`:

```
$action = new MyAction();

// Get parameter definitions
$parameters = $action::reflection()->parameters();

// Get return definition
$return = $action::reflection()->return();

// Check if parameter exists
if ($parameters->has('userId')) {
    // ...
}

// Iterate parameters
foreach ($parameters as $name => $parameter) {
    echo $name . ': ' . get_class($parameter);
}
```

The `Controller` is a specialized Action for handling command-like instructions. Unlike standard Actions that accept any type of parameters, Controllers restrict parameters to scalar types: `string`, `int`, and `float`.

Controllers are ideal for handling user input from APIs, CLI commands, or form submissions where all arguments arrive as scalar values.

### Creating a Controller

[](#creating-a-controller)

Extend the `Controller` class to create a compliant Controller:

```
use Chevere\Action\Controller;
use Chevere\Parameter\Interfaces\ParametersInterface;
use function Chevere\Parameter\parameters;
use function Chevere\Parameter\string;
use function Chevere\Parameter\int;

class CreatePostController extends Controller
{
    public function __invoke(
        string $title,
        string $content,
        int $authorId
    ): array {
        $this->assertArguments($title, $content, $authorId);
        return [
            'id' => 1,
            'title' => $title,
            'author' => $authorId
        ];
    }

    public static function acceptParameters(): ParametersInterface
    {
        return parameters(
            title: string(minLength: 5, maxLength: 200),
            content: string(minLength: 10),
            authorId: int(min: 1),
        );
    }
}
```

### Usage Example

[](#usage-example)

```
$controller = new CreatePostController();
$result = $controller(
    title: 'My First Post',
    content: 'This is great content.',
    authorId: 42
);
// Returns: ['id' => 1, 'title' => 'My First Post', 'author' => 42]
```

**Note:** Controllers enforce type validation at the class level. Any parameter with a type other than `string|int|float` will trigger a validation error during class initialization.

Documentation
-------------

[](#documentation)

Documentation is available at [chevere.org/packages/action](https://chevere.org/packages/action).

License
-------

[](#license)

Copyright [Rodolfo Berrios A.](https://rodolfoberrios.com/)

Chevere is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance88

Actively maintained with recent releases

Popularity27

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~60 days

Recently: every ~37 days

Total

14

Last Release

79d ago

Major Versions

1.1.x-dev → 2.0.02025-09-12

2.0.x-dev → 3.0.02026-02-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/a87fbbcb1518fe64878bc53dedd9f61b81243cbb21b73c26f5e299d501375984?d=identicon)[rodolfo](/maintainers/rodolfo)

---

Top Contributors

[![rodber](https://avatars.githubusercontent.com/u/20590102?v=4)](https://github.com/rodber "rodber (115 commits)")

---

Tags

cheverephpphp-library

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/chevere-action/health.svg)

```
[![Health](https://phpackages.com/badges/chevere-action/health.svg)](https://phpackages.com/packages/chevere-action)
```

###  Alternatives

[xrdebug/php

PHP client library for xrDebug

23920.3k2](/packages/xrdebug-php)[chevere/chevere

High quality library for building modern PHP

9718.9k4](/packages/chevere-chevere)[chevere/workflow

Declarative workflow engine for PHP with automatic dependency resolution, sync/async job execution, and type-safe response chaining.

1111.8k1](/packages/chevere-workflow)[chevere/parameter

Dynamic parameter-argument validation for PHP with rich type constraints, attributes, and schema introspection.

1023.7k20](/packages/chevere-parameter)[chevere/var-dump

A multi-purpose colorful modern alternative to var\_dump

3739.7k16](/packages/chevere-var-dump)[chevere/throwable-handler

Throwable handler for multiple contexts

1131.9k9](/packages/chevere-throwable-handler)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
