nepada/phpstan-message-bus

PHPStan extension for nepada/message-bus.

Installs: 1 425

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 0

Open Issues: 0

Type:phpstan-extension

v3.2.0 2024-04-01 22:41 UTC

This package is auto-updated.

Last update: 2024-04-06 17:43:20 UTC


README

Build Status Coverage Status Downloads this Month Latest stable

Installation

Via Composer:

composer require --dev nepada/phpstan-mesasge-bus

Unless you also install phpstan/extension-installer you need to manually enable the extension in your config:

includes:
    - vendor/nepada/phpstan-message-bus/extension.neon

Either way, you need to specify the directories in which your command handlers are located:

parameters:
    commandHandlerDirectories:
        - app
        - src

Description

The package currently provides only one extension - DynamicMethodThrowTypeExtension. The extension propagates exception thrown by command handlers up to the command bus caller.

final class FooService
{

    private \Nepada\MessageBus\Commands\CommandBus $commandBus;

    public function __construct(\Nepada\MessageBus\Commands\CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }

    public function placeOrder(): void
    {
        try {
            $command = new PlaceOrderCommand();
            $this->commandBus->handle($command);
        } catch (FailedToPlaceOrderException $exception) {
            // FailedToPlaceOrderException may be thrown and needs to handled
        }
    }

}

final class PlaceOrderCommand implements \Nepada\MessageBus\Commands\Command
{

}

final class PlaceOrderHandler implements \Nepada\MessageBus\Commands\CommandHandler
{

    /**
     * @param PlaceOrderCommand $command
     * @throws FailedToPlaceOrderException
     */
    public function __invoke(PlaceOrderCommand $command): void
    {
        throw new FailedToPlaceOrderException('Failed to place order');
    }

}

class FailedToPlaceOrderException extends \RuntimeException
{

}