nepada/phpstan-message-bus

PHPStan extension for nepada/message-bus.

Installs: 436

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

Open Issues: 0

Type:phpstan-extension

v2.1.0 2020-11-13 16:50 UTC

README

Build 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
{

}