Skip to content

Commit

Permalink
Code Quality using Rector (#211)
Browse files Browse the repository at this point in the history
## Description



## Checklist
- [ ] Updated CHANGELOG files
- [ ] Updated Documentation
- [ ] Unit Tests Created
- [x] php-cs-fixer
  • Loading branch information
JoshuaEstes authored Aug 1, 2024
1 parent b9781aa commit 16fe677
Show file tree
Hide file tree
Showing 43 changed files with 234 additions and 529 deletions.
8 changes: 5 additions & 3 deletions Aggregate/AbstractAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace SonsOfPHP\Component\EventSourcing\Aggregate;

use DateTimeImmutable;
use Generator;
use SonsOfPHP\Component\EventSourcing\Message\MessageInterface;
use SonsOfPHP\Component\EventSourcing\Metadata;

Expand All @@ -12,7 +14,7 @@
*/
abstract class AbstractAggregate implements AggregateInterface
{
private AggregateIdInterface $id;
private readonly AggregateIdInterface $id;
private AggregateVersionInterface $version;
private array $pendingEvents = [];

Expand Down Expand Up @@ -72,7 +74,7 @@ final public function peekPendingEvents(): iterable
return $this->pendingEvents;
}

final public static function buildFromEvents(AggregateIdInterface $id, \Generator $events): AggregateInterface
final public static function buildFromEvents(AggregateIdInterface $id, Generator $events): AggregateInterface
{
$aggregate = new static($id);
foreach ($events as $event) {
Expand All @@ -93,7 +95,7 @@ final protected function raiseEvent(MessageInterface $event): void
// Prepopulate with some metadata
$event = $event->withMetadata([
Metadata::AGGREGATE_ID => $this->getAggregateId()->toString(),
Metadata::TIMESTAMP => (new \DateTimeImmutable())->format(Metadata::DEFAULT_TIMESTAMP_FORMAT),
Metadata::TIMESTAMP => (new DateTimeImmutable())->format(Metadata::DEFAULT_TIMESTAMP_FORMAT),
Metadata::TIMESTAMP_FORMAT => Metadata::DEFAULT_TIMESTAMP_FORMAT,
]);

Expand Down
2 changes: 1 addition & 1 deletion Aggregate/AbstractAggregateId.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
abstract class AbstractAggregateId implements AggregateIdInterface
{
public function __construct(
private ?string $id = null,
private readonly ?string $id = null,
) {
if (null === $id) {
throw new EventSourcingException('Argument (#1) $id cannot be null');
Expand Down
3 changes: 2 additions & 1 deletion Aggregate/AbstractSnapshotableAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SonsOfPHP\Component\EventSourcing\Aggregate;

use Generator;
use SonsOfPHP\Component\EventSourcing\Snapshot\Snapshot;
use SonsOfPHP\Component\EventSourcing\Snapshot\SnapshotInterface;

Expand Down Expand Up @@ -34,7 +35,7 @@ public function createSnapshot(): SnapshotInterface
/**
* @see SnapshotableAggregateInterface::buildFromSnapshotAndEvents
*/
public static function buildFromSnapshotAndEvents(SnapshotInterface $snapshot, \Generator $messages): SnapshotableAggregateInterface
public static function buildFromSnapshotAndEvents(SnapshotInterface $snapshot, Generator $messages): SnapshotableAggregateInterface
{
$aggregate = static::buildFromSnapshot($snapshot);
foreach ($messages as $msg) {
Expand Down
3 changes: 2 additions & 1 deletion Aggregate/AggregateIdInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace SonsOfPHP\Component\EventSourcing\Aggregate;

use SonsOfPHP\Component\EventSourcing\Exception\EventSourcingException;
use Stringable;

/**
* Aggregate ID.
*
* @author Joshua Estes <[email protected]>
*/
interface AggregateIdInterface extends \Stringable
interface AggregateIdInterface extends Stringable
{
/**
* Create a new AggregateID.
Expand Down
5 changes: 3 additions & 2 deletions Aggregate/AggregateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SonsOfPHP\Component\EventSourcing\Aggregate;

use Generator;
use SonsOfPHP\Component\EventSourcing\Exception\EventSourcingException;
use SonsOfPHP\Component\EventSourcing\Message\MessageInterface;

Expand Down Expand Up @@ -47,9 +48,9 @@ public function peekPendingEvents(): iterable;
/**
* Build Aggregate from a collection of Domain Events.
*
* @param \Generator $events yields MessageInterface objects
* @param Generator $events yields MessageInterface objects
*
* @throws EventSourcingException
*/
public static function buildFromEvents(AggregateIdInterface $id, \Generator $events): self;
public static function buildFromEvents(AggregateIdInterface $id, Generator $events): self;
}
6 changes: 1 addition & 5 deletions Aggregate/AggregateVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
*/
final class AggregateVersion implements AggregateVersionInterface
{
private int $version;

public function __construct(int $version = 0)
public function __construct(private readonly int $version = 0)
{
$this->version = $version;

if (!$this->isValid()) {
throw new EventSourcingException(sprintf('Version "%s" is invalid.', $this->version));
}
Expand Down
11 changes: 2 additions & 9 deletions Aggregate/Repository/AggregateRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@
*/
class AggregateRepository implements AggregateRepositoryInterface
{
public function __construct(
protected string $aggregateClass,
protected EventDispatcherInterface $eventDispatcher,
protected MessageRepositoryInterface $messageRepository,
protected ?MessageEnricherInterface $messageEnricher = null
) {
$this->messageEnricher = $messageEnricher ?? new MessageEnricher(new NullMessageEnricherProvider());
}
public function __construct(protected string $aggregateClass, protected EventDispatcherInterface $eventDispatcher, protected MessageRepositoryInterface $messageRepository, protected MessageEnricherInterface $messageEnricher = new MessageEnricher(new NullMessageEnricherProvider())) {}

public function find(AggregateIdInterface|string $id): ?AggregateInterface
{
Expand All @@ -39,7 +32,7 @@ public function find(AggregateIdInterface|string $id): ?AggregateInterface
$aggregateClass = $this->aggregateClass;

return $aggregateClass::buildFromEvents($id, $events);
} catch (AggregateNotFoundException $e) {
} catch (AggregateNotFoundException) {
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion Aggregate/SnapshotableAggregateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SonsOfPHP\Component\EventSourcing\Aggregate;

use Generator;
use SonsOfPHP\Component\EventSourcing\Snapshot\SnapshotInterface;

/**
Expand All @@ -20,5 +21,5 @@ public function createSnapshot(): SnapshotInterface;

public static function buildFromSnapshot(SnapshotInterface $snapshot): self;

public static function buildFromSnapshotAndEvents(SnapshotInterface $snapshot, \Generator $events): self;
public static function buildFromSnapshotAndEvents(SnapshotInterface $snapshot, Generator $events): self;
}
4 changes: 3 additions & 1 deletion Exception/AggregateNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace SonsOfPHP\Component\EventSourcing\Exception;

use Exception;

/**
* @author Joshua Estes <[email protected]>
*/
class AggregateNotFoundException extends \Exception {}
class AggregateNotFoundException extends Exception {}
2 changes: 1 addition & 1 deletion Exception/EventSourcingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
*
* @author Joshua Estes <[email protected]>
*/
class EventSourcingException extends \Exception {}
class EventSourcingException extends Exception {}
7 changes: 1 addition & 6 deletions Message/Enricher/Handler/EventTypeMessageEnricherHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
*/
final class EventTypeMessageEnricherHandler implements MessageEnricherHandlerInterface
{
private MessageProviderInterface $messageProvider;

public function __construct(MessageProviderInterface $messageProvider)
{
$this->messageProvider = $messageProvider;
}
public function __construct(private readonly MessageProviderInterface $messageProvider) {}

public function enrich(MessageInterface $message): MessageInterface
{
Expand Down
7 changes: 1 addition & 6 deletions Message/Enricher/MessageEnricher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
*/
final class MessageEnricher implements MessageEnricherInterface
{
private MessageEnricherProviderInterface $provider;

public function __construct(MessageEnricherProviderInterface $provider)
{
$this->provider = $provider;
}
public function __construct(private readonly MessageEnricherProviderInterface $provider) {}

public function enrich(MessageInterface $message): MessageInterface
{
Expand Down
17 changes: 11 additions & 6 deletions Message/MessageMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@

namespace SonsOfPHP\Component\EventSourcing\Message;

use ArrayIterator;
use Countable;
use DateTimeImmutable;
use IteratorAggregate;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateId;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateIdInterface;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateVersion;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateVersionInterface;
use SonsOfPHP\Component\EventSourcing\Exception\EventSourcingException;
use SonsOfPHP\Component\EventSourcing\Metadata;
use Traversable;

/**
* The Event Message Metadata is stored in here.
*
* @author Joshua Estes <[email protected]>
*/
final class MessageMetadata implements \IteratorAggregate, \Countable
final class MessageMetadata implements IteratorAggregate, Countable
{
public function __construct(
private array $metadata = [],
Expand All @@ -31,9 +36,9 @@ public function __construct(
], $metadata);
}

public function getIterator(): \Traversable
public function getIterator(): Traversable
{
return new \ArrayIterator($this->metadata);
return new ArrayIterator($this->metadata);
}

public function count(): int
Expand All @@ -46,7 +51,7 @@ public function all(): array
return $this->metadata;
}

public function with(string $key, $value)
public function with(string $key, $value): self
{
$that = clone $this;
$that->metadata[$key] = $value;
Expand Down Expand Up @@ -87,13 +92,13 @@ public function getEventType(): string
return $this->get(Metadata::EVENT_TYPE);
}

public function getTimestamp(): \DateTimeImmutable
public function getTimestamp(): DateTimeImmutable
{
if (false === $this->has(Metadata::TIMESTAMP) || null === $this->get(Metadata::TIMESTAMP)) {
throw new EventSourcingException('Timestamp is required.');
}

return new \DateTimeImmutable($this->get(Metadata::TIMESTAMP));
return new DateTimeImmutable($this->get(Metadata::TIMESTAMP));
}

public function getTimestampFormat(): string
Expand Down
13 changes: 9 additions & 4 deletions Message/MessagePayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@

namespace SonsOfPHP\Component\EventSourcing\Message;

use ArrayIterator;
use Countable;
use IteratorAggregate;
use Traversable;

/**
* The Event Message Payload is stored in here.
*
* @author Joshua Estes <[email protected]>
*/
final class MessagePayload implements \IteratorAggregate, \Countable
final class MessagePayload implements IteratorAggregate, Countable
{
public function __construct(
private array $payload = [],
) {}

public function getIterator(): \Traversable
public function getIterator(): Traversable
{
return new \ArrayIterator($this->payload);
return new ArrayIterator($this->payload);
}

public function count(): int
Expand All @@ -30,7 +35,7 @@ public function all(): array
return $this->payload;
}

public function with(string $key, $value)
public function with(string $key, $value): self
{
$that = clone $this;
$that->payload[$key] = $value;
Expand Down
9 changes: 2 additions & 7 deletions Message/NamespaceMessageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
*/
class NamespaceMessageProvider implements MessageProviderInterface
{
private string $namespace;

public function __construct(string $namespace)
{
$this->namespace = $namespace;
}
public function __construct(private readonly string $namespace) {}

public function getEventTypeForMessage($message): string
{
Expand All @@ -32,7 +27,7 @@ public function getEventTypeForMessage($message): string

$eventType = trim(substr($message, \strlen($this->namespace)), '\\');

if ($this->namespace !== substr($message, 0, \strlen($this->namespace))) {
if (!str_starts_with($message, $this->namespace)) {
throw new EventSourcingException(sprintf('Message "%s" is not in the Namespace "%s"', $message, $this->namespace));
}

Expand Down
2 changes: 1 addition & 1 deletion Message/Repository/InMemoryMessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function find(string|AggregateIdInterface $id, int|AggregateVersionInterf
}

foreach ($this->storage[$id->toString()] as $ver => $message) {
if (null !== $version && $ver <= $version->toInt()) {
if ($version instanceof AggregateVersionInterface && $ver <= $version->toInt()) {
continue;
}

Expand Down
8 changes: 3 additions & 5 deletions Message/Serializer/MessageSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
*/
class MessageSerializer implements MessageSerializerInterface
{
private MessageProviderInterface $messageProvider;
private MessageEnricherInterface $messageEnricher;
private MessageUpcasterInterface $messageUpcaster;
private readonly MessageEnricherInterface $messageEnricher;
private readonly MessageUpcasterInterface $messageUpcaster;

public function __construct(
MessageProviderInterface $messageProvider,
private readonly MessageProviderInterface $messageProvider,
MessageEnricherInterface $messageEnricher = null,
MessageUpcasterInterface $messageUpcaster = null
) {
$this->messageProvider = $messageProvider;
$this->messageEnricher = $messageEnricher ?? new MessageEnricher(new AllMessageEnricherProvider([new EventTypeMessageEnricherHandler($this->messageProvider)]));
$this->messageUpcaster = $messageUpcaster ?? new MessageUpcaster(new NullMessageUpcasterProvider());
}
Expand Down
7 changes: 1 addition & 6 deletions Message/Upcaster/MessageUpcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
*/
final class MessageUpcaster implements MessageUpcasterInterface
{
private MessageUpcasterProviderInterface $provider;

public function __construct(MessageUpcasterProviderInterface $provider)
{
$this->provider = $provider;
}
public function __construct(private readonly MessageUpcasterProviderInterface $provider) {}

public function upcast(array $data): array
{
Expand Down
11 changes: 1 addition & 10 deletions Snapshot/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
*/
final class Snapshot implements SnapshotInterface
{
private AggregateIdInterface $id;
private AggregateVersionInterface $version;
private $state;

public function __construct(AggregateIdInterface $id, AggregateVersionInterface $version, $state)
{
$this->id = $id;
$this->version = $version;
$this->state = $state;
}
public function __construct(private readonly AggregateIdInterface $id, private readonly AggregateVersionInterface $version, private $state) {}

public function getAggregateId(): AggregateIdInterface
{
Expand Down
Loading

0 comments on commit 16fe677

Please sign in to comment.