diff --git a/baseline.xml b/baseline.xml index 10aae5a50..d35f6b024 100644 --- a/baseline.xml +++ b/baseline.xml @@ -1,5 +1,5 @@ - + @@ -20,11 +20,6 @@ getName()]]> - - - - - @@ -83,15 +78,10 @@ - - - - - - ]]> + ]]> diff --git a/src/Aggregate/AggregateHeader.php b/src/Aggregate/AggregateHeader.php index 77b66833e..3b0171289 100644 --- a/src/Aggregate/AggregateHeader.php +++ b/src/Aggregate/AggregateHeader.php @@ -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 { diff --git a/src/Console/OutputStyle.php b/src/Console/OutputStyle.php index 5150747a5..0efeb23ad 100644 --- a/src/Console/OutputStyle.php +++ b/src/Console/OutputStyle.php @@ -4,7 +4,6 @@ namespace Patchlevel\EventSourcing\Console; -use DateTimeInterface; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Serializer\Encoder\Encoder; use Patchlevel\EventSourcing\Serializer\EventSerializer; @@ -12,7 +11,6 @@ use Throwable; use function array_keys; -use function array_values; use function sprintf; final class OutputStyle extends SymfonyStyle diff --git a/src/EventBus/Header.php b/src/EventBus/Header.php index 9b3a1ea04..dc40fdc61 100644 --- a/src/EventBus/Header.php +++ b/src/EventBus/Header.php @@ -4,9 +4,7 @@ namespace Patchlevel\EventSourcing\EventBus; -/** - * @psalm-immutable - */ +/** @psalm-immutable */ interface Header { } diff --git a/src/EventBus/Message.php b/src/EventBus/Message.php index 391101c64..b78859945 100644 --- a/src/EventBus/Message.php +++ b/src/EventBus/Message.php @@ -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 diff --git a/src/EventBus/Serializer/DefaultHeadersSerializer.php b/src/EventBus/Serializer/DefaultHeadersSerializer.php index d0aff990b..0f73a5bdd 100644 --- a/src/EventBus/Serializer/DefaultHeadersSerializer.php +++ b/src/EventBus/Serializer/DefaultHeadersSerializer.php @@ -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 { @@ -25,19 +21,29 @@ public function __construct( ) { } + /** + * @param list
$headers + * + * @return array + */ 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 $serializedHeaders + * + * @return list
+ */ public function deserialize(array $serializedHeaders): array { $headers = []; diff --git a/src/EventBus/Serializer/EventSerializerMessageSerializer.php b/src/EventBus/Serializer/EventSerializerMessageSerializer.php index 9bceb36eb..67a386a65 100644 --- a/src/EventBus/Serializer/EventSerializerMessageSerializer.php +++ b/src/EventBus/Serializer/EventSerializerMessageSerializer.php @@ -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( @@ -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); diff --git a/src/EventBus/Serializer/HeadersSerializer.php b/src/EventBus/Serializer/HeadersSerializer.php index 058d9662e..016abd99d 100644 --- a/src/EventBus/Serializer/HeadersSerializer.php +++ b/src/EventBus/Serializer/HeadersSerializer.php @@ -10,12 +10,14 @@ interface HeadersSerializer { /** * @param list
$headers + * * @return array */ public function serialize(array $headers): array; /** * @param array $serializedHeaders + * * @return list
*/ public function deserialize(array $serializedHeaders): array; diff --git a/src/Metadata/Message/MessageHeaderRegistry.php b/src/Metadata/Message/MessageHeaderRegistry.php index f73773d8d..9c673337f 100644 --- a/src/Metadata/Message/MessageHeaderRegistry.php +++ b/src/Metadata/Message/MessageHeaderRegistry.php @@ -5,6 +5,7 @@ namespace Patchlevel\EventSourcing\Metadata\Message; use Patchlevel\EventSourcing\EventBus\Header; + use function array_flip; use function array_key_exists; diff --git a/src/Outbox/OutboxHeader.php b/src/Outbox/OutboxHeader.php index cc7271bff..7ad7a8607 100644 --- a/src/Outbox/OutboxHeader.php +++ b/src/Outbox/OutboxHeader.php @@ -6,9 +6,7 @@ use Patchlevel\EventSourcing\EventBus\Header; -/** - * @psalm-immutable - */ +/** @psalm-immutable */ #[\Patchlevel\EventSourcing\Attribute\Header('outbox')] final class OutboxHeader implements Header { diff --git a/src/Store/ArchivedHeader.php b/src/Store/ArchivedHeader.php index 25afd15c7..21b603874 100644 --- a/src/Store/ArchivedHeader.php +++ b/src/Store/ArchivedHeader.php @@ -6,9 +6,7 @@ use Patchlevel\EventSourcing\EventBus\Header; -/** - * @psalm-immutable - */ +/** @psalm-immutable */ #[\Patchlevel\EventSourcing\Attribute\Header('archived')] final class ArchivedHeader implements Header { diff --git a/src/Store/DoctrineDbalStore.php b/src/Store/DoctrineDbalStore.php index ec16fde67..bca843404 100644 --- a/src/Store/DoctrineDbalStore.php +++ b/src/Store/DoctrineDbalStore.php @@ -20,8 +20,11 @@ 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; @@ -29,21 +32,19 @@ 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) @@ -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) + ), ); } } diff --git a/src/Store/DoctrineDbalStoreStream.php b/src/Store/DoctrineDbalStoreStream.php index 042cda686..75adc5c60 100644 --- a/src/Store/DoctrineDbalStoreStream.php +++ b/src/Store/DoctrineDbalStoreStream.php @@ -16,6 +16,8 @@ use Patchlevel\EventSourcing\Serializer\SerializedEvent; use Traversable; +use function array_map; + /** @implements IteratorAggregate */ final class DoctrineDbalStoreStream implements Stream, IteratorAggregate { @@ -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) diff --git a/src/Store/DoctrineHelper.php b/src/Store/DoctrineHelper.php index c6b96dbc8..e2e0523b5 100644 --- a/src/Store/DoctrineHelper.php +++ b/src/Store/DoctrineHelper.php @@ -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; @@ -49,7 +47,6 @@ public static function normalizeCustomHeaders(string $customHeaders, AbstractPla throw new InvalidType('custom_headers', 'array'); } - /** @var array $normalizedCustomHeaders */ return $normalizedCustomHeaders; } diff --git a/src/Store/NewStreamStartHeader.php b/src/Store/NewStreamStartHeader.php index bb69695fa..13e58d691 100644 --- a/src/Store/NewStreamStartHeader.php +++ b/src/Store/NewStreamStartHeader.php @@ -6,9 +6,7 @@ use Patchlevel\EventSourcing\EventBus\Header; -/** - * @psalm-immutable - */ +/** @psalm-immutable */ #[\Patchlevel\EventSourcing\Attribute\Header('newStreamStart')] final class NewStreamStartHeader implements Header { diff --git a/tests/Integration/BasicImplementation/BasicIntegrationTest.php b/tests/Integration/BasicImplementation/BasicIntegrationTest.php index e519d941a..24b8f7510 100644 --- a/tests/Integration/BasicImplementation/BasicIntegrationTest.php +++ b/tests/Integration/BasicImplementation/BasicIntegrationTest.php @@ -47,7 +47,7 @@ public function testSuccessful(): void DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), DefaultHeadersSerializer::createFromPaths([ __DIR__ . '/../../../src', - __DIR__ + __DIR__, ]), 'eventstore', ); @@ -115,7 +115,7 @@ public function testSnapshot(): void DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), DefaultHeadersSerializer::createFromPaths([ __DIR__ . '/../../../src', - __DIR__ + __DIR__, ]), 'eventstore', ); diff --git a/tests/Integration/BasicImplementation/Header/BazHeader.php b/tests/Integration/BasicImplementation/Header/BazHeader.php index f6a723b46..023e5aee2 100644 --- a/tests/Integration/BasicImplementation/Header/BazHeader.php +++ b/tests/Integration/BasicImplementation/Header/BazHeader.php @@ -6,9 +6,7 @@ use Patchlevel\EventSourcing\EventBus\Header; -/** - * @psalm-immutable - */ +/** @psalm-immutable */ #[\Patchlevel\EventSourcing\Attribute\Header('baz')] final class BazHeader implements Header { diff --git a/tests/Integration/BasicImplementation/Header/FooHeader.php b/tests/Integration/BasicImplementation/Header/FooHeader.php index 29a233dcc..fd56923a3 100644 --- a/tests/Integration/BasicImplementation/Header/FooHeader.php +++ b/tests/Integration/BasicImplementation/Header/FooHeader.php @@ -6,9 +6,7 @@ use Patchlevel\EventSourcing\EventBus\Header; -/** - * @psalm-immutable - */ +/** @psalm-immutable */ #[\Patchlevel\EventSourcing\Attribute\Header('foo')] final class FooHeader implements Header { diff --git a/tests/Integration/Outbox/OutboxTest.php b/tests/Integration/Outbox/OutboxTest.php index 181b57f8d..7d7b0178f 100644 --- a/tests/Integration/Outbox/OutboxTest.php +++ b/tests/Integration/Outbox/OutboxTest.php @@ -48,7 +48,7 @@ public function testSuccessful(): void $eventSerializer = DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']); $headerSerializer = DefaultHeadersSerializer::createFromPaths([ __DIR__ . '/../../../src', - __DIR__ + __DIR__, ]); $store = new DoctrineDbalStore( @@ -56,7 +56,7 @@ public function testSuccessful(): void DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), DefaultHeadersSerializer::createFromPaths([ __DIR__ . '/../../../src', - __DIR__ + __DIR__, ]), 'eventstore', ); @@ -122,7 +122,7 @@ public function testSuccessfulWithEventSerializerMessageSerializer(): void $eventSerializer = DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']); $headerSerializer = DefaultHeadersSerializer::createFromPaths([ __DIR__ . '/../../../src', - __DIR__ + __DIR__, ]); $store = new DoctrineDbalStore( @@ -130,7 +130,7 @@ public function testSuccessfulWithEventSerializerMessageSerializer(): void DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), DefaultHeadersSerializer::createFromPaths([ __DIR__ . '/../../../src', - __DIR__ + __DIR__, ]), 'eventstore', ); @@ -141,7 +141,7 @@ public function testSuccessfulWithEventSerializerMessageSerializer(): void $eventSerializer, $headerSerializer, new MetadataHydrator(), - new JsonEncoder() + new JsonEncoder(), ), 'outbox', ); diff --git a/tests/Unit/EventBus/Serializer/HeadersSerializerTest.php b/tests/Unit/EventBus/Serializer/HeadersSerializerTest.php index 0edbe334e..e19b0e099 100644 --- a/tests/Unit/EventBus/Serializer/HeadersSerializerTest.php +++ b/tests/Unit/EventBus/Serializer/HeadersSerializerTest.php @@ -6,20 +6,11 @@ use DateTimeImmutable; use Patchlevel\EventSourcing\Aggregate\AggregateHeader; -use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\EventBus\Serializer\DefaultHeadersSerializer; -use Patchlevel\EventSourcing\EventBus\Serializer\DeserializeFailed; -use Patchlevel\EventSourcing\EventBus\Serializer\EventSerializerMessageSerializer; -use Patchlevel\EventSourcing\EventBus\Serializer\HeadersSerializer; use Patchlevel\EventSourcing\EventBus\Serializer\SerializedHeader; -use Patchlevel\EventSourcing\Metadata\Event\AttributeEventRegistryFactory; use Patchlevel\EventSourcing\Metadata\Message\AttributeMessageHeaderRegistryFactory; use Patchlevel\EventSourcing\Serializer\Encoder\JsonEncoder; -use Patchlevel\EventSourcing\Serializer\EventSerializer; -use Patchlevel\EventSourcing\Serializer\SerializedEvent; use Patchlevel\EventSourcing\Store\ArchivedHeader; -use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; -use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisited; use Patchlevel\Hydrator\MetadataHydrator; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; @@ -33,8 +24,8 @@ public function testSerialize(): void { $serializer = new DefaultHeadersSerializer( (new AttributeMessageHeaderRegistryFactory())->create([ - __DIR__ . '/../../../../src', # add our headers - __DIR__ . '/../../Fixture', # add user headers + __DIR__ . '/../../../../src', // add our headers + __DIR__ . '/../../Fixture', // add user headers ]), new MetadataHydrator(), new JsonEncoder(), @@ -42,7 +33,7 @@ public function testSerialize(): void $content = $serializer->serialize([ new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2020-01-01T20:00:00.000000+0100')), - new ArchivedHeader(false) + new ArchivedHeader(false), ]); self::assertEquals( @@ -50,7 +41,7 @@ public function testSerialize(): void new SerializedHeader('aggregate', '{"aggregateName":"profile","aggregateId":"1","playhead":1,"recordedOn":"2020-01-01T20:00:00+01:00"}'), new SerializedHeader('archived', '{"archived":false}'), ], - $content + $content, ); } @@ -58,8 +49,8 @@ public function testDeserialize(): void { $serializer = new DefaultHeadersSerializer( (new AttributeMessageHeaderRegistryFactory())->create([ - __DIR__ . '/../../../../src', # add our headers - __DIR__ . '/../../Fixture', # add user headers + __DIR__ . '/../../../../src', // add our headers + __DIR__ . '/../../Fixture', // add user headers ]), new MetadataHydrator(), new JsonEncoder(), @@ -69,15 +60,15 @@ public function testDeserialize(): void [ new SerializedHeader('aggregate', '{"aggregateName":"profile","aggregateId":"1","playhead":1,"recordedOn":"2020-01-01T20:00:00+01:00"}'), new SerializedHeader('archived', '{"archived":false}'), - ] + ], ); self::assertEquals( [ new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2020-01-01T20:00:00.000000+0100')), - new ArchivedHeader(false) + new ArchivedHeader(false), ], - $deserializedMessage + $deserializedMessage, ); } }