Skip to content

Commit

Permalink
Make command bus method configurable
Browse files Browse the repository at this point in the history
Now the command bus doesn't need a method named handle but can use
anything it likes.
  • Loading branch information
rosstuck committed Sep 18, 2019
1 parent 558002b commit cb356e8
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ And you're good to go!

It's very common to have a bridge interface from your application to any external packages, including Tactician. In that case, you'll have a different command bus class and Tactician-PHPStan won't catch errors because it's looking for usages of the `League\Tactician\CommandBus`.

Instead, you can configure the command bus class to scan for:
Instead, you can configure the command bus class to scan for, as well as (optionally) the method to use:

~~~
# phpstan.neon
parameters:
tactician:
bootstrap: handler-mapping-loader.php
bus: My\App\CommandBus
method: execute
~~~

If neither is specified, the default class is `League\Tactician\CommandBus` and a method named `handle`.

## Testing
To run all unit tests, use the locally installed PHPUnit:

Expand Down
3 changes: 3 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
parameters:
tactician:
bus: League\Tactician\CommandBus
method: handle

services:
-
class: League\Tactician\PHPStan\TacticianRuleSet
arguments:
commandBusClass: %tactician.bus%
commandBusMethod: %tactician.method%
tags:
- phpstan.rules.rule

-
class: League\Tactician\PHPStan\HandlerReturnTypeExtension
arguments:
commandBusClass: %tactician.bus%
commandBusMethod: %tactician.method%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

Expand Down
9 changes: 7 additions & 2 deletions src/HandlerReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ final class HandlerReturnTypeExtension implements DynamicMethodReturnTypeExtensi
* @var string
*/
private $commandBusClass;
/**
* @var string
*/
private $commandBusMethod;

public function __construct(CommandToHandlerMapping $mapping, string $commandBusClass)
public function __construct(CommandToHandlerMapping $mapping, string $commandBusClass, string $commandBusMethod)
{
$this->mapping = $mapping;
$this->commandBusClass = $commandBusClass;
$this->commandBusMethod = $commandBusMethod;
}

public function setBroker(Broker $broker): void
Expand All @@ -50,7 +55,7 @@ public function getClass(): string

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'handle';
return $methodReflection->getName() === $this->commandBusMethod;
}

public function getTypeFromMethodCall(
Expand Down
9 changes: 7 additions & 2 deletions src/TacticianRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ final class TacticianRuleSet implements Rule
* @var string
*/
private $commandBusClass;
/**
* @var string
*/
private $commandBusMethod;

public function __construct(CommandToHandlerMapping $mapping, Broker $broker, string $commandBusClass)
public function __construct(CommandToHandlerMapping $mapping, Broker $broker, string $commandBusClass, string $commandBusMethod)
{
$this->mapping = $mapping;
$this->broker = $broker;
$this->commandBusClass = $commandBusClass;
$this->commandBusMethod = $commandBusMethod;
}

public function getNodeType(): string
Expand All @@ -50,7 +55,7 @@ public function processNode(Node $methodCall, Scope $scope): array
{
if (! $methodCall instanceof MethodCall
|| ! $methodCall->name instanceof Node\Identifier
|| $methodCall->name->name !== 'handle') {
|| $methodCall->name->name !== $this->commandBusMethod) {
return [];
}

Expand Down
3 changes: 2 additions & 1 deletion tests/TacticianRuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ protected function getRule(): Rule
new Handle()
),
$this->createBroker(),
CommandBus::class
CommandBus::class,
'handle'
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/data/MissingHandlerClassForAlternateBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class SomeCommand

class DerpBus
{
public function handle(object $command): void
public function execute(object $command): void
{

}
}

$commandBus = new DerpBus();
$commandBus->handle(new SomeCommand());
$commandBus->execute(new SomeCommand());
4 changes: 2 additions & 2 deletions tests/data/UnionAndIntersectionClassesBehaveProperly.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class SomeTest extends TestCase

class DerpBus
{
public function handle(object $command): void
public function execute(object $command): void
{

}
}
$commandBus->expects(TestCase::once())->method('handle') ;
$commandBus->expects(TestCase::once())->method('execute') ;
1 change: 1 addition & 0 deletions tests/phpstan-alternate-class.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ parameters:
tactician:
bootstrap: tests/handler-mapping-loader.php
bus: MissingHandlerClassForAlternateBus\DerpBus
method: execute

0 comments on commit cb356e8

Please sign in to comment.