Define generic type for message payload#242
Open
lcobucci wants to merge 1 commit into
Open
Conversation
f68a824 to
336cff2
Compare
336cff2 to
9d4baa4
Compare
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>
9d4baa4 to
20b907f
Compare
Author
|
@frankdejonge I fixed the PHPStan errors. I, unfortunately, had to add some 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>} |
lcobucci
commented
Oct 10, 2025
| public function aggregateVersion(): int | ||
| { | ||
| $version = $this->headers[Header::AGGREGATE_ROOT_VERSION] ?? null; | ||
| \assert($version === null || \is_numeric($version)); |
Author
There was a problem hiding this comment.
I used \ on the calls, instead of importing the functions, to make it easier to simply remove these once PHPStan gets smarter 👍
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes
Messagea bit more useful when combined with static analysis tools.