Skip to content

Commit

Permalink
full list of changes below:
Browse files Browse the repository at this point in the history
- psalm set to level 3
- [WiP] phpstan set to level 6
  • Loading branch information
alanbem committed May 18, 2022
1 parent 1fe7696 commit 5349b08
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 46 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 2
level: 3
paths:
- src/
- tests/
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="7"
errorLevel="6"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
8 changes: 8 additions & 0 deletions src/Domain/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Streak\Domain\Id\UUID;

/**
* @template T of object
*
* @author Alan Gabriel Bem <[email protected]>
*/
interface Envelope extends ValueObject
Expand All @@ -24,6 +26,9 @@ public function uuid(): UUID;

public function name(): string;

/**
* @return T
*/
public function message();

/**
Expand All @@ -33,5 +38,8 @@ public function message();
*/
public function get($name);

/**
* @return array<scalar, scalar>
*/
public function metadata(): array;
}
9 changes: 2 additions & 7 deletions src/Domain/Event/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @author Alan Gabriel Bem <[email protected]>
*
* @template T of Event
* @implements Domain\Envelope<T>
*
* @see \Streak\Domain\Event\EnvelopeTest
*/
Expand Down Expand Up @@ -51,9 +52,6 @@ public function __construct(UUID $uuid, string $name, private Event $message, Do
}
}

/**
* @return Envelope<T>
*/
public static function new(Event $message, Domain\Id $producerId, ?int $version = null): self
{
return new self(UUID::random(), $message::class, $message, $producerId, $producerId, $version);
Expand All @@ -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;
}
Expand Down
9 changes: 7 additions & 2 deletions src/Domain/Event/Listener/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
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;
Expand Down
5 changes: 5 additions & 0 deletions src/Domain/Event/Subscription/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
use Streak\Domain\Event;

/**
* @template T of Event\Subscription
*
* @author Alan Gabriel Bem <[email protected]>
*/
interface Factory
{
/**
* @return T
*/
public function create(Event\Listener $listener): Event\Subscription;
}
6 changes: 4 additions & 2 deletions src/Infrastructure/Domain/Event/LoggingListener/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Streak\Infrastructure\Domain\Event\LoggingListener;

/**
* @implements Event\Listener\Factory<LoggingListener>
*
* @author Alan Gabriel Bem <[email protected]>
*
* @see \Streak\Infrastructure\Domain\Event\LoggingListener\FactoryTest
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Streak\Infrastructure\Domain\UnitOfWork;

/**
* @implements Event\Subscription\Factory<CommittingSubscription>
*
* @author Alan Gabriel Bem <[email protected]>
*
* @see \Streak\Infrastructure\Domain\Event\Subscription\CommittingSubscription\FactoryTest
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Domain/Event/Subscription/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DAO\Subscription> */
private Subscription\Factory $subscriptions,
private Event\Listener\Factory $listeners,
private Connection $connection,
private Event\Converter $converter
) {
}

/**
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DAO\Subscription>
*
* @author Alan Gabriel Bem <[email protected]>
*
* @see \Streak\Infrastructure\Domain\Event\Subscription\DAO\Subscription\FactoryTest
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Streak\Infrastructure\Domain\Event\Subscription\DbalTransactionalSubscription;

/**
* @implements Event\Subscription\Factory<DbalTransactionalSubscription>
*
* @author Alan Gabriel Bem <[email protected]>
*
* @see \Streak\Infrastructure\Domain\Event\Subscription\DbalTransactionalSubscription\FactoryTest
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class SubscriptionDAOUnitOfWork implements UnitOfWork
{
/**
* @var Subscription[]
* @var DAO\Subscription[]
*/
private array $uncommited = [];

Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/Domain/EventSourcingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ class EventSourcedAggregateStub implements Event\Sourced\Aggregate
use Aggregate\Identification;

/**
* @var Event\Envelope[]
* @var array<int, Event\Envelope>
*/
private array $appliedEvents = [];

Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 5349b08

Please sign in to comment.