From 42017ad734a9dec5f96fb9881d135c09e58b635f Mon Sep 17 00:00:00 2001 From: thepercival Date: Sun, 26 May 2024 18:11:48 +0200 Subject: [PATCH] fix(counter): readonly --- domain/Counter.php | 17 ++++++++--------- tests/cases/CounterTest.php | 6 +++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/domain/Counter.php b/domain/Counter.php index 71cac78..182ffbe 100644 --- a/domain/Counter.php +++ b/domain/Counter.php @@ -7,7 +7,7 @@ /** * @template T */ -class Counter implements \Countable +readonly class Counter implements \Countable { /** * @param T $countedObject @@ -16,28 +16,27 @@ public function __construct(protected mixed $countedObject, protected int $count { } - public function reset(): void + public function reset2(): self { - $this->count = 0; + return new self($this->countedObject); } /** * @return self */ - public function decrement2(): self + public function decrement(): self { return new self($this->countedObject, $this->count - 1 ); } - public function increment(): int + public function increment(): self { - $this->count++; - return $this->count; + return new self($this->countedObject, $this->count + 1 ); } - public function increase(int $count): void + public function increase(int $count): self { - $this->count += $count; + return new self($this->countedObject, $this->count + $count ); } public function count(): int diff --git a/tests/cases/CounterTest.php b/tests/cases/CounterTest.php index e97cad0..71f7e1b 100644 --- a/tests/cases/CounterTest.php +++ b/tests/cases/CounterTest.php @@ -23,7 +23,7 @@ public function testReset(): void $stdClass = new \stdClass(); $counter = new Counter($stdClass, 4); - $counter->reset(); + $counter->reset2(); self::assertSame(0, $counter->count()); } @@ -32,10 +32,10 @@ public function testDecrement(): void $stdClass = new \stdClass(); $counter = new Counter($stdClass); - self::assertSame(-1, ($counter->decrement2())->count()); + self::assertSame(-1, $counter->decrement()->count()); $counter = new Counter($stdClass, 4); - self::assertSame(3, ($counter->decrement2())->count()); + self::assertSame(3, $counter->decrement()->count()); } public function testIncrement(): void