Skip to content

Define generic type for message payload#242

Open
lcobucci wants to merge 1 commit into
EventSaucePHP:mainfrom
lcobucci:add-generic-definition-for-message-payload
Open

Define generic type for message payload#242
lcobucci wants to merge 1 commit into
EventSaucePHP:mainfrom
lcobucci:add-generic-definition-for-message-payload

Conversation

@lcobucci
Copy link
Copy Markdown

@lcobucci lcobucci commented Jul 4, 2025

This makes Message a bit more useful when combined with static analysis tools.

@lcobucci lcobucci force-pushed the add-generic-definition-for-message-payload branch from f68a824 to 336cff2 Compare July 4, 2025 09:38
@lcobucci lcobucci force-pushed the add-generic-definition-for-message-payload branch from 336cff2 to 9d4baa4 Compare October 2, 2025 14:02
Fixes EventSaucePHP#241

This sets up a new generic type for messages, allowing users to define expectation on the payload type.

It's a minor BC-break, given people who are defining custom types for aggregate root ids with `Message<MyCustomRootId>` will have to migrate to `Message<object, MyCustomRootId>`.

Signed-off-by: Luís Cobucci <lcobucci@gmail.com>
@lcobucci lcobucci force-pushed the add-generic-definition-for-message-payload branch from 9d4baa4 to 20b907f Compare October 10, 2025 16:06
@lcobucci
Copy link
Copy Markdown
Author

lcobucci commented Oct 10, 2025

@frankdejonge I fixed the PHPStan errors.

I, unfortunately, had to add some assert() calls due to phpstan/phpstan#8438.

I really wanted to have something like this:

- * @phpstan-type HeaderValue TId|int|string|array<mixed>|null|bool|float
+ * @phpstan-type CustomHeaderValue int|string|array<mixed>|null|bool|float
+ * @phpstan-type HeaderValue TId|CustomHeaderValue
- * @phpstan-type HeadersShape array{non-empty-string, HeaderValue}
+ * @phpstan-type HeadersShape array{Header::EVENT_ID?: non-empty-string, Header::EVENT_TYPE?: non-empty-string, Header::TIME_OF_RECORDING?: non-empty-string, Header::TIME_OF_RECORDING_FORMAT?: non-empty-string, Header::AGGREGATE_ROOT_ID?: TId, Header::AGGREGATE_ROOT_ID_TYPE?: non-empty-string, Header::AGGREGATE_ROOT_TYPE?: non-empty-string, Header::AGGREGATE_ROOT_VERSION?: int|numeric-string, ...<non-empty-string, CustomHeaderValue>}

Comment thread src/Message.php
public function aggregateVersion(): int
{
$version = $this->headers[Header::AGGREGATE_ROOT_VERSION] ?? null;
\assert($version === null || \is_numeric($version));
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used \ on the calls, instead of importing the functions, to make it easier to simply remove these once PHPStan gets smarter 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant