nepada/phpstan-message-bus

PHPStan extension for nepada/message-bus.

Installs: 1 573

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 0

Open Issues: 0

Type:phpstan-extension

v4.0.0 2024-11-11 18:27 UTC

This package is auto-updated.

Last update: 2024-11-11 18:30:24 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
{

}