diff --git a/phpstan.neon b/phpstan.neon index 64e5e97c..93f52b64 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 2 + level: 3 paths: - src/ - tests/ diff --git a/psalm.xml b/psalm.xml index 5df66af5..a3b7bdbb 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ */ interface Envelope extends ValueObject @@ -24,6 +26,9 @@ public function uuid(): UUID; public function name(): string; + /** + * @return T + */ public function message(); /** @@ -33,5 +38,8 @@ public function message(); */ public function get($name); + /** + * @return array + */ public function metadata(): array; } diff --git a/src/Domain/Event/Envelope.php b/src/Domain/Event/Envelope.php index 47ac1484..c81b6a04 100644 --- a/src/Domain/Event/Envelope.php +++ b/src/Domain/Event/Envelope.php @@ -21,6 +21,7 @@ * @author Alan Gabriel Bem * * @template T of Event + * @implements Domain\Envelope * * @see \Streak\Domain\Event\EnvelopeTest */ @@ -51,9 +52,6 @@ public function __construct(UUID $uuid, string $name, private Event $message, Do } } - /** - * @return Envelope - */ public static function new(Event $message, Domain\Id $producerId, ?int $version = null): self { return new self(UUID::random(), $message::class, $message, $producerId, $producerId, $version); @@ -69,10 +67,7 @@ public function name(): string return $this->get(self::METADATA_NAME); } - /** - * @return T - */ - public function message(): Event + public function message() { return $this->message; } diff --git a/src/Domain/Event/Listener/Factory.php b/src/Domain/Event/Listener/Factory.php index 1ca6760d..6a42573d 100644 --- a/src/Domain/Event/Listener/Factory.php +++ b/src/Domain/Event/Listener/Factory.php @@ -15,20 +15,25 @@ use Streak\Domain\Event; use Streak\Domain\Event\Exception\InvalidEventGiven; -use Streak\Domain\Event\Listener; use Streak\Domain\Exception\InvalidIdGiven; /** + * @template T of Event\Listener + * * @author Alan Gabriel Bem */ interface Factory { /** + * @return T + * * @throws InvalidIdGiven */ - public function create(Listener\Id $id): Listener; + public function create(Event\Listener\Id $id): Event\Listener; /** + * @return T + * * @throws InvalidEventGiven */ public function createFor(Event\Envelope $event): Event\Listener; diff --git a/src/Domain/Event/Subscription/Factory.php b/src/Domain/Event/Subscription/Factory.php index b5ddf677..40d3a030 100644 --- a/src/Domain/Event/Subscription/Factory.php +++ b/src/Domain/Event/Subscription/Factory.php @@ -16,9 +16,14 @@ use Streak\Domain\Event; /** + * @template T of Event\Subscription + * * @author Alan Gabriel Bem */ interface Factory { + /** + * @return T + */ public function create(Event\Listener $listener): Event\Subscription; } diff --git a/src/Infrastructure/Domain/Event/LoggingListener/Factory.php b/src/Infrastructure/Domain/Event/LoggingListener/Factory.php index 761a368d..798e628c 100644 --- a/src/Infrastructure/Domain/Event/LoggingListener/Factory.php +++ b/src/Infrastructure/Domain/Event/LoggingListener/Factory.php @@ -18,6 +18,8 @@ use Streak\Infrastructure\Domain\Event\LoggingListener; /** + * @implements Event\Listener\Factory + * * @author Alan Gabriel Bem * * @see \Streak\Infrastructure\Domain\Event\LoggingListener\FactoryTest @@ -28,14 +30,14 @@ public function __construct(private Event\Listener\Factory $factory, private Log { } - public function create(Event\Listener\Id $id): Event\Listener + public function create(Event\Listener\Id $id): LoggingListener { $saga = $this->factory->create($id); return new LoggingListener($saga, $this->logger); } - public function createFor(Event\Envelope $event): Event\Listener + public function createFor(Event\Envelope $event): LoggingListener { $listener = $this->factory->createFor($event); diff --git a/src/Infrastructure/Domain/Event/Sourced/Subscription/InMemoryState.php b/src/Infrastructure/Domain/Event/Sourced/Subscription/InMemoryState.php index 3364262a..368a28b3 100644 --- a/src/Infrastructure/Domain/Event/Sourced/Subscription/InMemoryState.php +++ b/src/Infrastructure/Domain/Event/Sourced/Subscription/InMemoryState.php @@ -55,7 +55,7 @@ public function get(string $name) return $this->state[$name]; } - public function set(string $name, $value): State + public function set(string $name, $value): self { $this->validate($name, $value); diff --git a/src/Infrastructure/Domain/Event/Subscription/CommittingSubscription/Factory.php b/src/Infrastructure/Domain/Event/Subscription/CommittingSubscription/Factory.php index b46bfc3f..3eb9460c 100644 --- a/src/Infrastructure/Domain/Event/Subscription/CommittingSubscription/Factory.php +++ b/src/Infrastructure/Domain/Event/Subscription/CommittingSubscription/Factory.php @@ -19,6 +19,8 @@ use Streak\Infrastructure\Domain\UnitOfWork; /** + * @implements Event\Subscription\Factory + * * @author Alan Gabriel Bem * * @see \Streak\Infrastructure\Domain\Event\Subscription\CommittingSubscription\FactoryTest @@ -29,7 +31,7 @@ public function __construct(private Subscription\Factory $factory, private UnitO { } - public function create(Event\Listener $listener): Event\Subscription + public function create(Event\Listener $listener): CommittingSubscription { $subscription = $this->factory->create($listener); diff --git a/src/Infrastructure/Domain/Event/Subscription/DAO.php b/src/Infrastructure/Domain/Event/Subscription/DAO.php index 8087dcc0..a7a78fba 100644 --- a/src/Infrastructure/Domain/Event/Subscription/DAO.php +++ b/src/Infrastructure/Domain/Event/Subscription/DAO.php @@ -30,7 +30,7 @@ public function exists(Listener\Id $id): bool; /** * @param string[] $types * - * @return DAO\Subscription[] + * @return Subscription[] */ public function all(array $types = [], ?bool $completed = null): iterable; } diff --git a/src/Infrastructure/Domain/Event/Subscription/DAO/DbalPostgresDAO.php b/src/Infrastructure/Domain/Event/Subscription/DAO/DbalPostgresDAO.php index 409ebb42..a2325804 100644 --- a/src/Infrastructure/Domain/Event/Subscription/DAO/DbalPostgresDAO.php +++ b/src/Infrastructure/Domain/Event/Subscription/DAO/DbalPostgresDAO.php @@ -30,8 +30,13 @@ */ class DbalPostgresDAO implements DAO { - public function __construct(private Subscription\Factory $subscriptions, private Event\Listener\Factory $listeners, private Connection $connection, private Event\Converter $converter) - { + public function __construct( + /** @var Subscription\Factory */ + private Subscription\Factory $subscriptions, + private Event\Listener\Factory $listeners, + private Connection $connection, + private Event\Converter $converter + ) { } /** @@ -69,11 +74,7 @@ public function exists(Listener\Id $id): bool } /** - * @param string[] $types - * * @throws \Doctrine\DBAL\DBALException - * - * @return Subscription[] */ public function all(array $types = [], ?bool $completed = null): iterable { @@ -186,7 +187,7 @@ public function drop(): void $statement->execute(); } - private function fromRow($row): Subscription + private function fromRow($row): DAO\Subscription { $id = $row['subscription_type']; $id = $id::fromString($row['subscription_id']); @@ -306,11 +307,9 @@ private function doSave(DAO\Subscription $subscription): void } /** - * * @throws \Doctrine\DBAL\DBALException - * @return \Streak\Infrastructure\Domain\Event\Subscription\DAO\Subscription|null */ - private function doOne(Event\Listener\Id $id) + private function doOne(Event\Listener\Id $id): ?DAO\Subscription { $sql = 'SELECT subscription_type, subscription_id, subscription_version, state, started_by, started_at, last_processed_event, last_event_processed_at, completed, paused_at FROM subscriptions WHERE subscription_type = :subscription_type AND subscription_id = :subscription_id LIMIT 1'; diff --git a/src/Infrastructure/Domain/Event/Subscription/DAO/InMemoryDAO.php b/src/Infrastructure/Domain/Event/Subscription/DAO/InMemoryDAO.php index 56360950..cac5f52f 100644 --- a/src/Infrastructure/Domain/Event/Subscription/DAO/InMemoryDAO.php +++ b/src/Infrastructure/Domain/Event/Subscription/DAO/InMemoryDAO.php @@ -60,7 +60,7 @@ public function exists(Listener\Id $id): bool public function all(array $types = [], ?bool $completed = null): iterable { - foreach ($this->subscriptions as $key => $stored) { + foreach ($this->subscriptions as $stored) { if (\count($types)) { $type = $stored->id()::class; if (false === \in_array($type, $types)) { diff --git a/src/Infrastructure/Domain/Event/Subscription/DAO/Subscription.php b/src/Infrastructure/Domain/Event/Subscription/DAO/Subscription.php index 3c09522f..29b2e418 100644 --- a/src/Infrastructure/Domain/Event/Subscription/DAO/Subscription.php +++ b/src/Infrastructure/Domain/Event/Subscription/DAO/Subscription.php @@ -16,6 +16,7 @@ use Streak\Domain\Clock; use Streak\Domain\Event; use Streak\Domain\Event\Listener; +use Streak\Domain\Event\Listener\State; use Streak\Domain\Event\Subscription\Exception; use Streak\Domain\EventStore; use Streak\Infrastructure\Domain\Event\Sourced\Subscription\InMemoryState; @@ -28,12 +29,12 @@ class Subscription implements Event\Subscription { private const LIMIT_TO_INITIAL_STREAM = 0; - private InMemoryState $state; + private State $state; private ?Event\Envelope $startedBy = null; - private ?\DateTimeImmutable $startedAt = null; + private ?\DateTimeImmutable $startedAt = null; // @TODO: check if needed private ?\DateTimeImmutable $pausedAt = null; private ?Event\Envelope $lastProcessedEvent = null; - private ?\DateTimeImmutable $lastEventProcessedAt = null; + private ?\DateTimeImmutable $lastEventProcessedAt = null; // @TODO: check if needed private int $version = 0; private bool $completed = false; diff --git a/src/Infrastructure/Domain/Event/Subscription/DAO/Subscription/Factory.php b/src/Infrastructure/Domain/Event/Subscription/DAO/Subscription/Factory.php index 3568288c..ccfc119e 100644 --- a/src/Infrastructure/Domain/Event/Subscription/DAO/Subscription/Factory.php +++ b/src/Infrastructure/Domain/Event/Subscription/DAO/Subscription/Factory.php @@ -15,9 +15,11 @@ use Streak\Domain\Clock; use Streak\Domain\Event; -use Streak\Infrastructure\Domain\Event\Subscription\DAO\Subscription; +use Streak\Infrastructure\Domain\Event\Subscription\DAO; /** + * @implements Event\Subscription\Factory + * * @author Alan Gabriel Bem * * @see \Streak\Infrastructure\Domain\Event\Subscription\DAO\Subscription\FactoryTest @@ -28,8 +30,8 @@ public function __construct(private Clock $clock) { } - public function create(Event\Listener $listener): Event\Subscription + public function create(Event\Listener $listener): DAO\Subscription { - return new Subscription($listener, $this->clock); + return new DAO\Subscription($listener, $this->clock); } } diff --git a/src/Infrastructure/Domain/Event/Subscription/DbalTransactionalSubscription/Factory.php b/src/Infrastructure/Domain/Event/Subscription/DbalTransactionalSubscription/Factory.php index 86c89620..0fc0e64c 100644 --- a/src/Infrastructure/Domain/Event/Subscription/DbalTransactionalSubscription/Factory.php +++ b/src/Infrastructure/Domain/Event/Subscription/DbalTransactionalSubscription/Factory.php @@ -18,6 +18,8 @@ use Streak\Infrastructure\Domain\Event\Subscription\DbalTransactionalSubscription; /** + * @implements Event\Subscription\Factory + * * @author Alan Gabriel Bem * * @see \Streak\Infrastructure\Domain\Event\Subscription\DbalTransactionalSubscription\FactoryTest @@ -35,7 +37,7 @@ public function __construct(private Event\Subscription\Factory $factory, private $this->maxTransactionSize = $maxTransactionSize; } - public function create(Event\Listener $listener): Event\Subscription + public function create(Event\Listener $listener): DbalTransactionalSubscription { $subscription = $this->factory->create($listener); diff --git a/src/Infrastructure/Domain/UnitOfWork/SubscriptionDAOUnitOfWork.php b/src/Infrastructure/Domain/UnitOfWork/SubscriptionDAOUnitOfWork.php index 186cfd3a..69f2b0ae 100644 --- a/src/Infrastructure/Domain/UnitOfWork/SubscriptionDAOUnitOfWork.php +++ b/src/Infrastructure/Domain/UnitOfWork/SubscriptionDAOUnitOfWork.php @@ -27,7 +27,7 @@ class SubscriptionDAOUnitOfWork implements UnitOfWork { /** - * @var Subscription[] + * @var DAO\Subscription[] */ private array $uncommited = []; @@ -98,7 +98,7 @@ public function commit(): \Generator try { while ($object = array_shift($this->uncommited)) { - /** @var Subscription $object */ + /** @var DAO\Subscription $object */ try { $this->dao->save($object); @@ -125,16 +125,12 @@ public function clear(): void private function supports(object $subscription): bool { - if ($subscription instanceof DAO\Subscription) { - return true; - } - while ($subscription instanceof Subscription\Decorator) { $subscription = $subscription->subscription(); + } - if ($subscription instanceof DAO\Subscription) { - return true; - } + if ($subscription instanceof DAO\Subscription) { + return true; } return false; diff --git a/tests/Domain/EventSourcingTest.php b/tests/Domain/EventSourcingTest.php index 85ac0bd0..bbe4da6e 100644 --- a/tests/Domain/EventSourcingTest.php +++ b/tests/Domain/EventSourcingTest.php @@ -637,7 +637,7 @@ class EventSourcedAggregateStub implements Event\Sourced\Aggregate use Aggregate\Identification; /** - * @var Event\Envelope[] + * @var array */ private array $appliedEvents = []; @@ -671,12 +671,12 @@ public function appliedEvents(): array private function applyEvent1(Event1 $event): void { - $this->appliedEvents = [$event]; + $this->appliedEvents[] = $event; } private function applyEvent2(Event2 $event): void { - $this->appliedEvents = [$event]; + $this->appliedEvents[] = $event; } }