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 31, 2022
1 parent 1fe7696 commit 82c00be
Show file tree
Hide file tree
Showing 26 changed files with 142 additions and 66 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
2 changes: 1 addition & 1 deletion src/Domain/AggregateRoot/EventSourcing.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private function apply(Event $event): void
}

/**
* @return Event\Sourced\Entity[]
* @return iterable<Event\Sourced\Entity>
*/
private function eventSourcedEntities(): iterable
{
Expand Down
10 changes: 9 additions & 1 deletion 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,14 +26,20 @@ public function uuid(): UUID;

public function name(): string;

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

/**
* @param string $name
*
* @return float|int|string|null
* @return null|scalar
*/
public function get($name);

/**
* @return array<scalar, scalar>
*/
public function metadata(): array;
}
54 changes: 35 additions & 19 deletions src/Domain/Event/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
/**
* @author Alan Gabriel Bem <[email protected]>
*
* @template T of Event
* @template TMessage as Event
* @implements Domain\Envelope<TMessage>
*
* @see \Streak\Domain\Event\EnvelopeTest
*/
Expand All @@ -33,26 +34,44 @@ final class Envelope implements Domain\Envelope
public const METADATA_PRODUCER_ID = 'producer_id';
public const METADATA_ENTITY_TYPE = 'entity_type';
public const METADATA_ENTITY_ID = 'entity_id';
private array $metadata = [];

/**
* @param T $message
* @var-phpstan non-empty-array<non-empty-string, scalar>
* @var-psalm non-empty-array<non-empty-string, scalar>&array{
* uuid: non-empty-string,
* name: non-empty-string,
* producer_type: non-empty-string,
* producer_id: non-empty-string,
* entity_type: non-empty-string,
* entity_id: non-empty-string,
* version?: positive-int,
* }
*/
private array $metadata;

/**
* @param TMessage $message
*/
public function __construct(UUID $uuid, string $name, private Event $message, Domain\Id $producerId, Domain\Id $entityId, ?int $version = null)
{
$this->metadata[self::METADATA_UUID] = $uuid->toString();
$this->metadata[self::METADATA_NAME] = $name;
$this->metadata[self::METADATA_PRODUCER_TYPE] = $producerId::class;
$this->metadata[self::METADATA_PRODUCER_ID] = $producerId->toString();
$this->metadata[self::METADATA_ENTITY_TYPE] = $entityId::class;
$this->metadata[self::METADATA_ENTITY_ID] = $entityId->toString();
$this->metadata = [
self::METADATA_UUID => $uuid->toString(),
self::METADATA_NAME => $name,
self::METADATA_PRODUCER_TYPE => $producerId::class,
self::METADATA_PRODUCER_ID => $producerId->toString(),
self::METADATA_ENTITY_TYPE => $entityId::class,
self::METADATA_ENTITY_ID => $entityId->toString(),
];
if (null !== $version) {
$this->metadata[self::METADATA_VERSION] = $version;
}
}

/**
* @return Envelope<T>
* @template TEvent of Event
* @param TEvent $message
*
* @return self<TEvent>
*/
public static function new(Event $message, Domain\Id $producerId, ?int $version = null): self
{
Expand All @@ -61,18 +80,15 @@ public static function new(Event $message, Domain\Id $producerId, ?int $version

public function uuid(): UUID
{
return new UUID($this->get(self::METADATA_UUID));
return new UUID($this->metadata[self::METADATA_UUID]);
}

public function name(): string
{
return $this->get(self::METADATA_NAME);
return $this->metadata[self::METADATA_NAME];
}

/**
* @return T
*/
public function message(): Event
public function message()
{
return $this->message;
}
Expand All @@ -84,7 +100,7 @@ public function producerId(): Domain\Id
/** @var class-string<Domain\Id> $class */
/** @phpstan-var class-string<Domain\Id> $class */
/** @psalm-var class-string<Domain\Id> $class */
return $class::fromString($this->get(self::METADATA_PRODUCER_ID));
return $class::fromString((string) $this->get(self::METADATA_PRODUCER_ID));
}

public function entityId(): Domain\Id
Expand All @@ -94,12 +110,12 @@ public function entityId(): Domain\Id
/** @var class-string<Domain\Id> $class */
/** @phpstan-var class-string<Domain\Id> $class */
/** @psalm-var class-string<Domain\Id> $class */
return $class::fromString($this->get(self::METADATA_ENTITY_ID));
return $class::fromString((string) $this->get(self::METADATA_ENTITY_ID));
}

public function version(): ?int
{
return $this->get(self::METADATA_VERSION);
return $this->metadata[self::METADATA_VERSION] ?? null;
}

public function set(string $name, $value): self
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
4 changes: 3 additions & 1 deletion src/Domain/Event/Sourced/Entity/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function applyEvent(Event\Envelope $event): void
}

/**
* @return Event\Sourced\Entity[]
* @return \Generator<int, Event\Sourced\Entity>
*/
public function extractEventSourcedEntities(): iterable
{
Expand Down Expand Up @@ -128,6 +128,8 @@ private static function applyEventByArgumentType(Event\Envelope $event, object &

/**
* Extract event sourced entities recursively.
*
* @return \Generator<int, Event\Sourced\Entity>
*/
private static function doExtractEventSourcedEntities(object $object, array &$ignored = []): iterable
{
Expand Down
2 changes: 2 additions & 0 deletions src/Domain/Event/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* @author Alan Gabriel Bem <[email protected]>
*
* @psalm-immutable
*/
interface Stream extends \Traversable
{
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

/**
* @author Alan Gabriel Bem <[email protected]>
*
* @see \Streak\Infrastructure\Domain\AggregateRoot\Snapshotter\Storage\RedisStorageTest
*/
final class RedisStorage implements Storage, Resettable
{
Expand All @@ -38,12 +40,17 @@ public function find(AggregateRoot $aggregate): string
throw new SnapshotNotFound($aggregate);
}

// Redis::get() can return Redis instance when it's in multi() mode.
if ($snapshot instanceof \Redis) {
throw new SnapshotNotFound($aggregate);
}

return $snapshot;
}

public function store(AggregateRoot $aggregate, string $snapshot): void
{
$this->redis->set($this->key($aggregate), (string) $snapshot);
$this->redis->set($this->key($aggregate), $snapshot);
}

public function reset(): bool
Expand Down
2 changes: 2 additions & 0 deletions src/Infrastructure/Domain/Event/InMemoryStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
/**
* @author Alan Gabriel Bem <[email protected]>
*
* @psalm-immutable
*
* @see \Streak\Infrastructure\Domain\Event\InMemoryStreamTest
*/
final class InMemoryStream implements \IteratorAggregate, Event\Stream
Expand Down
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 iterable<int,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,9 @@ public function exists(Listener\Id $id): bool
}

/**
* @param string[] $types
* @return \Generator<int, Subscription>
*
* @throws \Doctrine\DBAL\DBALException
*
* @return Subscription[]
*/
public function all(array $types = [], ?bool $completed = null): iterable
{
Expand Down Expand Up @@ -186,7 +189,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 +309,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 Expand Up @@ -354,6 +355,8 @@ private function doExists(Listener\Id $id): bool
}

/**
* @return \Generator<int, Subscription>
*
* @throws \Doctrine\DBAL\DBALException
*/
private function doAll(array $types, ?bool $completed): \Generator
Expand Down
Loading

0 comments on commit 82c00be

Please sign in to comment.