Skip to content

Commit

Permalink
Update SA
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBadura committed Feb 27, 2024
1 parent 8bd3b44 commit 0dd6401
Show file tree
Hide file tree
Showing 20 changed files with 62 additions and 93 deletions.
14 changes: 2 additions & 12 deletions baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.22.1@e9dad66e11274315dac27e08349c628c7d6a1a43">
<files psalm-version="5.22.2@d768d914152dbbf3486c36398802f74e80cfde48">
<file src="src/Aggregate/AggregateRootBehaviour.php">
<UnsafeInstantiation>
<code><![CDATA[new static()]]></code>
Expand All @@ -20,11 +20,6 @@
<code><![CDATA[$method->getName()]]></code>
</MixedMethodCall>
</file>
<file src="src/EventBus/Serializer/EventSerializerMessageSerializer.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$data['headers']]]></code>
</MixedArgumentTypeCoercion>
</file>
<file src="src/Metadata/AggregateRoot/AggregateRootMetadataAwareMetadataFactory.php">
<InvalidReturnStatement>
<code><![CDATA[$aggregate::metadata()]]></code>
Expand Down Expand Up @@ -83,15 +78,10 @@
<code><![CDATA[$index]]></code>
</InvalidPropertyAssignmentValue>
</file>
<file src="src/Store/DoctrineDbalStoreStream.php">
<ArgumentTypeCoercion>
<code><![CDATA[DoctrineHelper::normalizeCustomHeaders($data['custom_headers'], $platform)]]></code>
</ArgumentTypeCoercion>
</file>
<file src="src/Store/DoctrineHelper.php">
<MixedReturnTypeCoercion>
<code><![CDATA[$normalizedCustomHeaders]]></code>
<code><![CDATA[array<string, mixed>]]></code>
<code><![CDATA[array<array{name: string, payload: string}>]]></code>
</MixedReturnTypeCoercion>
</file>
<file src="tests/Benchmark/BasicImplementation/Aggregate/Profile.php">
Expand Down
4 changes: 1 addition & 3 deletions src/Aggregate/AggregateHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use Patchlevel\EventSourcing\EventBus\Header;
use Patchlevel\Hydrator\Normalizer\DateTimeImmutableNormalizer;

/**
* @psalm-immutable
*/
/** @psalm-immutable */
#[\Patchlevel\EventSourcing\Attribute\Header('aggregate')]
final class AggregateHeader implements Header
{
Expand Down
2 changes: 0 additions & 2 deletions src/Console/OutputStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Patchlevel\EventSourcing\Console;

use DateTimeInterface;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Serializer\Encoder\Encoder;
use Patchlevel\EventSourcing\Serializer\EventSerializer;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

use function array_keys;
use function array_values;
use function sprintf;

final class OutputStyle extends SymfonyStyle
Expand Down
4 changes: 1 addition & 3 deletions src/EventBus/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Patchlevel\EventSourcing\EventBus;

/**
* @psalm-immutable
*/
/** @psalm-immutable */
interface Header
{
}
3 changes: 3 additions & 0 deletions src/EventBus/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Patchlevel\EventSourcing\EventBus;

use function array_key_exists;
use function array_values;
use function assert;
use function is_a;

/**
* @template-covariant T of object
Expand Down
16 changes: 11 additions & 5 deletions src/EventBus/Serializer/DefaultHeadersSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
namespace Patchlevel\EventSourcing\EventBus\Serializer;

use Patchlevel\EventSourcing\EventBus\Header;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Metadata\Message\AttributeMessageHeaderRegistryFactory;
use Patchlevel\EventSourcing\Metadata\Message\MessageHeaderRegistry;
use Patchlevel\EventSourcing\Serializer\Encoder\Encoder;
use Patchlevel\EventSourcing\Serializer\Encoder\JsonEncoder;
use Patchlevel\EventSourcing\Serializer\SerializedEvent;

use Patchlevel\Hydrator\Hydrator;
use Patchlevel\Hydrator\MetadataHydrator;
use function is_array;

final class DefaultHeadersSerializer implements HeadersSerializer
{
Expand All @@ -25,19 +21,29 @@ public function __construct(
) {
}

/**
* @param list<Header> $headers
*
* @return array<SerializedHeader>
*/
public function serialize(array $headers): array
{
$serializedHeaders = [];
foreach ($headers as $header) {
$serializedHeaders[] = new SerializedHeader(
$this->messageHeaderRegistry->headerName($header::class),
$this->encoder->encode($this->hydrator->extract($header))
$this->encoder->encode($this->hydrator->extract($header)),
);
}

return $serializedHeaders;
}

/**
* @param array<SerializedHeader> $serializedHeaders
*
* @return list<Header>
*/
public function deserialize(array $serializedHeaders): array
{
$headers = [];
Expand Down
16 changes: 4 additions & 12 deletions src/EventBus/Serializer/EventSerializerMessageSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@

namespace Patchlevel\EventSourcing\EventBus\Serializer;

use DateTimeImmutable;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Metadata\Message\MessageHeaderRegistry;
use Patchlevel\EventSourcing\Serializer\Encoder\Encoder;
use Patchlevel\EventSourcing\Serializer\EventSerializer;
use Patchlevel\EventSourcing\Serializer\SerializedEvent;

use Patchlevel\Hydrator\Hydrator;
use function base64_decode;
use function base64_encode;
use function array_map;
use function is_array;
use function is_string;
use function serialize;
use function unserialize;

/**
* @psalm-type EncodedData array{serializedEvent: array{name: string, payload: string}, headers: array{name: string, payload: string}}
*/
/** @psalm-type EncodedData array{serializedEvent: array{name: string, payload: string}, headers: array{name: string, payload: string}} */
final class EventSerializerMessageSerializer implements MessageSerializer
{
public function __construct(
Expand Down Expand Up @@ -62,8 +54,8 @@ public function deserialize(string $content): Message
$event = $this->eventSerializer->deserialize(new SerializedEvent($messageData['serializedEvent']['name'], $messageData['serializedEvent']['payload']));
$headers = $this->headersSerializer->deserialize(array_map(
/** @param array{name: string, payload: string} $headerData */
fn (array $headerData) => new SerializedHeader($headerData['name'], $headerData['payload']),
$messageData['headers']
static fn (array $headerData) => new SerializedHeader($headerData['name'], $headerData['payload']),
$messageData['headers'],
));

return Message::createWithHeaders($event, $headers);
Expand Down
2 changes: 2 additions & 0 deletions src/EventBus/Serializer/HeadersSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ interface HeadersSerializer
{
/**
* @param list<Header> $headers
*
* @return array<SerializedHeader>
*/
public function serialize(array $headers): array;

/**
* @param array<SerializedHeader> $serializedHeaders
*
* @return list<Header>
*/
public function deserialize(array $serializedHeaders): array;
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Message/MessageHeaderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Patchlevel\EventSourcing\Metadata\Message;

use Patchlevel\EventSourcing\EventBus\Header;

use function array_flip;
use function array_key_exists;

Expand Down
4 changes: 1 addition & 3 deletions src/Outbox/OutboxHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Patchlevel\EventSourcing\EventBus\Header;

/**
* @psalm-immutable
*/
/** @psalm-immutable */
#[\Patchlevel\EventSourcing\Attribute\Header('outbox')]
final class OutboxHeader implements Header
{
Expand Down
4 changes: 1 addition & 3 deletions src/Store/ArchivedHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Patchlevel\EventSourcing\EventBus\Header;

/**
* @psalm-immutable
*/
/** @psalm-immutable */
#[\Patchlevel\EventSourcing\Attribute\Header('archived')]
final class ArchivedHeader implements Header
{
Expand Down
23 changes: 12 additions & 11 deletions src/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,31 @@
use Patchlevel\EventSourcing\Serializer\EventSerializer;

use function array_fill;
use function array_filter;
use function array_values;
use function count;
use function implode;
use function in_array;
use function is_int;
use function is_string;
use function sprintf;

final class DoctrineDbalStore implements Store, ArchivableStore, SchemaConfigurator
{
public function __construct(
private readonly Connection $connection,
private readonly Connection $connection,
private readonly EventSerializer $eventSerializer,
private readonly HeadersSerializer $headersSerializer,
private readonly string $storeTableName = 'eventstore',
)
{
private readonly string $storeTableName = 'eventstore',
) {
}

public function load(
Criteria|null $criteria = null,
int|null $limit = null,
int|null $offset = null,
bool $backwards = false,
): DoctrineDbalStoreStream
{
int|null $limit = null,
int|null $offset = null,
bool $backwards = false,
): DoctrineDbalStoreStream {
$builder = $this->connection->createQueryBuilder()
->select('*')
->from($this->storeTableName)
Expand Down Expand Up @@ -292,8 +293,8 @@ private function getCustomHeaders(Message $message): array
return array_values(
array_filter(
$message->headers(),
static fn(Header $header) => !in_array($header::class, $filteredHeaders, true)
)
static fn (Header $header) => !in_array($header::class, $filteredHeaders, true)
),
);
}
}
6 changes: 4 additions & 2 deletions src/Store/DoctrineDbalStoreStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Patchlevel\EventSourcing\Serializer\SerializedEvent;
use Traversable;

use function array_map;

/** @implements IteratorAggregate<Message> */
final class DoctrineDbalStoreStream implements Stream, IteratorAggregate
{
Expand Down Expand Up @@ -105,8 +107,8 @@ private function buildGenerator(

$customHeaders = $headersSerializer->deserialize(array_map(
/** @param array{name: string, payload: string} $customHeader */
fn (array $customHeader) => new SerializedHeader($customHeader['name'], $customHeader['payload']),
DoctrineHelper::normalizeCustomHeaders($data['custom_headers'], $platform)
static fn (array $customHeader) => new SerializedHeader($customHeader['name'], $customHeader['payload']),
DoctrineHelper::normalizeCustomHeaders($data['custom_headers'], $platform),
));

yield Message::create($event)
Expand Down
3 changes: 0 additions & 3 deletions src/Store/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Patchlevel\EventSourcing\EventBus\Header;
use RuntimeException;

use function is_array;
use function is_bool;
Expand Down Expand Up @@ -49,7 +47,6 @@ public static function normalizeCustomHeaders(string $customHeaders, AbstractPla
throw new InvalidType('custom_headers', 'array');
}

/** @var array<array{name: string, payload: string}> $normalizedCustomHeaders */
return $normalizedCustomHeaders;
}

Expand Down
4 changes: 1 addition & 3 deletions src/Store/NewStreamStartHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Patchlevel\EventSourcing\EventBus\Header;

/**
* @psalm-immutable
*/
/** @psalm-immutable */
#[\Patchlevel\EventSourcing\Attribute\Header('newStreamStart')]
final class NewStreamStartHeader implements Header
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testSuccessful(): void
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__
__DIR__,
]),
'eventstore',
);
Expand Down Expand Up @@ -115,7 +115,7 @@ public function testSnapshot(): void
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__
__DIR__,
]),
'eventstore',
);
Expand Down
4 changes: 1 addition & 3 deletions tests/Integration/BasicImplementation/Header/BazHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Patchlevel\EventSourcing\EventBus\Header;

/**
* @psalm-immutable
*/
/** @psalm-immutable */
#[\Patchlevel\EventSourcing\Attribute\Header('baz')]
final class BazHeader implements Header
{
Expand Down
4 changes: 1 addition & 3 deletions tests/Integration/BasicImplementation/Header/FooHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Patchlevel\EventSourcing\EventBus\Header;

/**
* @psalm-immutable
*/
/** @psalm-immutable */
#[\Patchlevel\EventSourcing\Attribute\Header('foo')]
final class FooHeader implements Header
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Outbox/OutboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public function testSuccessful(): void
$eventSerializer = DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']);
$headerSerializer = DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__
__DIR__,
]);

$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__
__DIR__,
]),
'eventstore',
);
Expand Down Expand Up @@ -122,15 +122,15 @@ public function testSuccessfulWithEventSerializerMessageSerializer(): void
$eventSerializer = DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']);
$headerSerializer = DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__
__DIR__,
]);

$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__
__DIR__,
]),
'eventstore',
);
Expand All @@ -141,7 +141,7 @@ public function testSuccessfulWithEventSerializerMessageSerializer(): void
$eventSerializer,
$headerSerializer,
new MetadataHydrator(),

Check failure on line 143 in tests/Integration/Outbox/OutboxTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

InvalidArgument

tests/Integration/Outbox/OutboxTest.php:143:17: InvalidArgument: Argument 3 of Patchlevel\EventSourcing\EventBus\Serializer\EventSerializerMessageSerializer::__construct expects Patchlevel\EventSourcing\Serializer\Encoder\Encoder, but Patchlevel\Hydrator\MetadataHydrator provided (see https://psalm.dev/004)
new JsonEncoder()
new JsonEncoder(),
),
'outbox',
);
Expand Down
Loading

0 comments on commit 0dd6401

Please sign in to comment.