Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect aggregate root ID type on AggregateRootWithAggregates #225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

axlon
Copy link
Contributor

@axlon axlon commented Jun 3, 2024

What does this fix?

The AggregateRootWithAggregates breaks type inference for the aggregate root's ID. When using this trait PHPStan will always infer the type as EventSauce\EventSourcing\AggregateRootId

For example:

final class FooId implements AggregateRootId
{
    public function toString(): string
    {
        return 'foo';
    }

    public static function fromString(string $aggregateRootId): static
    {
        return new self();
    }
}

/**
 * @implements AggregateRoot<FooId>
 */
final class Foo implements AggregateRoot
{
    /**
     * @use AggregateRootWithAggregates<FooId, EventSourcedAggregate>
     */
    use AggregateRootWithAggregates;

    public function weCannotInferKeyType(): void
    {
        dumpType($this->aggregateRootId()); // Dumped type: EventSauce\EventSourcing\AggregateRootId
    }
}

With this PR you'd have to add FooId to the @use statement, after which $this->aggregateRootId() would be typed as FooId.

Something to take note of

While this fixes the mistyping completely, it could be considered a breaking change as adding a new template to a trait will surely cause some PHPStan workflows to fail.

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.

2 participants