Skip to content
This repository has been archived by the owner on May 6, 2018. It is now read-only.

Commit

Permalink
Use Doctrine CS with some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Feb 6, 2018
1 parent 23008a2 commit 40d3c99
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 22 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"league/tactician": "^1.0"
},
"require-dev": {
"doctrine/coding-standard": "^3.0",
"infection/infection": "^0.7",
"league/tactician-container": "^2.0",
"phpstan/phpstan": "^0.9",
Expand Down
17 changes: 7 additions & 10 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@
<file>src</file>
<file>tests</file>

<rule ref="PSR2"/>
<rule ref="Doctrine" />

<rule ref="Generic.Formatting.MultipleStatementAlignment">
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="error" value="true"/>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="1"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
<property name="newlinesCountAfterDeclare" value="2"/>
</properties>
</rule>

<rule ref="Generic.Formatting.SpaceAfterCast" />
<rule ref="Generic.Formatting.SpaceAfterNot" />
<rule ref="Generic.Arrays.DisallowLongArraySyntax" />

<rule ref="Squiz.Strings.ConcatenationSpacing">
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing">
<properties>
<property name="spacing" value="1"/>
<property name="ignoreNewlines" value="true"/>
<property name="spacesCountBeforeColon" value="0"/>
</properties>
</rule>
</ruleset>
50 changes: 48 additions & 2 deletions src/DependencyInjection/RegisterServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use function array_combine;
use function array_map;
use function array_values;
use function krsort;
use function sprintf;
use function uniqid;

final class RegisterServices implements CompilerPassInterface
{
Expand All @@ -43,10 +49,13 @@ final class RegisterServices implements CompilerPassInterface
private $queryBusId;

/**
* @var array
* @var mixed[]
*/
private $dependencies;

/**
* @param mixed[] $dependencies
*/
public function __construct(
string $commandBusId,
string $queryBusId,
Expand All @@ -57,7 +66,7 @@ public function __construct(
$this->dependencies = $dependencies;
}

public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
$dependencies = $this->extractDependencies($container);
$parsedHandlers = $this->extractHandlers($container);
Expand All @@ -83,6 +92,9 @@ public function process(ContainerBuilder $container)
);
}

/**
* @return Reference[]
*/
private function extractDependencies(ContainerBuilder $container): array
{
$services = [];
Expand Down Expand Up @@ -159,6 +171,11 @@ private function extractHandlers(ContainerBuilder $container): array
return $handlers;
}

/**
* @param string[][] $handlers
*
* @return string[][]
*/
private function appendHandler(array $handlers, string $busId, string $message, string $serviceId): array
{
$handlers[$busId] = $handlers[$busId] ?? [];
Expand Down Expand Up @@ -196,6 +213,11 @@ private function extractMiddlewares(ContainerBuilder $container): array
return $middlewares;
}

/**
* @param Reference[][][] $middlewares
*
* @return Reference[][][]
*/
private function appendMiddleware(array $middlewares, string $busId, int $priority, string $serviceId): array
{
$middlewares[$busId] = $middlewares[$busId] ?? [];
Expand Down Expand Up @@ -225,6 +247,10 @@ private function prioritiseMiddlewares(array $middlewares): array
return $prioritised;
}

/**
* @param string[] $handlers
* @param Reference[] $middlewares
*/
private function registerCommandBus(
ContainerBuilder $container,
Reference $messageCreator,
Expand All @@ -248,6 +274,10 @@ private function registerCommandBus(
);
}

/**
* @param string[] $handlers
* @param Reference[] $middlewares
*/
private function registerQueryBus(
ContainerBuilder $container,
Reference $messageCreator,
Expand Down Expand Up @@ -286,6 +316,10 @@ private function registerReadModelConverter(ContainerBuilder $container, Referen
return new Reference($readModelConversionId);
}

/**
* @param string[] $handlers
* @param Reference[] $middlewares
*/
private function registerTacticianBus(
string $id,
ContainerBuilder $container,
Expand All @@ -312,6 +346,9 @@ private function registerTacticianBus(
return new Reference($id);
}

/**
* @param string[] $handlers
*/
private function registerTacticianHandler(
ContainerBuilder $container,
string $busId,
Expand All @@ -335,6 +372,9 @@ private function registerTacticianHandler(
return new Reference($id);
}

/**
* @param string[] $handlers
*/
private function registerTacticianLocator(
ContainerBuilder $container,
string $handlerId,
Expand All @@ -353,6 +393,9 @@ private function registerTacticianLocator(
return new Reference($id);
}

/**
* @param string[] $handlers
*/
private function registerServiceLocator(ContainerBuilder $container, array $handlers): Reference
{
$serviceIds = array_values($handlers);
Expand All @@ -368,6 +411,9 @@ function (string $id): Reference {
);
}

/**
* @param mixed[] $arguments
*/
private function createService(string $class, array $arguments = []): Definition
{
return (new Definition($class, $arguments))->setPublic(false);
Expand Down
5 changes: 5 additions & 0 deletions src/Middleware/ReadModelConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function __construct(ReadModelConverter $converter)
$this->converter = $converter;
}

/**
* @param object|mixed $query
*
* @return mixed
*/
public function execute($query, callable $next)
{
$result = $next($query);
Expand Down
3 changes: 3 additions & 0 deletions src/QueryBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function __construct(
$this->messageCreator = $messageCreator;
}

/**
* @return mixed
*/
public function handle(string $query, ServerRequestInterface $request)
{
return $this->serviceBus->handle(
Expand Down
42 changes: 35 additions & 7 deletions tests/DependencyInjection/RegisterServicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
use Lcobucci\Chimera\ReadModelConverter;
use League\Tactician\Middleware;
use League\Tactician\Plugins\NamedCommand\NamedCommandExtractor;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use function array_filter;
use function current;
use function end;
use function preg_match;

final class RegisterServicesTest extends \PHPUnit\Framework\TestCase
final class RegisterServicesTest extends TestCase
{
private const DEFAULT_MIDDLEWARES_PATTERN = '/^(chimera\.(read_model_conversion|bus_internal)\..*|'
. '.*\.inner\.handler)$/';
Expand Down Expand Up @@ -63,6 +68,9 @@ public function processShouldThrowAnExceptionIfAHandlerIsNotTaggedToHandleSometh
);
}

/**
* @return string[]
*/
public function handlerTags(): array
{
return [
Expand Down Expand Up @@ -95,6 +103,10 @@ public function processShouldCreateCommandAndQueryBusesWithDefaultHandlersConnec
*
* @dataProvider possibleHandlerTags
*
* @param mixed[] $tags
* @param string[] $expectedCommandHandlers
* @param string[] $expectedQueryHandlers
*
* @covers \Lcobucci\Chimera\Bus\Tactician\DependencyInjection\RegisterServices
*/
public function processShouldCreateCommandAndQueryBusesWhichAreConnectedToTheTaggedHandlers(
Expand All @@ -121,6 +133,9 @@ public function processShouldCreateCommandAndQueryBusesWhichAreConnectedToTheTag
$this->assertSameHandlers($container, self::QUERY_BUS, $expectedQueryHandlers);
}

/**
* @return mixed[]
*/
public function possibleHandlerTags(): array
{
return [
Expand Down Expand Up @@ -237,7 +252,7 @@ public function processShouldAlwaysCreateCommandAndQueryBusesWithAPrioritizedLis
}

/**
* @dataProvider
* @return mixed[]
*/
public function provideOverridableDependencies(): array
{
Expand All @@ -246,26 +261,26 @@ public function provideOverridableDependencies(): array
self::COMMAND_BUS,
'method_name_inflector',
'inflector',
2
2,
],
'query bus with overridden inflector' => [
self::QUERY_BUS,
'method_name_inflector',
'inflector',
2
2,
],
'command bus with overridden extractor' => [
self::COMMAND_BUS,
'class_name_extractor',
'extractor',
0
0,
],
'query bus with overridden extractor' => [
self::QUERY_BUS,
'class_name_extractor',
'extractor',
0
]
0,
],
];
}

Expand Down Expand Up @@ -333,6 +348,10 @@ public function processShouldCreateCommandAndQueryBusesThatUseOverriddenMessageC
);
}

/**
* @param mixed[] $dependencies
* @param Definition[] $definitions
*/
public function processCompilerPass(
string $commandBusId,
string $queryBusId,
Expand All @@ -348,6 +367,9 @@ public function processCompilerPass(
return $container;
}

/**
* @param string[] $expectedMiddlewares
*/
private function assertSameDeclaredMiddlewares(
ContainerBuilder $container,
string $busId,
Expand All @@ -366,6 +388,9 @@ function (Reference $reference): bool {
self::assertEquals($expectedMiddlewares, $middlewareList);
}

/**
* @param string[] $handlerMap
*/
private function assertSameHandlers(ContainerBuilder $container, string $bus, array $handlerMap = []): void
{
$handlerLocator = $container->getDefinition(
Expand Down Expand Up @@ -405,6 +430,9 @@ private function getQueryBusConverter(ContainerBuilder $container, string $bus):
);
}

/**
* @return string[]
*/
private function getBusMiddlewares(ContainerBuilder $container, string $bus): array
{
$internalBus = $container->getDefinition(
Expand Down
10 changes: 8 additions & 2 deletions tests/MessageBusTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use Lcobucci\Chimera\MessageCreator;
use League\Tactician\CommandBus as ServiceBus;
use League\Tactician\Middleware;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;

abstract class MessageBusTestCase extends \PHPUnit\Framework\TestCase
abstract class MessageBusTestCase extends TestCase
{
protected function createMessageCreator(
string $message,
ServerRequestInterface $request,
$createdObject
object $createdObject
): MessageCreator {
$messageCreator = $this->createMock(MessageCreator::class);

Expand Down Expand Up @@ -41,6 +42,11 @@ public function __construct(callable $callback)
$this->callback = $callback;
}

/**
* @param object|mixed $command
*
* @return mixed
*/
public function execute($command, callable $next)
{
return ($this->callback)($command);
Expand Down
3 changes: 2 additions & 1 deletion tests/Middleware/ReadModelConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use Lcobucci\Chimera\Bus\Tactician\Middleware\ReadModelConversion;
use Lcobucci\Chimera\Bus\Tactician\Tests\FetchById;
use Lcobucci\Chimera\ReadModelConverter;
use PHPUnit\Framework\TestCase;

final class ReadModelConversionTest extends \PHPUnit\Framework\TestCase
final class ReadModelConversionTest extends TestCase
{
/**
* @test
Expand Down
3 changes: 3 additions & 0 deletions tests/QueryBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function (): void {
);
}

/**
* @return mixed
*/
private function handle(callable $handler)
{
$request = $this->createMock(ServerRequestInterface::class);
Expand Down

0 comments on commit 40d3c99

Please sign in to comment.