Skip to content

Commit

Permalink
full list of changes below:
Browse files Browse the repository at this point in the history
- phpstan set to level 3
  • Loading branch information
alanbem committed Jun 12, 2022
1 parent 3d9c569 commit 43a12a1
Show file tree
Hide file tree
Showing 40 changed files with 210 additions and 139 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
5 changes: 3 additions & 2 deletions src/Application/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
namespace Streak\Application;

use Streak\Domain;
use Streak\Domain\Exception\CommandNotSupported;
use Streak\Domain\Exception;

/**
* @author Alan Gabriel Bem <[email protected]>
*/
interface CommandBus
{
/**
* @throws CommandNotSupported
* @throws Exception\CommandNotSupported
* @throws Exception\ConcurrentWriteDetected
*/
public function dispatch(Domain\Command $command): void;
}
5 changes: 2 additions & 3 deletions src/Domain/AggregateRoot/EventSourcing.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ final public function applyEvent(Event\Envelope $event): void
}

foreach ($this->eventSourcedEntities() as $entity) {
/** @var Event\Sourced\Entity $entity */
if ($entity->id()->equals($event->entityId())) {
$aggregate = $entity;
$stack = [];
Expand Down Expand Up @@ -147,9 +146,9 @@ private function apply(Event $event): void
}

/**
* @return Event\Sourced\Entity[]
* @return \Generator<Event\Sourced\Entity>
*/
private function eventSourcedEntities(): iterable
private function eventSourcedEntities(): \Generator
{
yield from Event\Sourced\Entity\Helper::for($this)->extractEventSourcedEntities();
}
Expand Down
7 changes: 6 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,12 +26,15 @@ 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);

Expand Down
2 changes: 2 additions & 0 deletions src/Domain/Event/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ interface Converter
{
/**
* @throws Exception\ConversionToArrayNotPossible
* @throws \Exception
*/
public function objectToArray(object $object): array;

/**
* @throws Exception\ConversionToObjectNotPossible
* @throws \Exception
*/
public function arrayToObject(array $data): object;
}
85 changes: 55 additions & 30 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,45 @@ 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: class-string<Domain\Id>,
* producer_id: non-empty-string,
* entity_type: class-string<Domain\Id>,
* entity_id: non-empty-string,
* version?: positive-int,
* }
*/
private array $metadata;

/**
* @param non-empty-string $name
* @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,55 +81,54 @@ 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]);
}

/**
* @return non-empty-string
*/
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;
}

public function producerId(): Domain\Id
{
$class = $this->get(self::METADATA_PRODUCER_TYPE);
$class = $this->metadata[self::METADATA_PRODUCER_TYPE];
$id = $this->metadata[self::METADATA_PRODUCER_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($id);
}

public function entityId(): Domain\Id
{
$class = $this->get(self::METADATA_ENTITY_TYPE);
$class = $this->metadata[self::METADATA_ENTITY_TYPE];
$id = $this->metadata[self::METADATA_ENTITY_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($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
/**
* @param non-empty-string $name
* @return self<TMessage>
*/
public function set(string $name, bool|float|int|string $value): self
{
if (empty($name)) {
if (empty($name)) { // @phpstan-ignore-line
throw new \InvalidArgumentException('Name of the attribute can not be empty.');
}
if (!\is_scalar($value)) {
throw new \InvalidArgumentException(sprintf('Value for attribute "%s" is a scalar.', $name));
}

$new = new self(
$this->uuid(),
Expand Down Expand Up @@ -149,6 +168,9 @@ public function equals(object $envelope): bool
return true;
}

/**
* @return self<TMessage>
*/
public function defineVersion(int $version): self
{
return new self(
Expand All @@ -161,6 +183,9 @@ public function defineVersion(int $version): self
);
}

/**
* @return self<TMessage>
*/
public function defineEntityId(Domain\Id $entityId): self
{
return new self(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(private object $subject, private Event\Envelope $eve
parent::__construct($message, 0, $previous);
}

public function subject()
public function subject(): object
{
return $this->subject;
}
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
9 changes: 6 additions & 3 deletions src/Domain/Event/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
final class Metadata
{
/**
* @var array<string, string>
*/
private array $metadata = [];

private function __construct(array $metadata)
Expand All @@ -34,7 +37,7 @@ public function set(string $name, string $value): void
$this->metadata[$name] = $value;
}

public function has(string $name)
public function has(string $name): bool
{
if (isset($this->metadata[$name])) {
return true;
Expand All @@ -52,7 +55,7 @@ public function get(string $name, string $default = null): ?string
return $this->metadata[$name];
}

public static function fromObject($object): self
public static function fromObject(object $object): self
{
if (!isset($object->__streak_metadata)) {
return new self([]);
Expand All @@ -77,7 +80,7 @@ public static function fromArray(array $metadata): self
return new self($metadata);
}

public function toObject($object): void
public function toObject(object $object): void
{
$object->__streak_metadata = $this->metadata;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Domain/Event/Sourced/Entity/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function applyEvent(Event\Envelope $event): void
}

/**
* @return Event\Sourced\Entity[]
* @return \Generator<int, Event\Sourced\Entity>
*/
public function extractEventSourcedEntities(): iterable
public function extractEventSourcedEntities(): \Generator
{
yield from self::doExtractEventSourcedEntities($this->object);
}
Expand Down Expand Up @@ -128,8 +128,10 @@ 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
private static function doExtractEventSourcedEntities(object $object, array &$ignored = []): \Generator
{
$ignored[] = $object;

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;
}
8 changes: 7 additions & 1 deletion src/Domain/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
*/
interface Id extends Domain\ValueObject
{
/**
* @return non-empty-string
*/
public function toString(): string;

public static function fromString(string $id): Domain\Id;
/**
* @param non-empty-string $id
*/
public static function fromString(string $id): static;
}
Loading

0 comments on commit 43a12a1

Please sign in to comment.