Skip to content

Commit

Permalink
Merge pull request #453 from patchlevel/readonly-attributes
Browse files Browse the repository at this point in the history
change attributes into readonly public objects
  • Loading branch information
DavidBadura authored Jan 3, 2024
2 parents 0f98c5f + 9d12630 commit fc89381
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 89 deletions.
7 changes: 1 addition & 6 deletions src/Attribute/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
final class Aggregate
{
public function __construct(
private string $name,
public readonly string $name,
) {
}

public function name(): string
{
return $this->name;
}
}
8 changes: 1 addition & 7 deletions src/Attribute/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ final class Apply
{
/** @param class-string|null $eventClass */
public function __construct(
private string|null $eventClass = null,
public readonly string|null $eventClass = null,
) {
}

/** @return class-string|null */
public function eventClass(): string|null
{
return $this->eventClass;
}
}
7 changes: 1 addition & 6 deletions src/Attribute/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
final class Event
{
public function __construct(
private string $name,
public readonly string $name,
) {
}

public function name(): string
{
return $this->name;
}
}
14 changes: 2 additions & 12 deletions src/Attribute/Projector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@
final class Projector
{
public function __construct(
private string $name,
private int $version = 0,
public readonly string $name,
public readonly int $version = 0,

Check warning on line 14 in src/Attribute/Projector.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.3, ubuntu-latest)

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ #[Attribute(Attribute::TARGET_CLASS)] final class Projector { - public function __construct(public readonly string $name, public readonly int $version = 0) + public function __construct(public readonly string $name, public readonly int $version = -1) { } }

Check warning on line 14 in src/Attribute/Projector.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.3, ubuntu-latest)

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ #[Attribute(Attribute::TARGET_CLASS)] final class Projector { - public function __construct(public readonly string $name, public readonly int $version = 0) + public function __construct(public readonly string $name, public readonly int $version = 1) { } }
) {
}

public function name(): string
{
return $this->name;
}

public function version(): int
{
return $this->version;
}
}
21 changes: 3 additions & 18 deletions src/Attribute/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,9 @@
final class Snapshot
{
public function __construct(
private string $name,
private int|null $batch = null,
private string|null $version = null,
public readonly string $name,
public readonly int|null $batch = null,
public readonly string|null $version = null,
) {
}

public function name(): string
{
return $this->name;
}

public function batch(): int|null
{
return $this->batch;
}

public function version(): string|null
{
return $this->version;
}
}
8 changes: 1 addition & 7 deletions src/Attribute/Subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ final class Subscribe
{
/** @param class-string $eventClass */
public function __construct(
private string $eventClass,
public readonly string $eventClass,
) {
}

/** @return class-string */
public function eventClass(): string
{
return $this->eventClass;
}
}
17 changes: 4 additions & 13 deletions src/Attribute/SuppressMissingApply.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,20 @@ final class SuppressMissingApply
public const ALL = '*';

/** @var list<class-string> */
private array $suppressEvents = [];
private bool $suppressAll = false;
public readonly array $suppressEvents;
public readonly bool $suppressAll;

/** @param list<class-string>|self::ALL $suppress */
public function __construct(string|array $suppress)
{
if ($suppress === self::ALL) {
$this->suppressEvents = [];
$this->suppressAll = true;

return;
}

$this->suppressEvents = $suppress;
}

/** @return list<class-string> */
public function suppressEvents(): array
{
return $this->suppressEvents;
}

public function suppressAll(): bool
{
return $this->suppressAll;
$this->suppressAll = false;
}
}
2 changes: 1 addition & 1 deletion src/EventBus/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function init(): void

foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$eventClass = $instance->eventClass();
$eventClass = $instance->eventClass;

if (array_key_exists($eventClass, $this->subscribeMethods)) {
throw new DuplicateSubscribeMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ private function findSuppressMissingApply(ReflectionClass $reflector): array
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();

if ($instance->suppressAll()) {
if ($instance->suppressAll) {
$suppressAll = true;

continue;
}

foreach ($instance->suppressEvents() as $event) {
foreach ($instance->suppressEvents as $event) {
$suppressEvents[$event] = true;
}
}
Expand All @@ -97,7 +97,7 @@ private function findAggregateName(ReflectionClass $reflector): string

$aggregateAttribute = $attributeReflectionList[0]->newInstance();

return $aggregateAttribute->name();
return $aggregateAttribute->name;
}

private function findIdProperty(ReflectionClass $reflector): string
Expand Down Expand Up @@ -128,9 +128,9 @@ private function findSnapshot(ReflectionClass $reflector): Snapshot|null
$attribute = $attributeReflectionList[0]->newInstance();

return new Snapshot(
$attribute->name(),
$attribute->batch(),
$attribute->version(),
$attribute->name,
$attribute->batch,
$attribute->version,
);
}

Expand Down Expand Up @@ -159,7 +159,7 @@ private function findApplyMethods(ReflectionClass $reflector, string $aggregate)

foreach ($attributes as $attribute) {
$applyAttribute = $attribute->newInstance();
$eventClass = $applyAttribute->eventClass();
$eventClass = $applyAttribute->eventClass;

if ($eventClass !== null) {
$hasOneNonEmptyApply = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function create(array $paths): AggregateRootRegistry
throw new NoAggregateRoot($class);
}

$aggregateName = $attributes[0]->newInstance()->name();
$aggregateName = $attributes[0]->newInstance()->name;

$result[$aggregateName] = $class;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Metadata/Event/AttributeEventMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public function metadata(string $event): EventMetadata
}

$eventAttribute = $attributeReflectionList[0]->newInstance();
$eventName = $eventAttribute->name();

$this->eventMetadata[$event] = new EventMetadata(
$eventName,
$eventAttribute->name,
$this->splitStream($reflectionClass),
);

Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Event/AttributeEventRegistryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function create(array $paths): EventRegistry
continue;
}

$eventName = $attributes[0]->newInstance()->name();
$eventName = $attributes[0]->newInstance()->name;

$result[$eventName] = $class;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Metadata/Projector/AttributeProjectorMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function metadata(string $projector): ProjectorMetadata
throw new ClassIsNotAProjector($projector);
}

$projection = $attributes[0]->newInstance();
$projectorInfo = $attributes[0]->newInstance();

$methods = $reflector->getMethods();

Expand All @@ -45,7 +45,7 @@ public function metadata(string $projector): ProjectorMetadata

foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$eventClass = $instance->eventClass();
$eventClass = $instance->eventClass;

if (array_key_exists($eventClass, $subscribeMethods)) {
throw new DuplicateSubscribeMethod(
Expand Down Expand Up @@ -87,8 +87,8 @@ public function metadata(string $projector): ProjectorMetadata
}

$metadata = new ProjectorMetadata(
$projection->name(),
$projection->version(),
$projectorInfo->name,
$projectorInfo->version,
$subscribeMethods,
$createMethod,
$dropMethod,
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Attribute/SuppressMissingApplyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public function testSuppressEvents(): void
{
$attribute = new SuppressMissingApply([ProfileCreated::class]);

self::assertSame([ProfileCreated::class], $attribute->suppressEvents());
self::assertSame(false, $attribute->suppressAll());
self::assertSame([ProfileCreated::class], $attribute->suppressEvents);
self::assertSame(false, $attribute->suppressAll);
}

public function testSuppressAll(): void
{
$attribute = new SuppressMissingApply(SuppressMissingApply::ALL);

self::assertSame([], $attribute->suppressEvents());
self::assertSame(true, $attribute->suppressAll());
self::assertSame([], $attribute->suppressEvents);
self::assertSame(true, $attribute->suppressAll);
}
}

0 comments on commit fc89381

Please sign in to comment.