Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ jobs:
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer require "phpunit/phpunit:${{ matrix.phpunit }}" --no-interaction --no-update
composer require "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer require "nunomaduro/larastan:${{ matrix.larastan }}" --no-interaction --no-update
composer require "larastan/larastan:${{ matrix.larastan }}" --no-interaction --no-update
composer update --prefer-dist --no-interaction
composer dump

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"require-dev": {
"laravel/pint": "^1.2",
"nunomaduro/larastan": "^2.0",
"larastan/larastan": "^2.0",
"orchestra/testbench": "^7.11.0",
"phpunit/phpunit": "^9.5.10"
},
Expand All @@ -41,13 +41,16 @@
},
"scripts": {
"larastan": [
"./vendor/bin/phpstan analyse"
"./vendor/bin/phpstan analyse src"
],
"pint": [
"./vendor/bin/pint"
],
"pint-check": [
"./vendor/bin/pint --test"
],
"test": [
"./vendor/bin/phpunit tests"
]
},
"config": {
Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
includes:
- vendor/larastan/larastan/extension.neon

parameters:
paths:
- config
- src

level: max

checkMissingIterableValueType: true
treatPhpDocTypesAsCertain: false
55 changes: 32 additions & 23 deletions src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirschbaum\Actions;

use Exception;
use Illuminate\Support\Traits\Macroable;
use Kirschbaum\Actions\Contracts\Actionable;
use Kirschbaum\Actions\Exceptions\ActionableInterfaceNotFoundException;
Expand All @@ -13,6 +14,8 @@ class Action

/**
* Arguments to pass into the action's constructor.
*
* @var array<int|string, mixed>
*/
protected array $arguments;

Expand All @@ -21,7 +24,7 @@ class Action
*
* @param mixed ...$arguments
*
* @return mixed
* @return mixed|void
*
* @throws Throwable
*/
Expand All @@ -35,6 +38,9 @@ public function act(string $action, ...$arguments)
/**
* Initiate the given action if the given condition is true.
*
* @template TValue
*
* @param TValue $condition
* @param mixed ...$arguments
*
* @return mixed|void
Expand All @@ -53,6 +59,9 @@ public function actWhen($condition, string $action, ...$arguments)
/**
* Initiate the action if the given condition is false.
*
* @template TValue
*
* @param TValue $condition
* @param mixed ...$arguments
*
* @return mixed|void
Expand Down Expand Up @@ -80,7 +89,11 @@ protected function handle(string $action)
{
$action = new $action(...$this->arguments);

$this->checkActionForInterface($action);
throw_unless(
$action instanceof Actionable,
ActionableInterfaceNotFoundException::class
);

$this->raiseBeforeActionEvent($action);

try {
Expand All @@ -103,20 +116,7 @@ protected function actionHasFailedMethod(Actionable $action): bool
}

/**
* Determine if the action has the proper interface.
*
* @throws Throwable
*/
protected function checkActionForInterface($action): void
{
throw_unless(
$action instanceof Actionable,
ActionableInterfaceNotFoundException::class
);
}

/**
* Dispatch appropriate action event.
* Dispatch the appropriate action event.
*/
protected function dispatchEvent(string $event, Actionable $action): void
{
Expand All @@ -140,19 +140,28 @@ protected function eventExists(Actionable $action, string $event): bool
/**
* Fire failure event and/or call failed action method if they exist.
*
*
* @return mixed
* @return mixed|void
*
* @throws Throwable
*/
protected function handleFailure(Actionable $action, Throwable $exception)
{
if ($this->actionHasFailedMethod($action)) {
return $action->failed($exception);
/**
* @var callable $callback
*/
$callback = [$action, 'failed'];

return call_user_func($callback, $exception);
}

if ($this->hasCustomException($action)) {
$exception = $action->exception;
$properties = get_object_vars($action);

/**
* @var Exception $exception
*/
$exception = $properties['exception'];

throw new $exception();
}
Expand All @@ -161,7 +170,7 @@ protected function handleFailure(Actionable $action, Throwable $exception)
}

/**
* Check if action has a custom exception.
* Check if the action has a custom exception.
*/
protected function hasCustomException(Actionable $action): bool
{
Expand All @@ -170,15 +179,15 @@ protected function hasCustomException(Actionable $action): bool
}

/**
* Raise the before action event.
* Raise the "before" action event.
*/
protected function raiseBeforeActionEvent(Actionable $action): void
{
$this->dispatchEvent('before', $action);
}

/**
* Raise the after action event.
* Raise the "after" action event.
*/
protected function raiseAfterActionEvent(Actionable $action): void
{
Expand Down
27 changes: 21 additions & 6 deletions src/ActionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
class ActionsServiceProvider extends ServiceProvider
{
/**
* All of the container bindings that should be registered.
* All the container bindings that should be registered.
*
* @var array
* @var array<class-string<Action>|string,class-string<Action>>
*/
public $bindings = [
public array $bindings = [
'actions' => Action::class,
Action::class => Action::class,
];
Expand Down Expand Up @@ -49,6 +49,8 @@ public function boot(): void

/**
* Get the services provided by the provider.
*
* @return list<class-string<Action>>
*/
public function provides(): array
{
Expand All @@ -61,6 +63,9 @@ public function provides(): array
protected function bootActionMacro(): void
{
Action::macro('getMacro', function (string $name): callable|object {
/**
* @phpstan-ignore-next-line
*/
return static::$macros[$name];
});
}
Expand All @@ -72,17 +77,27 @@ protected function bootActionMacro(): void
*/
protected function bootAutoDiscoverActions(): void
{
$paths = collect(config('laravel-actions.paths'))
/**
* @var list<string> $configPaths
*/
$configPaths = config('laravel-actions.paths');

$paths = collect($configPaths)
->unique()
->filter(function ($path) {
->filter(function (string $path): bool {
return is_dir($path);
});

if ($paths->isEmpty()) {
return;
}

foreach ((new Finder())->in($paths->toArray())->files() as $action) {
/**
* @var list<string> $dirs
*/
$dirs = $paths->toArray();

foreach ((new Finder())->in($dirs)->files() as $action) {
if (preg_match('#(namespace)(\\s+)([A-Za-z0-9\\\\]+?)(\\s*);#sm', $action->getContents(), $namespaceMatches)) {
$action = (string) Str::of($namespaceMatches[3])
->finish('\\')
Expand Down
12 changes: 2 additions & 10 deletions src/Commands/stubs/Action.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@ class DummyClass implements Actionable

/**
* Event to dispatch before action starts.
*
* @var string
*/
public $before = '';
public string $before = '';

/**
* Event to dispatch after action completes.
*
* @var string
*/
public $after = '';
public string $after = '';

/**
* Create a new action instance.
*
* @return void
*/
public function __construct()
{
Expand All @@ -35,8 +29,6 @@ class DummyClass implements Actionable

/**
* Execute the action.
*
* @return mixed
*/
public function __invoke()
{
Expand Down
1 change: 0 additions & 1 deletion src/Traits/CanAct.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ trait CanAct
/**
* Handles static method calls by passing them to the Action class.
*
*
* @return mixed|void
*/
public static function __callStatic(string $name, array $arguments)
Expand Down
12 changes: 9 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @param mixed ...$arguments
*
* @return mixed
* @return mixed|void
*
* @throws Throwable
*/
Expand All @@ -22,9 +22,12 @@ function act(string $action, ...$arguments)
/**
* Initiate the given action if the given condition is true.
*
* @template TValue
*
* @param TValue $condition
* @param mixed ...$arguments
*
* @return mixed
* @return mixed|void
*
* @throws Throwable
*/
Expand All @@ -38,9 +41,12 @@ function act_when($condition, string $action, ...$arguments)
/**
* Initiate the given action if the given condition is false.
*
* @template TValue
*
* @param TValue $condition
* @param mixed ...$arguments
*
* @return mixed
* @return mixed|void
*
* @throws Throwable
*/
Expand Down
15 changes: 3 additions & 12 deletions tests/Fixtures/Actions/ActionWithAllEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,25 @@
use Kirschbaum\Actions\Traits\CanAct;
use Tests\Fixtures\Events\AfterEvent;
use Tests\Fixtures\Events\BeforeEvent;
use Throwable;

class ActionWithAllEvents implements Actionable
{
use CanAct;

/**
* Event to dispatch before action starts.
*
* @var string
*/
public $before = BeforeEvent::class;
public string $before = BeforeEvent::class;

/**
* Event to dispatch after action completes.
*
* @var string
*/
public $after = AfterEvent::class;
public string $after = AfterEvent::class;

/**
* Execute the action.
*
* @return mixed
*
* @throws Throwable
*/
public function __invoke()
public function __invoke(): bool
{
return true;
}
Expand Down
6 changes: 1 addition & 5 deletions tests/Fixtures/Actions/ActionWithCustomException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ class ActionWithCustomException implements Actionable

/**
* Event to dispatch if action throws an exception.
*
* @var string
*/
public $exception = CustomFailedException::class;
public string $exception = CustomFailedException::class;

/**
* Execute the action.
*
* @return mixed
*
* @throws Throwable
*/
public function __invoke()
Expand Down
2 changes: 0 additions & 2 deletions tests/Fixtures/Actions/ActionWithException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class ActionWithException implements Actionable
/**
* Execute the action.
*
* @return mixed
*
* @throws Throwable
*/
public function __invoke()
Expand Down
Loading